| by Arround The Web | No comments

Pandas Read Text File

In Python, “Text Files” is the popular format that is used to store information or data in a structured or unstructured format. Python provides various modules and functions for performing different file manipulation operations on text files. For example, to read the text file, various methods of the “pandas” module are used in Python.

This article will explain a comprehensive guide on reading text files in Python using several examples.

How to Read a Text File Utilizing Python Pandas?

The following methods are used to read a text file using Pandas:

The following is the original text file:

Method 1: Reading Text File Using “pandas.read_csv()” Method

The “pandas.read_csv()” is used to read the CSV file in Python, but it can also be used to read the “Text” file. The syntax of “pandas.read_csv()” method is shown below:

pd.read_csv(filepath_or_buffer, sep=' ,' , usecols=None, engine=None,header='infer', index_col=None, skiprows=None, nrows=None)

 

In this syntax:

  • The “filepath” parameter represents the file name or path that should be read.
  • The “sep” and “header” parameters represent the delimiter value and header value used while reading the file.
  • The “indx_col” parameter indicates the column that is used as the index of the retrieved DataFrame.
  • The remaining/other parameters are optional and execute particular operations.

For example, in the code below, the “pandas.read_csv()” method takes the text file and separator name as an argument and retrieves the DataFrame.

import pandas
df = pandas.read_csv("example.txt", sep=" ")
print(df)

 

The specified file has been read successfully:

Using Default and Custom Header

We can also read the file using the default header value or with a custom header value. The “sep” and “header=None” parameters are passed to the “pandas.read_csv()” method to read the text file without the header. We can pass the new custom names for the header using the “names” parameter. Take the following examples to demonstrate this:

import pandas
df = pandas.read_csv("example.txt", sep=" ", header=None)
print(df, '\n')
df = pandas.read_csv("example.txt", sep=" ", header=None, names=["X", "Y", "Z"])
print(df)

 

The execution of the above code retrieved the following output to the console:

Using Comma Delimiter

The “pandas.read_csv()” method can also read the text file using different separators or delimiter values. In the below code, the “comma (,)” separator is assigned to the “sep=” parameter of this method to read the text file:

import pandas
df = pandas.read_csv("example.txt", sep=",")
print(df)

 

This code retrieves the below output to the console:

Method 2: Reading Text File Using “pandas.read_table()” Method

The “pandas.read_table()” method is used to read the general delimited file and retrieve the Pandas DataFrame. Here in the below code, the “pandas.read_table()” method reads the specified text file “example.txt” based on the “delimiter” value:

import pandas
df = pandas.read_table("example.txt", delimiter=" ")
print(df)

 

The above code execution retrieves the below output:

Method 3: Reading Text File Using “pandas.read_fwf()” Method

The “pandas.read_fwf()” method is used to read a fixed-width formatted lines table into the DataFrame of Pandas. This method can also be utilized for reading the text file in Python. The syntax of “pandas.read_fwf()” method is shown below:

pandas.read_fwf(filepath_or_buffer, *, colspecs='infer', widths=None, infer_nrows=100, dtype_backend=_NoDefault.no_default, **kwds)

 

In this syntax:

  • The “filepath” parameter indicates the path of the file or file-like object.
  • The “colspecs” parameter specifies the column position or ranges of the file.
  • The “width” parameter is utilized to indicate the width of each file column.
  • The remaining/other parameters are not mandatory and utilized for various operations.

The following code is utilized to read the text file:

import pandas
df = pandas.read_fwf("example.txt")
print(df)

 

The particular text file has been read successfully:

Conclusion

The “pandas.read_csv()”, “pandas.read_table()”, and “pandas.read_fwf()” methods are used to read a text file using Pandas in Python. The “pandas.read_csv()” is utilized for reading the text file with the default header, custom header and using different delimited values. The other methods can also efficiently read the text files in Python. This tutorial delivered in-depth detail on Pandas reading text files using multiple examples.

Share Button

Source: linuxhint.com

Leave a Reply