创建一个新的DataFrame,将列dict中的每个键添加为标题

时间:2017-03-04 20:18:23

标签: python pandas dictionary dataframe multiple-columns

我有一个DataFrame,其中包含一个带字典的特定列。

我想在DataFrame中为包含dicts的列中的每个元素上找到的每个键添加一个新标头,如果该元素不包含,则分配给这些新单元格的每个新值应对应f = open('data.txt', 'r') data_txt = f.read() data_txt = data_txt.replace('[', '') data_txt = data_txt.replace(']', '') np.loadtxt(StringIO.StringIO(data_txt), delimiter = '\n') 那个标题密钥和相应的密钥值。

以下是测试和可视化我所说内容的数据:

导入依赖项:

None

创建包含内部字典列表的字典:

import pandas as pd
import numpy as np

为测试创建适当的初始DataFrame:

data = {'string_info': ['User1', 'User2', 'User3'],
        'dict_info': [{'elm1': 'attr5', 'elm2': 'attr9', 'elm3': 'attr33'},
                 {'elm5': 'attr31', 'elm7': 'attr13'},
                 {'elm5': 'attr28', 'elm1': 'attr23', 'elm2': 'attr33','elm6': 'attr33'}],
        'int_info': [4, 24, 31],}

手动说明我想要的输出:

df = pd.DataFrame.from_dict(data)
df

所需的输出是:

data2 = {'string_info': ['User1', 'User2', 'User3'],
        'elm1': ['attr5',None,'attr23'],
        'elm2': ['attr9',None,'attr33'],
        'elm3': ['attr33',None,None],
        'elm4': [None,None,None],
        'elm5': [None,'attr31',None],
        'elm6': [None,None,'attr33'],
        'elm7': [None,None,'attr13'],
        'int_info': [4, 24, 31]}

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以将concatDataFrame构造函数一起使用,以将dict替换为列:

print (pd.DataFrame(df.dict_info.values.tolist()))
     elm1    elm2    elm3    elm5    elm6    elm7
0   attr5   attr9  attr33     NaN     NaN     NaN
1     NaN     NaN     NaN  attr31     NaN  attr13
2  attr23  attr33     NaN  attr28  attr33     NaN

print (pd.concat([pd.DataFrame(df.dict_info.values.tolist()),
                  df[['int_info','string_info']]], axis=1))
     elm1    elm2    elm3    elm5    elm6    elm7  int_info string_info
0   attr5   attr9  attr33     NaN     NaN     NaN         4       User1
1     NaN     NaN     NaN  attr31     NaN  attr13        24       User2
2  attr23  attr33     NaN  attr28  attr33     NaN        31       User3

如果需要None添加replace

print (pd.concat([pd.DataFrame(df.dict_info.values.tolist()).replace({np.nan:None}), 
                  df[['int_info','string_info']]], axis=1))
     elm1    elm2    elm3    elm5    elm6    elm7  int_info string_info
0   attr5   attr9  attr33    None    None    None         4       User1
1    None    None    None  attr31    None  attr13        24       User2
2  attr23  attr33    None  attr28  attr33    None        31       User3