简体   繁体   English

使用 Python 从字典中提取多个数据帧

[英]Extract multiple dataframes from dictionary with Python

I'm using the pandas library in Python.我在 Python 中使用 pandas 库。

I've taken an excel file and stored the contents in a data frame by doing the following:我采用了 excel 文件,并通过执行以下操作将内容存储在数据框中:

path = r"filepath"
sheets_dict = pd.read_excel(path,sheet_name=None)

As there was multiple sheets, each containing a table of data with identical columns, I used pd.read_excel(path,sheet_name=None).由于有多个工作表,每个工作表都包含一个具有相同列的数据表,因此我使用了 pd.read_excel(path,sheet_name=None)。 This stored all the individual sheets into a dictionary with the key for each value/sheet being the sheet name.这将所有单独的工作表存储到字典中,每个值/工作表的键是工作表名称。

I now what to unpack the dictionary and place each sheet into a single data frame.我现在要解压字典并将每张表放入单个数据框中。 I want to use the key of each sheet in the dictionary as either part of a mulitindex so I know what key/sheet of each table came from or appended as a new column which gives me the key/sheet name for each unique subset of the dataframe.我想将字典中每个工作表的键用作 mulitindex 的任何一部分,所以我知道每个表的键/表来自或附加为新列,这为我提供了每个唯一子集的键/表名称dataframe。

I've tried the following:我尝试了以下方法:

for k,df in sheets_dict.items():
    df = pd.concat([pd.DataFrame(df)])
    df['extract'] = k

However I'm not getting the results I want.但是我没有得到我想要的结果。

Any suggestions?有什么建议么?

you can use the keys argument in pd.concat which will set the keys of your dict as the index.您可以在pd.concat中使用keys参数,它将您的 dict 的键设置为索引。

df = pd.concat(sheets_dict.values(),keys=sheets_dict.keys())

by default, pd.concat(sheet_dict) will set the indices as the keys.默认情况下, pd.concat(sheet_dict)会将索引设置为键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM