Skip to content
Related Articles

Related Articles

Working with excel files using Pandas
  • Last Updated : 19 Feb, 2020

Excel sheet is one of the most ubiquitous forms of files in the IT Industry. Everyone who uses a computer at one time or the other have come across and worked with excel spreadsheets. This popularity of excel is due to its vast range of applications in the field of storing and manipulation of data in a tabular and systematic form. Moreover, excel sheets are very instinctive and user-friendly, which makes it ideal for manipulating large datasets even for less technical folks. If you are looking for places to learn to manipulate and automate stuff in excel files using Python, look no more. You are at the right place.

In this article, you will learn how to use Pandas to work with Excel spreadsheets. At the end of the article, you will have the knowledge of:

  • Necessary modules needed for this and how to set them up in your system.
  • Reading data from excel file into pandas using Python.
  • Exploring the data from excel files in Pandas.
  • Using functions to manipulate and reshape the data in Pandas.

Installation

To install pandas in Anaconda, we can use the following command in Anaconda Terminal:

conda install pandas

To install pandas in regular Python (Non-Anaconda), we can use the following command in the command prompt:

pip install pandas

Getting Started

First of all, we need to import the pandas module which can be done by running the command:






import pandas as pds

Input File: Let’s suppose the excel file looks like this

Sheet 1:
python-pandas

Sheet 2:

python-pandas-read-excel

Now we can import the excel file using the read_excel function in pandas, as shown below:




file =('path_of_excel_file')
newData = pds.read_excel(file)
newData

Output:
python-pandas-read-excel

The second statement reads the data from excel and stores it into a pandas Data Frame which is represented by the variable newData. If there are multiple sheets in the excel workbook, the command will import data of the first sheet. To make a data frame with all the sheets in the workbook, the easiest method is to create different data frames separately and then concatenate them. The read_excel method takes argument sheet_name and index_col where we can specify the sheet of which the data frame should be made of and index_col specifies the title column.

Example:






sheet1 = pds.read_excel(file
                        sheet_name = 0
                        index_col = 0)
  
sheet2 = pds.read_excel(file
                        sheet_name = 1
                        index_col = 0)
  
newData = pds.concat([sheet1, sheet2])

The third statement concatenates both the sheets. Now to check the whole data frame, we can simply run the following command:




newData

Output:
python-pandas-read-excel

To view 5 columns from the top and from the bottom of the data frame, we can run the command:




newData.head()
newData.tail()

Output:

python-pandas-read-excel

python-pandas-read-excel

This head() and tail() method also take arguments as numbers for the number of columns to show.
The shape() method can be used to view the number of rows and columns in the data frame as follows:




newData.shape

Output:

python-pandas-read-excel

If any column contains numerical data, we can sort that column using the sort_values() method in pandas as follows:




sorted_column = newData.sort_values(['Height'], ascending = False)

Now, lets suppose we want the top 5 values of the sorted column, we can use the head() method here:




sorted_column['Height'].head(5)

Output:
python-pandas-read-excel

We can do that with any numerical column of the data frame as shown below:




newData['Weight'].head()

Output:
python-pandas-read-excel

Now, suppose our data is mostly numerical. We can get the statistical information like mean, max, min, etc. about the data frame using the describe() method as shown below:




newData.describe()

Output:
python-pandas-read-excel

This can also be done separately for all the numerical columns using the following command:




newData['Weight'].mean()

Output:

python-pandas-read-excel

Other statistical information can also be calculated using the respective methods.
Like in excel, formulas can also be applied and calculated columns can be created as follows:




newData['calculated_column']= newData[“Height”] + newData[“Weight”]
newData['calculated_column'].head()

Output:
python-pandas-read-excel

After operating on the data in the data frame, we can export the data back to an excel file using the method to_excel. For this we need to specify an output excel file where the transformed data is to be written, as shown below:




newData.to_excel('Output File.xlsx')

Output:
python-pandas-read-excel

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :