将字典列表转换为数据框

时间:2020-11-05 09:52:06

标签: python pandas dictionary

我有一个字典列表,看起来像:

markers_id.getSource().forEachFeature(function(feature){
  if (feature.get('title') == searchString) {
     // found feature
  }
});

我想将其转换为这样的数据框: 键应该作为行出现,而测试应该作为列出现。并且该测试没有其他测试中存在的键时,应填充为NAN

list_dict = [{'test1':{'a':1,'b':12,'c':40,'d':120,'e':20,'f':1,'g':2,'h':'2'}},
                  {'test2':{'a':5,'b':'10','c':20}},
                   {'test3':{'e':21,'f':'18','g':22,'h':20}}]

请帮助我。

2 个答案:

答案 0 :(得分:5)

将dict理解与平坦的嵌套dict一起使用,并传递给Dataframe构造函数:

df = pd.DataFrame({k: v for x in list_dict for k, v in x.items()})
print (df)
  test1 test2 test3
a     1     5   NaN
b    12    10   NaN
c    40    20   NaN
d   120   NaN   NaN
e    20   NaN    21
f     1   NaN    18
g     2   NaN    22
h     2   NaN    20

或者为每个嵌套字典创建DataFrame并传递到concat,如果大型词​​典和许多外键会像第一个解决方案那样较慢:

df = pd.concat([pd.DataFrame(x) for x in list_dict], axis=1)
print (df)
  test1 test2 test3
a     1     5   NaN
b    12    10   NaN
c    40    20   NaN
d   120   NaN   NaN
e    20   NaN    21
f     1   NaN    18
g     2   NaN    22
h     2   NaN    20

答案 1 :(得分:2)

在构建reduce之前先使用DataFrame

from functools import reduce
df = pd.DataFrame(reduce(lambda cum_dict, new_dict: dict(cum_dict, **new_dict), 
                         list_dict))

print (df)
  test1 test2 test3
a     1     5   NaN
b    12    10   NaN
c    40    20   NaN
d   120   NaN   NaN
e    20   NaN    21
f     1   NaN    18
g     2   NaN    22
h     2   NaN    20