有几个选项:
将所有工作表直接读入有序字典。
import pandas as pd
# for pandas version >= 0.21.0
sheet_to_df_map = pd.read_excel(file_name, sheet_name=None)
# for pandas version < 0.21.0
sheet_to_df_map = pd.read_excel(file_name, sheetname=None)
将第一张表直接读入数据框
df = pd.read_excel('excel_file_path.xls')
# this will read the first sheet into df
读取 excel 文件并获取工作表列表。然后选择并加载工作表。
xls = pd.ExcelFile('excel_file_path.xls')
# Now you can list all sheets in the file
xls.sheet_names
# ['house', 'house_extra', ...]
# to read just one sheet to dataframe:
df = pd.read_excel(file_name, sheet_name="house")
阅读所有工作表并将其存储在字典中。与第一个相同,但更明确。
# to read all sheets to a map
sheet_to_df_map = {}
for sheet_name in xls.sheet_names:
sheet_to_df_map[sheet_name] = xls.parse(sheet_name)
# you can also use sheet_index [0,1,2..] instead of sheet name.
感谢@ihightower 指出阅读所有表格的方法,感谢@toto_tico,@red-headphone 指出版本问题。
sheetname:字符串,整数,字符串/整数的混合列表,或无,默认为 0
自 0.21.0 版起已弃用:改用 sheet_name Source Link