pandas is a Python library. Pandas are mainly used when manipulating data. Pandas are essential for AI and machine learning. However, it also has a function to operate Excel files. This time, I will explain how to read an Excel file using pandas.
The contents of sample.xlsx If
main.py
import pandas as pd
df = pd.read_excel('sample.xlsx')
print(df)
When you run the above main.py And succeeded in reading the Excel file safely.
By the way, the second line of main.py
df = pd.read_excel('sample.xlsx', sheet_name =None)
Then you can get the data from all the sheets in sample.xlsx.
When sheet_name is set to None, all sheets are read. By specifying a number starting from 0 for sheet_name, the [specified number + 1] th sheet can be read.
df = pd.read_excel('sample.xlsx', sheet_name =0) #Read the first sheet
df = pd.read_excel('sample.xlsx', sheet_name =2) #Read the third sheet
By specifying a character string in sheet_name, you can read a sheet that has that character string in the sheet name.
df = pd.read_excel('sample.xlsx', sheet_name ='sheet1') #name is'sheet1'Sheet reading
I introduced how to read an Excel file using pandas. This feature is just the tip of the full iceberg of pandas.
It is a library that is used in a wide variety of situations, so if you are interested, please check out the detailed site.
Recommended Posts