How to load data from text file in Pandas

  1. read_csv() method to load data from text file
  2. read_fwf() method to load width formated text file to Pandas dataframe
  3. read_table() method to load text file to Pandas dataframe

We will introduce the methods to load the data from txt file with Pandas dataframe. We will also go through the available options.

First, we will create a simple text file called sample.txt and add the following lines to the file:

45 apple orange banana mango
12 orange kiwi onion tomato

We need to save it to the same directory from where Python script will be running.

read_csv() method to load data from text file

read_csv() is the best way to convert text file into Pandas Dataframe. We need to set header=None as we don’t have any header in the above-created file. We can also set keep_default_na=False inside the method if we wish to replace empty values with NaN.

Example Codes:

# python 3.x
import pandas as pd
df = pd.read_csv(
    'sample.txt', sep=" ",header=None)
print(df)

Output:

    0       1       2       3       4
0  45   apple  orange  banana   mango
1  12  orange    kiwi   onion  tomato

We set sep=" " because values are separated by a single white space. Similarly, we can set sep="," if we read data from a comma-separated file. Replace the white spaces inside sample.txt with , and then run the code after replacing sep=" " with sep=",".

Sample.txt

45,apple,orange,banana,mango
12,orange,kiwi,,tomato

Code:

# python 3.x
import pandas as pd
df = pd.read_csv(
    'sample.txt', sep=",",header=None)
print(df)

Output:

    0       1       2       3       4
0  45   apple  orange  banana   mango
1  12  orange    kiwi     NaN  tomato

read_fwf() method to load width formated text file to Pandas dataframe

read_fwf() is very helpful when we have a width formatted text file. we can’t use sep because different values may have different delimiters. Consider the following text file:

Sample.txt

45 apple  orange banana mango
12 orange kiwi   onion  tomato

If we look at sample.text we will see that delimiter is not the same for each value. So read_fwf() will do the job here.

Code:

# python 3.x
import pandas as pd
df = pd.read_fwf(
    'sample.txt',header=None)
print(df)

Output:

    0       1       2       3       4
0  45   apple  orange  banana   mango
1  12  orange    kiwi   onion  tomato

read_table() method to load text file to Pandas dataframe

read_table() is another approach to load data from text file to Pandas dataframe.

Sample.txt:

45 apple orange banana mango
12 orange kiwi onion tomato

Code:

# python 3.x
import pandas as pd
df = pd.read_table(
    'sample.txt',header=None,sep=" ")
print(df)

Output:

    0       1       2       3       4
0  45   apple  orange  banana   mango
1  12  orange    kiwi   onion  tomato
comments powered by Disqus