[英]How to open an excel file with multiple sheets in pandas?
I have an excel file composed of several sheets.我有一个由几张纸组成的 excel 文件。 I need to load them as separate dataframes individually.
我需要将它们单独加载为单独的数据帧。 What would be a similar function as pd.read_csv("") for this kind of task?
对于这种任务,与 pd.read_csv("") 类似的 function 是什么?
PS due to the size I cannot copy and paste individual sheets in excel PS 由于尺寸原因,我无法在 excel 中复制和粘贴单张纸
Use pandas read_excel()
method that accepts a sheetname
parameter: 使用大熊猫
read_excel()
它接受一个方法sheetname
参数:
import pandas as pd
df = pd.read_excel(excel_file_path, sheetname="sheet_name")
For more in-depth explanation of how read_excel()
works see http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_excel.html 有关
read_excel()
工作原理的更深入说明,请参见http://pandas.pydata.org/pandas-docs/stable/generation/pandas.read_excel.html
import pandas
# setting sheet_name = None, reads all sheets into a dict
sheets = pandas.read_excel(filepath, sheet_name=None)
# i will be the keys in a dictionary object
# the values are the dataframes of each sheet
for i in sheets:
print(f"sheet[{i}]")
print(f"sheet[{i}].columns={sheets[i].columns}")
for index, row in sheets[i].iterrows():
print(f"index={index} row={row}")
exFile = ExcelFile(f) #load file f exFile = ExcelFile(f)#加载文件f
data = ExcelFile.parse(exFile) #this creates a dataframe out of the first sheet in file data = ExcelFile.parse(exFile)#这将从文件的第一张纸中创建一个数据框
If you can't type out each sheet name and want to read whole worksheet try this: 如果您无法输入每个工作表名称,但想阅读整个工作表,请尝试以下操作:
dfname=pd.ExcelFile('C://full_path.xlsx')
print(dfname.sheet_names)
df=pd.read_excel('C://fullpath.xlsx')
for items in dfname.sheet_names[1:]:
dfnew=pd.read_excel(full_path,sheet_name=items)
df=pd.concat([df,dfnew])
The thing is that pd.read_excel() can read the very first sheet and rest are unread.So you can use this 事实是pd.read_excel()可以读取第一张工作表,而其余的则未被读取,因此您可以使用它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.