Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data preprocessing
  • Data Manipulation
  • Data Analysis using Pandas
  • EDA
  • Pandas Exercise
  • Pandas AI
  • Numpy
  • Matplotlib
  • Plotly
  • Data Analysis
  • Machine Learning
  • Data science
Open In App
Next Article:
How to Read Text Files with Pandas?
Next article icon

How to Read Text Files with Pandas?

Last Updated : 21 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 Files with Pandas

Below are the methods by which we can read text files with Pandas:

  • Using read_csv()
  • Using read_table()
  • Using read_fwf()

Read Text Files with Pandas 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: 

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

In this example, we are using read_csv() function to read the csv file.

Python
# Read Text Files with Pandas using read_csv()  # importing pandas import pandas as pd  # read text file into pandas DataFrame df = pd.read_csv("gfg.txt", sep=" ")  # display DataFrame print(df) 

Output:

Example 2

In this example, 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.

Python
# Read Text Files with Pandas using read_csv()  # importing pandas import pandas as pd  # read text file into pandas DataFrame and # create header df = pd.read_csv("gfg.txt", sep=" ", header=None)  # display DataFrame print(df) 

Output:

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.

Python
# Read Text Files with Pandas using read_csv()  # importing pandas import pandas as pd  # read text file into pandas DataFrame and create  # header with names df = pd.read_csv("gfg.txt", sep=" ", header=None,                   names=["Team1", "Team2"])  # display DataFrame print(df) 

Output:

Read Text Files with Pandas 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 = ' ')

Parameters:

  • filename.txt: As the name suggests it is the name of the text file from which we want to read data.

Example: In this example, we are using read_table() function to read the table.

Python
# Read Text Files with Pandas using read_table()  # importing pandas import pandas as pd  # read text file into pandas DataFrame df = pd.read_table("gfg.txt", delimiter=" ")  # display DataFrame print(df) 

Output:

Read Text Files with Pandas 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_fwf() 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_fwf() read the contents effectively into separate columns.

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

Parameters:

  • filename.txt: As the name suggests it is the name of the text file from which we want to read data.

Example: In this example, we are using read_fwf to read the data.

Python
# Read Text Files with Pandas using read_fwf()  # importing pandas import pandas as pd  # read text file into pandas DataFrame df = pd.read_fwf("gfg.txt")  # display DataFrame print(df) 

Output:



Next Article
How to Read Text Files with Pandas?

R

rushi_javiya
Improve
Article Tags :
  • Python
  • Python-pandas
  • Python pandas-io
Practice Tags :
  • python

Similar Reads

    How to Read JSON Files with Pandas?
    JSON (JavaScript Object Notation) store data using key-value pairs. Reading JSON files using Pandas is simple and helpful when you're working with data in .json format. There are mainly three methods to read Json file using Pandas Some of them are:Using pd.read_json() MethodUsing JSON Module and pd.
    2 min read
    Reading rpt files with Pandas
    In most cases, we usually have a CSV file to load the data from, but there are other formats such as JSON, rpt, TSV, etc. that can be used to store data. Pandas provide us with the utility to load data from them. In this article, we'll see how we can load data from an rpt file with the use of Pandas
    2 min read
    How to Read CSV Files with NumPy?
    Reading CSV files is a common task when working with data in Python. In this article we will see how to read CSV files using Numpy's loadtxt() and genfromtxt() methods.1. Using NumPy loadtxt() methodThe loadtext() method is faster and simpler for reading CSV files. It is best when the file has consi
    2 min read
    How to read csv file with Pandas without header?
    Prerequisites: Pandas A header of the CSV file is an array of values assigned to each of the columns. It acts as a row header for the data. This article discusses how we can read a csv file without header using pandas. To do this header attribute should be set to None while reading the file. Syntax:
    1 min read
    How to read large text files in Python?
    In this article, we will try to understand how to read a large text file using the fastest way, with less memory usage using Python.  To read large text files in Python, we can use the file object as an iterator to iterate over the file and perform the required task. Since the iterator just iterates
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences