简体   繁体   English

将字典值转换为数据框

[英]Convert dictionary values into a dataframe

This could be answered somewhere but I can't seem to work any of the solutions with what I have...这可以在某处得到回答,但我似乎无法使用我拥有的任何解决方案...

I have a following dictionary data_dict:我有以下字典 data_dict:

data_dict:
{'df_0':
     Name    Role       Location
0    Gina    Assistance    NY
1    Jake    Officer       Brooklyn
2    Boyle   Detective     99
3    Scully  Assistance    NY
4    Diaz    Officer       Brooklyn
 'df_1':     
     Name    Role       Location          
5    Hitchcock Detective     99
6    Amy    Assistance    NY
7    Terry    Officer       Brooklyn
 'df_2':  
     Name    Role       Location
7    Terry    Officer       Brooklyn
8    Holt   Detective     99
9    Judy   Assistance    NY
10   Adrian Officer       Brooklyn
}

In the dictionary above df_0, df_1, df_2 are the keys for data_dict and each of the keys is a dataframe that contains columns Name, Role, Locationdf_0上面的字典中,df_1、df_2data_dict的键,每个键都是一个包含名称、角色、位置列的数据

The output I am seeking is a master_df that combines values from data_dict keys and outputs the following results:我正在寻找的输出是一个master_df ,它结合了 data_dict 键的值并输出以下结果:

master_df:
     Name    Role       Location
0    Gina    Assistance    NY
1    Jake    Officer       Brooklyn
2    Boyle   Detective     99
3    Scully  Assistance    NY
4    Diaz    Officer       Brooklyn
5    Hitchcock Detective     99
6    Amy    Assistance    NY
7    Terry    Officer       Brooklyn
8    Holt   Detective     99
9    Judy   Assistance    NY
10   Adrian Officer       Brooklyn

Any help / guidance is appreciated!!任何帮助/指导表示赞赏!

You can try using concat function, something like this:您可以尝试使用concat函数,如下所示:

import pandas as pd
master = pd.concat (data_dict.keys())

You an concat and then drop_duplicates() :你一个concat然后drop_duplicates()

master_df = pd.concat([df_0, df_1, df_2]).drop_duplicates()

EDIT: Reading more closely... you are doing this from a dictionary... so try dejdej's answer.编辑:更仔细地阅读......你是从字典中做这件事......所以试试dejdej的答案。 You may have to drop duplicates, but I didn't test.您可能必须删除重复项,但我没有测试。

Thanks all!谢谢大家!

For my purpose I found this solution to output what I needed:为了我的目的,我找到了这个解决方案来输出我需要的东西:

output = pd.DataFrame()
for k in data_dict:
    output = output.append(data_dict[k], ignore_index=True)

Thank you everyone for your time and suggestions!感谢大家的时间和建议!

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

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