嵌套字典到pandas DataFrame

时间:2018-03-05 16:30:24

标签: python pandas dictionary dataframe

我的数据如下:

None

内部数组的长度始终相同。 key1,key2,key3也始终相同。

我想将其转换为pandas DataFrame,其中outer_key1,outer_key2,...是索引,key1,key2,key3是列。

编辑:

数据存在问题,我认为这是给定解决方案不起作用的原因。在少数情况下,在内部数组中有三个outer_key3: [ None, None, None ]而不是三个字典。像这样:

import pandas as pd import numpy as np from itertools import combinations import networkx as nx df = pd.DataFrame({'node_1': [1,0,1,1,0], 'node_2':[0,1,0,1,1], 'node_3':[1,1,0,1,1]}) # Array dimension l = len(df.columns) # empty matrice mat = np.zeros((l,l)) for i, row in df.iterrows(): positions = np.where(row)[0] if len(positions)>1: for comb in combinations(positions,2): i,j = comb mat[i,j] += 1 mat[j,i] += 1 mat

2 个答案:

答案 0 :(得分:2)

以这种方式:

d = { 'O1' : [ {'K1': 1},
               {'K2': 2},
               {'K3': 3} ],
      'O2' : [ {'K1': 4},
               {'K2': 5},
               {'K3': 6} ] }

d = {k: { k: v for d in L for k, v in d.items() } for k, L in d.items()}

df = pd.DataFrame.from_dict(d, orient='index')

#     K1  K2  K3
# O1   1   2   3
# O2   4   5   6

替代解决方案:

df = pd.DataFrame(d).T

None数据的更繁琐的方法:

d = { 'O1' : [ {'K1': 1},
               {'K2': 2},
               {'K3': 3} ],
      'O2' : [ {'K1': 4},
               {'K2': 5},
               {'K3': 6} ],
      'O3' : [ {'K1': None},
               {'K2': None},
               {'K3': None} ] }

d = {k: v if isinstance(v[0], dict) else [{k: None} for k in ('K1', 'K2','K3')] for k, v in d.items()}
d = {k: { k: v for d in L for k, v in d.items() } for k, L in d.items()}

df = pd.DataFrame.from_dict(d, orient='index')

#      K1   K2   K3
# O1  1.0  2.0  3.0
# O2  4.0  5.0  6.0
# O3  NaN  NaN  NaN

答案 1 :(得分:1)

来自Jpp的数据

pd.Series(d).apply(lambda x  : pd.Series({ k: v for y in x for k, v in y.items() }))
Out[1166]: 
    K1  K2  K3
O1   1   2   3
O2   4   5   6

更新

pd.Series(d).apply(lambda x  : pd.Series({ k: v for y in x for k, v in y.items() }))
Out[1179]: 
     K1   K2   K3
O1  1.0  2.0  3.0
O2  4.0  5.0  6.0
O3  NaN  NaN  NaN