How can pandas read xlsx data?

In pandas, you can use the read_excel() function to read xlsx data. Here is an example:

import pandas as pd

# 读取 xlsx 数据
data = pd.read_excel('data.xlsx', sheet_name='Sheet1')

# 打印数据
print(data)

The read_excel() function in the above code is used to read xlsx data, with the first parameter being the file path and the second parameter sheet_name specifying the name of the sheet to read (optional, defaulting to the first sheet). The data read is stored in a pandas DataFrame object, which can be printed using the print() function.

If you need to read multiple tables, you can use the parameter sheet_name=None, which will return a dictionary where each key corresponds to a table name and the value is the corresponding DataFrame object.

import pandas as pd

# 读取多个表格
data_dict = pd.read_excel('data.xlsx', sheet_name=None)

# 打印每个表格的数据
for sheet_name, df in data_dict.items():
    print(f"Sheet: {sheet_name}")
    print(df)
bannerAds