Read text file python pandas

In this article, we will discuss how to read text files with pandas in python. In python, the pandas module allows us to load DataFrames from external files and work on them. The dataset can be in different types of files.

Text File Used:

Read text file python pandas

Method 1: Using read_csv()

We will read the text file with pandas using the read_csv() function. Along with the text file, we also pass separator as a single space (‘ ’) for the space character because, for text files, the space character will separate each field. There are three parameters we can pass to the read_csv() function.

Syntax: 

data=pandas.read_csv(‘filename.txt’, sep=’ ‘, header=None, names=[“Column1”, “Column2”])

Parameters:

  • filename.txt: As the name suggests it is the name of the text file from which we want to read data.
  • sep: It is a separator field. In the text file, we use the space character(‘ ‘) as the separator.
  • header: This is an optional field. By default, it will take the first line of the text file as a header. If we use header=None then it will create the header.
  • names: We can assign column names while importing the text file by using the names argument.

Example 1:  

Python3

import pandas as pd

df = pd.read_csv("gfg.txt", sep=" ")

print(df)

Output:

Read text file python pandas

Example 2:

In example 2 we will make the header filed equal to None. This will create a default header in the output. And take the first line of the text file as data entry. The created header name will be a number starting from 0.

Python3

import pandas as pd

df = pd.read_csv("gfg.txt", sep=" ", header=None)

print(df)

Output:

Read text file python pandas

Example 3:

In the above output, we can see it creates a header starting from number 0. But we can also give names to the header. In this example, we will see how to create a header with a name using pandas.

Python3

import pandas as pd

df = pd.read_csv("gfg.txt", sep=" ", header=None

                 names=["Team1", "Team2"])

print(df)

Output:

Read text file python pandas

Method 2: Using read_table()

We can read data from a text file using read_table() in pandas. This function reads a general delimited file to a DataFrame object. This function is essentially the same as the read_csv() function but with the delimiter = ‘\t’, instead of a comma by default. We will read data with the read_table function making separator equal to a single space(‘ ‘).

Syntax: 

data=pandas.read_table('filename.txt', delimiter = ' ')

Example:

Python3

import pandas as pd

df = pd.read_table("gfg.txt", delimiter=" ")

print(df)

Output:

Read text file python pandas

Method 3: Using read_fwf()

The fwf in the read_fwf() function stands for fixed-width lines. We can use this function to load DataFrames from files. This function also supports text files. We will read data from the text files using the read_fef() function with pandas. It also supports optionally iterating or breaking the file into chunks. Since the columns in the text file were separated with a fixed width, this read_fef() read the contents effectively into separate columns.

Syntax: 

data=pandas.read_fwf('filename.txt')

Example:

Python3

import pandas as pd

df = pd.read_fwf("gfg.txt")

print(df)

Output:

Read text file python pandas


How do I read a .TXT file in pandas?

We will read the text file with pandas using the read_csv() function. Along with the text file, we also pass separator as a single space (' ') for the space character because, for text files, the space character will separate each field. There are three parameters we can pass to the read_csv() function.

How do you read a text file in Python?

To read a text file in Python, you follow these steps: First, open a text file for reading by using the open() function. Second, read text from the text file using the file read() , readline() , or readlines() method of the file object. Third, close the file using the file close() method.

How do I read a Python file in pandas?

Read CSV Files.
Load the CSV into a DataFrame: import pandas as pd. df = pd.read_csv('data.csv') ... .
Print the DataFrame without the to_string() method: import pandas as pd. ... .
Check the number of maximum returned rows: import pandas as pd. ... .
Increase the maximum number of rows to display the entire DataFrame: import pandas as pd..

How do I convert a TXT to a DataFrame in Python?

Methods to convert text file to DataFrame.
read_csv() method..
read_table() function..
read_fwf() function..