16.9 C
London
Sunday, June 30, 2024
HomePandas in PythonInput/Output in PythonHow to Read a Text File with Pandas (Including Examples)

How to Read a Text File with Pandas (Including Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

To read a text file with pandas in Python, you can use the following basic syntax:

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

This tutorial provides several examples of how to use this function in practice.

Read a Text File with a Header

Suppose we have the following text file called data.txt with a header:

Read text file in Pandas

To read this file into a pandas DataFrame, we can use the following syntax:

import pandas as pd

#read text file into pandas DataFrame
df = pd.read_csv("data.txt", sep=" ")

#display DataFrame
print(df)

   column1  column2
0        1        4
1        3        4
2        2        5
3        7        9
4        9        1
5        6        3
6        4        4
7        5        2
8        4        8
9        6        8

We can print the class of the DataFrame and find the number of rows and columns using the following syntax:

#display class of DataFrame
print(type(df))



#display number of rows and columns in DataFrame
df.shape

(10, 2)

We can see that df is a pandas DataFrame with 10 rows and 2 columns.

Read a Text File with No Header

Suppose we have the following text file called data.txt with no headers:

Pandas read text file with no headers

To read this file into a pandas DataFrame, we can use the following syntax:

#read text file into pandas DataFrame
df = pd.read_csv("data.txt", sep=" ", header=None)

#display DataFrame
print(df)

   0  1
0  1  4
1  3  4
2  2  5
3  7  9
4  9  1
5  6  3
6  4  4
7  5  2
8  4  8
9  6  8

Since the text file had no headers, pandas simply named the columns 0 and 1.

Read a Text File with No Header & Specify Column Names

If we’d like, we can assign column names while importing the text file by using the names argument:

#read text file into pandas DataFrame and specify column names
df = pd.read_csv("data.txt", sep=" ", header=None, names=["A", "B"])

#display DataFrame
print(df)

   A  B
0  1  4
1  3  4
2  2  5
3  7  9
4  9  1
5  6  3
6  4  4
7  5  2
8  4  8
9  6  8

Additional Resources

How to Read CSV Files with Pandas
How to Read Excel Files with Pandas
How to Read a JSON File with Pandas

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories