簡體   English   中英

將數百個帶有路徑引用的 excel 文件合並到一個 pandas 數據框中

[英]Merging hundreds excel file with path reference into one pandas dataframe

我可能有成百上千個帶括號的小 excel 文件到一個熊貓數據框中

這是我的參考表df

    Dataframe_name      Path                                 Sheet
45  finance_auditing    Finance - Accounting/TopSites-Fin... Aggregated_Data_for_Time_Period
46  finance_lending     Finance - Banking/TopSites-...          Aggregated_Data_for_Time_Period

我做了什么

finance_auditing  = pd.read_excel('Finance - Accounting/TopSites-Fin... ','Aggregated_Data_for_Time_Period')
finance_lending   = pd.read_excel('Finance - Banking/TopSites-... ','Aggregated_Data_for_Time_Period')
all = finance_auditing.append(finance_lending)

問題是我有數百個文件要讀取並且需要全部附加

您可以使用列表推導來遍歷路徑和工作表對並讀取相應的 excel 文件,最后concat所有 excel 文件:

pd.concat([pd.read_excel(path, sheet_name=sheet) 
           for path, sheet in zip(df.Path, df.Sheet)])

暫無
暫無

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

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