簡體   English   中英

如何在 pandas 中打開一個 excel 文件?

[英]How to open an excel file with multiple sheets in pandas?

我有一個由幾張紙組成的 excel 文件。 我需要將它們單獨加載為單獨的數據幀。 對於這種任務,與 pd.read_csv("") 類似的 function 是什么?

PS 由於尺寸原因,我無法在 excel 中復制和粘貼單張紙

使用大熊貓read_excel()它接受一個方法sheetname參數:

import pandas as pd

df = pd.read_excel(excel_file_path, sheetname="sheet_name")

有關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)#加載文件f

data = ExcelFile.parse(exFile)#這將從文件的第一張紙中創建一個數據框

如果您無法輸入每個工作表名稱,但想閱讀整個工作表,請嘗試以下操作:

                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])

事實是pd.read_excel()可以讀取第一張工作表,而其余的則未被讀取,因此您可以使用它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM