python logo

Read Excel with Pandas


Python hosting: Host, run, and code Python in the cloud!

Leveraging Pandas for Excel File Operations in Python
Excel files have always been a popular choice for data storage and analysis. With Python’s Pandas library, handling Excel files has never been easier. Dive in to learn how to efficiently read Excel files using Pandas.

Effortlessly Read Excel with Pandas
Pandas provides a rich set of methods to read, analyze, and manipulate Excel files with ease. By the end of this tutorial, you will be proficient in reading Excel columns, fetching specific data, and iterating through Excel data.

Accessing Excel Column Names with Pandas
To fetch column names from an Excel file, first, we need to load the data into a Pandas DataFrame. The method read_excel() is specifically designed for this purpose. Here’s how you can do it:

import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile

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

print("Column headings:")
print(df.columns)

Once the data is loaded into a DataFrame, accessing column names is as easy as calling df.columns.

Fetching Data from a Specific Excel Column
Once you have the data in a Pandas DataFrame, fetching data from a specific column becomes trivial. Here’s how you can fetch all data from the ‘Sepal width’ column:

print(df['Sepal width'])

Iterating Through Excel Data with Pandas
Sometimes, you might want to loop through the data. Here’s how you can iterate over the ‘Sepal width’ column:

for i in df.index:
print(df['Sepal width'][i])

Storing Excel Data in Python Lists
If you prefer working with lists, Pandas allows you to easily convert a column’s data to a list:

listSepalWidth = df['Sepal width']
print(listSepalWidth[0])

Extracting Multiple Columns from an Excel Sheet
You’re not limited to working with one column at a time. Here’s how you can fetch multiple columns:

import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile

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

sepalWidth = df['Sepal width']
sepalLength = df['Sepal length']
petalLength = df['Petal length']

Boost Your Python Data Analysis Skills
For those looking to further enhance their Python data analysis skills using Pandas, consider checking out the Data Analysis with Python Pandas course.

BackNext





Leave a Reply:




Seamus 2021-10-25T04:26:01.459Z

df = pd.read_excel('File.xlsx', sheetname='Sheet1')

should read
df = pd.read_excel('File.xlsx', sheet_name='Sheet1')

Frank 2021-10-25T04:27:01.459Z

Thanks, modified the code