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.
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
First of all, we need to import the pandas module which can be done by running the command:
Input File: Let’s suppose the excel file looks like this
Now we can import the excel file using the read_excel function in pandas, as shown below:
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
index_col where we can specify the sheet of which the data frame should be made of and
index_col specifies the title column.
The third statement concatenates both the sheets. Now to check the whole data frame, we can simply run the following command:
To view 5 columns from the top and from the bottom of the data frame, we can run the command:
tail() method also take arguments as numbers for the number of columns to show.
shape() method can be used to view the number of rows and columns in the data frame as follows:
If any column contains numerical data, we can sort that column using the
sort_values() method in pandas as follows:
Now, lets suppose we want the top 5 values of the sorted column, we can use the
head() method here:
We can do that with any numerical column of the data frame as shown below:
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:
This can also be done separately for all the numerical columns using the following command:
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:
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:
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