~ 5 min read

Reading Excel Files with Pandas

By: Adam Richardson
Share:

Understanding Pandas’ read_excel Function

In data analysis, pandas is the most popular library when it comes to data manipulation, especially tabular data or data that consists of rows and columns, just like an Excel spreadsheet. In this blog post, we will focus on the read_excel function of the pandas library.

The function read_excel() has numerous parameters, and we will only go through the essential ones. The basic parameter that we need to use is the path of the Excel file that we want to read.

import pandas as pd

df = pd.read_excel('example.xlsx')

This code reads the file ‘example.xlsx’ and turns it into a pandas DataFrame, assigning it to the variable df. The default behavior of the read_excel() function is to read the first sheet of the Excel file. If you need to read other sheets, you can use the sheet_name parameter.

import pandas as pd

df = pd.read_excel('example.xlsx', sheet_name='Sheet2')

This code reads the sheet named ‘Sheet2’ of the Excel file and assigns it to the variable df.

In addition to sheet_name parameter, read_excel() has various other optional parameters that can be used to adjust the behavior of the function. For instance, the usecols parameter can be used to specify which columns we want to read.

import pandas as pd

df = pd.read_excel('example.xlsx', usecols=['A', 'B', 'D'])

This code reads only the columns A, B, and D of the first sheet of the Excel file and assigns them to the variable df. In case you want to skip some rows, use the skiprows parameter.

import pandas as pd

df = pd.read_excel('example.xlsx', skiprows=2)

This code skips the first two rows of the first sheet of the Excel file and reads the rest, assigning it to the variable df.

Now that we’ve got an understanding of the read_excel function, we can work on analyzing and manipulating data from our spreadsheets with pandas!

Specifying Sheet Name and Data Range

Pandas’ read_excel function provides an easy way to read specific sheets from an Excel file. We can choose which sheet to read by specifying its name or number. We can also select a specific range of data to be read from that sheet.

To read a particular sheet, we can use the sheet_name parameter. Here is an example of how to read the sheet named ‘Sheet1’ of an Excel file:

import pandas as pd

df = pd.read_excel('example.xlsx', sheet_name='Sheet1')

The above code reads the data from the sheet named ‘Sheet1’ and stores it in a pandas DataFrame called ‘df’.

Sometimes we may need to read a range of data from a particular sheet. To achieve this, we can use the range parameter. Say that we want to read data from rows 2 through 6 of the sheet named ‘Sheet1’:

import pandas as pd

df = pd.read_excel('example.xlsx', sheet_name='Sheet1', range='A2:C6')

The above code will read the data from columns A through C, rows 2 through 6 of the sheet named ‘Sheet1’ and store it in a DataFrame.

The range parameter can also be used to specify the range in terms of Excel cell references. For example, to read the range starting at cell B3 and ending at cell E8 of the sheet named ‘Sheet1’:

import pandas as pd

df = pd.read_excel('example.xlsx', sheet_name='Sheet1', range='B3:E8')

It is also possible to read multiple sheets at once by passing a list of sheet names or sheet indices as the sheet_name parameter.

import pandas as pd

df_dict = pd.read_excel('example.xlsx', sheet_name=['Sheet1', 'Sheet2'])

The above code reads the ‘Sheet1’ and ‘Sheet2’ sheets and returns a dictionary of DataFrames, where the keys are the sheet names and the values are the DataFrames.

Overall, specifying sheet names and data ranges is crucial when working with large Excel files with multiple sheets. Pandas makes it easy to read in only the data that we need using these parameters.

Data Cleaning and Transformation

Once we have loaded our data with Pandas’ read_excel function, we may need to perform some cleaning or transformation to make the data ready for analysis.

One of the most common tasks in data cleaning involves dealing with missing or null data. Pandas provides several methods to handle this, including the dropna method, which drops any row or column that contains null or missing data. To drop any rows with missing data, we can do the following:

df.dropna(inplace=True)

The above code will remove any row that has null or missing values, and modify the original DataFrame.

Another common task is the conversion of data types from one type to another. For instance, we may want to convert a column of strings representing dates into a datetime format. Here’s an example:

df['date_column'] = pd.to_datetime(df['date_column'])

This code will convert the column named ‘date_column’ from a string representation of a date to a datetime object.

There may also be cases where we need to transform our data before it is ready for analysis. Suppose we have a column with a numerical value that needs to be scaled to a smaller range. We can use Pandas’ apply method to apply a function element-wise to a column.

def scale_values(x):
    return x/100

df['numeric_column'] = df['numeric_column'].apply(scale_values)

This code will apply the function scale_values to the column named ‘numeric_column’, scaling each numerical value down by a factor of 100.

Sometimes we may need to compute statistics or aggregations on our data. Pandas provides several methods to help us accomplish this. For instance, we can compute the mean or median of a column using the mean and median methods, respectively.

avg = df['numeric_column'].mean()
med = df['numeric_column'].median()

The above code calculates the mean and median of the column named ‘numeric_column’.

Overall, data cleaning and transformation are essential steps in data analysis, especially when working with large datasets. Pandas provides several methods to help us manipulate, transform and clean our data.

Summary

Pandas is a powerful library for data manipulation and analysis, and it provides an easy way to read Excel files using the read_excel function. In this article, we have discussed how to read specific sheets and ranges of data from Excel files, as well as how to perform data cleaning and transformation on the loaded data. Understanding these techniques will help you prepare your data for analysis and make more informed decisions. Remember to practice with real datasets to gain expertise in utilizing these methods to your advantage.

Share:
Subscribe to our newsletter

Stay up to date with our latest content - No spam!

Related Posts