我有一個有一百多頁的Excel檔案。 每張表都有相同的資訊列。 我已經將所有的作業表串聯到一個檔案中。
腳本
single_file = pd.concat(pd.read_excel('multiple_sheets. xlsx',sheet_name=None),ignore_index=True)
single_file.to_csv('single_file.csv')
該腳本作業正常。 問題是我想添加一列,用其原始作業表的名稱來識別每一行的資訊。 這可能嗎?
提前感謝。 Fernando
uj5u.com熱心網友回復:
你要把pd.concat保存在腳本的最后。
你已經用sheetname = None讀入了檔案,它創建了一個以作業表名稱為鍵的字典,并以資料框架為值。 在每個作業表上回圈,首先添加你的標簽列,然后進行連接。
uj5u.com熱心網友回復:
single_file = pd.read_excel('multiple_sheets.xlsx', sheet_name=None)
single_file = pd.concat([sheet.assign(identifier=i) for i,sheet in single_file.items()] )
在這里,我們使用了這樣的想法:所有的作業表都可以通過在資料幀的字典上迭代(使用items())來訪問。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/307533.html
標籤:
