Excel into Python (pandas)


Do you have some data in Excel that you would like to import into Python? You can do a lot with the data in Excel, but you can do a lot with the data in pandas as well. There is some external documentation on pandas.read_excel. You can also export from a DataFrame to an Excel file (see below). pandas supports reading data from Excel 2003 and higher.

The best way to learn programming is by example. You use either the pandas.ExcelFile class or pandas.read_excel function.

# Mike - November 10, 2024
import pandas as pd
# read an Excel file into Python in Jupyter Notebook
df_orig = pd.read_excel('D:\MyData\Research\Datasets\Planets\planets1.xlsx','planets_sheet')
df = df_orig.copy()
df

The pandas function is read_excel(). The Excel file is called planets1.xlsx. The worksheet inside of the file is called planets_sheet.

DataFrame out to Excel

How would you write this back out to Excel? Use the to_excel() function of pandas.

df.to_excel("D:\MyData\Research\Datasets\Planets\planets1_backout.xlsx", sheet_name='planets_backout')  

Leave a Reply