Read Text Files Using Pandas – A Brief Reference

Pandas Read Text

This is article is a simple tutorial about how we can read text files using the Pandas library in Python. Text files nowadays help to store a lot of raw information. They are one of the simplest ways of accessing a particular piece of information. They can hold the following:

  1. Code
  2. Raw info
  3. Links
  4. Messages and many more

So, to manage it there are some tools and techniques through which we can easily extract what we need. One of those is through computer programming. Let us see in more detail how it works.


Steps to Read Text Files using Python Pandas

A file in a computer can store a various number of files and extensions. In general, files are for storing information about anything. So, there is no specific definition for that. But, their extensions speak a lot about them. Every extension defines a different bit of data stored in it.

For example, a file of a particular programming language like python has an extension of .py. Extensions are simply to poster how the type of file it is and what data it represents.

Creating a sample.txt file in windows

The process is very simple to create a text file in windows. Follow the steps below:

  1. Go to the windows search bar and type in Notepad. Click on that.
  2. It opens with a blank page. There we can put any of the text or info we want and make changes to it anytime.
  3. After finishing the work, press Ctrl+S or go to the File option in the top left corner and click on Save to save the file in your desired location.

Read text files in Pandas

Pandas is a library in Python that covers some of the necessary data. It is mainly in use in the fields of Data Science and Machine Learning. It is an open-source project just like Python where anyone can contribute to the development.

Go to this link for more info. Following are its uses:

  1. Data analysis
  2. Data preprocessing
  3. Data cleaning
  4. Data wrangling
  5. Accessing information from files embedded on external links
  6. Extracting data from JSON, SQL, Excel file formats.

Purely built-in Python and other supportive libraries it provides a best workspace for managing a ton of data

Text-file Methods in Python Pandas

In Data Science the amount of information we fetch is huge so it is all enclosed in a file called a dataset. This dataset can be of thousands of rows and columns with various inputs. Pandas provide a lot more functions and methods for processing our data.

  1. read_excel() : read an excel file
  2. read_csv() : read a comma separated value file
  3. info() : display the information about all columns
  4. isna() : check the missing values
  5. sum() : sum of the values of any column of various data types
  6. dropna() : dropping a column
  7. head() : return first 5 rows of the dataset. But, can return according to the number when we give inside the braces.

These are the main functions. To know more about the library in advance, visit this link for the getting started guide.

Installing Pandas

Python PIP command namely ‘package installer for python’ makes it easy to install Pandas in any system. But, there are some limitations to this. But first, go to command prompt and type in

python --version

Make sure you have Python 3.6 or later.

Python Version Check

Next type pip install pandas as shown below:

read a text file using pandas
pandas installation

Installing Pandas using Anaconda

Note: For this you need Anaconda installed on your system.

Pandas come preinstalled with Anaconda but for reference, we shall know how to add new libraries through the conda prompt.

So, open the Anaconda prompt and type in this command

conda install pandas
Pandas Install Anaconda Prompt

Thus, we confirmed that this library is already present in conda environment.

Import Pandas

So, after installing and getting some rough info it’s time to get more familiar with it. The first thing to do is import the library and check whether it is correctly installed or not.

import pandas

If it gives no error after installation then it’s ready to use.

Reading a file in pandas

The tutorial is very simple about reading a file. We will read three types of files in this.

  1. Coma Separated Value files
  2. Excel files
  3. Text files

There are special functions for reading each file. As discussed earlier it has read_excel() and read_csv(). environment – ‘Jupyter Notebooks’

Reading an excel file in Python:

Sample file used –“train.xlsx”

import pandas as pd                       
data = pd.read_excel('train.xlsx')      
data.head()                                    

Output:

Reading An Excel File

Reading a text file in Python:

Sample file used – “titanic.txt”

import pandas as pd
data_1 = read_csv('titanic.txt')
data_1.head()

Output:

Reading A Text File

Conclusion

Here, we conclude this topic. So, in this way, we can read some of the files through pandas and make our data science and machine learning journey smoother. I think this is the most relevant way to getting started with pandas and configuring it in the system.