简体   繁体   English

如何在 pandas 中打开一个 excel 文件?

[英]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.

相关问题 如何通过pandas将多个工作表中的列合并到一个excel文件中 - How to merge columns from multiple sheets in one excel file by pandas 使用 Python/Pandas 创建具有多张工作表的 Excel 文件 - Create an Excel file with multiple sheets with Python/Pandas 如何使用多处理将多个Excel工作表导入到pandas中? - How to import multiple Excel sheets into pandas with multiprocessing? 如何从 python 读取多张 excel 文件? 我收到错误消息说“pandas”没有属性“excel” - How to read excel file with multiple sheets from python? I got error saying 'pandas' has no attribute 'excel' 如何使用pandas read_excel多处理多个excel表? - How to multiprocess multiple excel sheets using pandas read_excel? 在pandas中使用不同的工作表名称读取多个excel文件 - Read multiple excel file with different sheets names in pandas 将多张 excel 文件转换为 pandas 中的单个 JSON 格式 - Convert excel file with Multiple sheets to single JSON format in pandas 如何按 pandas dataframe 列分组并将不同的组保存到同一个 excel 文件中的多个工作表? - How to groupby a pandas dataframe column and save different group to multiple sheets in same excel file? 如何将熊猫read_excel()用于多张Excel文件? - How to use pandas read_excel() for excel file with multi sheets? 如何快速读取多个excel文件,每个文件中都有多个工作表-熊猫? - How to quickly read in multiple excel files with multiple sheets in each - Pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM