How To Read A Text File Using Pandas – Detailed Guide

Pandas allow you to read text files with a single line of code.

You can read the text file in Pandas using the pd.read_csv(“sample.txt”) statement.

In this tutorial, you’ll learn how to read a text file and create a dataframe using the Pandas library.

If you’re in Hurry

You can read the text file using pandas using the below code.

You need to use the separator as space using “ “. Because the default separator of the read_csv() method is ,.

Code

import pandas as pd

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

df

The text file will be read, and a dataframe will be created.

If You Want to Understand Details, Read on…

You can read a text file and create a dataframe using the read_csv() method available in the pandas library.

This method allows you to read files with the different separators, and also it allows you to handle headers while reading the file.

There are also other methods to read the text files. They are read_fwf() and read_table(). The main difference between these methods is the default separators.

Using read_csv() Method

This section teaches you how to read a text file using the read_csv() method.

You need to pass the file name and the separator as to read the text file separated by spaces.

If you do not pass the sep parameter, the complete line will be placed in one column of the dataframe.

Code

The below code demonstrates how to read the sample.txt file using the read_csv() method.

import pandas as pd

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

df

The text file will be read, and a dataframe will be created.

Dataframe Will Look Like

ProductsQuantity
0CPU1
1Speaker2
2Keyboard3
3Mouse4

Read Text File With Header

This section teaches you how to read a text file with headers.

You can control how to read header information using the header parameter.

To use the first line as a header, use header=0.

The default value for the header parameter is infer, which will infer the header information from the file. If the header parameter is NOT passed, the behaviour is similar to header=0, which uses the first line as the header.

Code

The below code demonstrates how to use the header parameter in the read_csv() method.

df = pd.read_csv("sample.txt", sep=" ", header=0)

df

The first line of the file is used as a header.

Dataframe Will Look Like

ProductsQuantity
0CPU1
1Speaker2
2Keyboard3
3Mouse4

Read Text File Without Header

This section teaches you how to read a text file without a header.

To read a file without a header, use the header=None parameter.

But in this case, if the file contains a header, you also need to ignore the first row using the skiprows=1 parameter. You can skip this parameter if the file doesn’t have header information.

Code

The code below demonstrates how to read a text file without a header and ignore the first line if the file contains the header information.

df = pd.read_csv("sample.txt", sep=" ",  header=None, skiprows=1)

df

Dataframe Will Look Like

01
0CPU1
1Speaker2
2Keyboard3
3Mouse4

You can also Add Header To Pandas Dataframe after creating it.

Read Text File Without Header And Specify Column Names

This section teaches you how to read a text file without a header and explicitly specify column names.

This is useful when the file doesn’t contain header information, and you want to assign meaningful column names.

You can use the header=None to read a file without a header and assign the column names using the names= [“Column name 1“, “Column name 2”] parameter. The column names must be passed as a list of parameters.

Code

The below code demonstrates how to read a text file without header and names manually.

df = pd.read_csv("sample.txt", sep=" ", header=None, names=["Product Name", "Quantity"], skiprows=1)

df

Dataframe Will Look Like

Product NameQuantity
0CPU1
1Speaker2
2Keyboard3
3Mouse4

This is how the read_csv() method can read a text file with or without headers.

Using read_fwf() method

This section teaches you how to read a text file using the read_fwf() method.

It reads a table of fixed-width formatted lines into DataFrame. Fixed Width formatted means each column in the text file is separated using a fixed width.

To understand more about the other optional parameters supported by read_fwf(), read the doc.

Code

The code below demonstrates how to use the read_fwf() method to read a text file.

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

df

Dataframe Will Look Like

Products Quantity
0CPU 1
1Speaker 2
2Keyboard 3
3Mouse 4

Using read_table() method

This section teaches you how to read a text file using the read_table() method.

It reads a general table like text file into a dataframe.

It uses the \t as the default separator. This means the text file is separated using a tab. You can use other separators to read the file using the sep parameter.

To understand more about the other optional parameters supported by read_table(), read the doc.

Code

The code below demonstrates how to use the read_ table() method to read a text file.

df = pd.read_table('sample.txt', sep=" ")

df

Dataframe Will Look Like

ProductsQuantity
0CPU1
1Speaker2
2Keyboard3
3Mouse4

Conclusion

You’ve learned how to read a text file using Pandas library. Also, you’ve learned how to handle headers while reading the text files and how to skip headers if you do not want them.

You May Also Like

Leave a Comment