How to read a text file using Pandas in Python
A lot of data is stored in .txt files. So, in this blog, we are going to master how to read a text file using pandas in Python. You can not only read the text file but also make effective changes to make the data of your text file look more attractive in python. There are several ways of reading a text file using pandas in Python and a lot of them are mentioned in this blog.
With this tutorial you will learn:
- How to read the whole text file using pandas
- How to read a specific or particular line from a text file
We are going to use this text file as an example.
pd.read_csv() : Reading a txt file using pandas
This method of reading a text file is very simple and easy to understand. The usage of this method is given below.
# import the library import pandas as pd # Use the method and write the name of the text file as written here DataFrame=pd.read_csv('Text_file.txt',sep=' ') # print the DataFrame print(DataFrame)
Here the name of the text file is ‘Text_file’ and .txt is the type of the file that shows that it is a text file and sep is the separation which is set to one space for better resolution.
Output:
To make it look more attractive we can set the header to None and add headers accordingly with the help of names.
# import the library import pandas as pd # Use the method and write the name of the text file as written here DataFrame=pd.read_csv('Text_file.txt',sep=' ',header=None,names=['Celebrity','Profession']) # print the DataFrame print(DataFrame)
Output:
You can also read: How to remove blank lines from a .txt file in Python
pd.read_table() : read and print a txt file in table format
You can use this method as well by replacing the sep
function with delimiter.
# import the library import pandas as pd # Use the method and write the name of the text file as written here DataFrame=pd.read_table('Text_file.txt',delimiter=' ',header=None,names=['Celebrity','Profession']) # print the DataFrame print(DataFrame)
Output:
pd.read_fwf() :
This is the method you can use to display a text file as it is without any changes. This method is used very rarely.
# import the library import pandas as pd # Use the method and write the name of the text file as written here DataFrame=pd.read_fwf('Text_file.txt') # print the DataFrame print(DataFrame)
Output:
Read particular lines from a txt file using Pandas in Python
Other than reading a complete text file you can also write a program to read particular lines of the text file. You can use nrows
to print the number of rows you want in the output.
This is a normal text file.
# import the library import pandas as pd # Use the method and write the name of the text file as written here DataFrame=pd.read_csv('Text_file2.txt',sep=' ',header=None,nrows=4) # print the DataFrame print(DataFrame)
Here, the first four lines of the text will get printed in the output.
Output:
Skip a particular line from a text file and print the rest using pandas
You can also use skiprows
to skip the particular lines from being printed.
# import the library import pandas as pd # Use the method and write the name of the text file as written here DataFrame=pd.read_csv('Text_file2.txt',sep=' ',header=None,skiprows=4) # print the DataFrame print(DataFrame)
Here the first four lines of the text file will not get printed.
Output:
readlines():
To read only a particular line from the entire text you may use readlines()
. The syntax is given below.
# open the text file document=open('Text_file2.txt') # command to readlines() data=document.readlines() # print the line by giving its index print(data[4]) # index of the line is 4
Output:
Leave a Reply