[英]Convert Dataframe to Nested Dictionary in Python
I'm looking for a way to convert a dataframe into a dictionary, very similar to what has been asked here:我正在寻找一种将 dataframe 转换为字典的方法,这与此处的要求非常相似:
Convert pandas DataFrame to a nested dict 将 pandas DataFrame 转换为嵌套字典
Assuming a sample data frame假设一个样本数据框
name v1 v2 v3
0 A A1 A11 1
1 A A2 A12 2
2 B B1 B12 3
3 C C1 C11 4
4 A A2 A21 6
5 A A2 A21 8
The number of columns may differ and so does the column names.列数可能不同,列名也可能不同。
I'm looking to generate:我正在寻找生成:
{
'A' : {
'A1' : { 'A11' : 1 },
'A2' : { 'A12' : 2 , 'A21' : 6 , 'A21' : 8 },
'B1' : {},
'C1' : {}
},
'B' : {
'A1' : {},
'A2' : {},
'B1' : { 'B12' : 3},
'C1' : {}
},
'C' : {
'A1' : {},
'A2' : {},
'B1' : {} ,
'C1' : { 'C11' : 4}
}
}
The method suggested elsewhere is via recursion:其他地方建议的方法是通过递归:
def recur_dictify(frame):
if len(frame.columns) == 1:
if frame.values.size == 1: return frame.values[0][0]
return frame.values.squeeze()
grouped = frame.groupby(frame.columns[0])
d = {k: recur_dictify(g.ix[:,1:]) for k,g in grouped}
return d
Which gives:这使:
>>> pprint.pprint(recur_dictify(df))
{'A': {'A1': {'A11': 1}, 'A2': {'A12': 2, 'A21': [6,8]}},
'B': {'B1': {'B12': 3}},
'C': {'C1': {'C11': 4}}}
But doesn't replicate the empty dict nest at level v2, and groups the repetition of A2 -A21 into array[6,8].但不会复制 v2 级别的空 dict 嵌套,并将 A2 -A21 的重复分组到数组 [6,8] 中。 I've looked at Convert a Pandas DataFrame to a dictionary , no luck so far.我看过Convert a Pandas DataFrame to a dictionary ,到目前为止还没有运气。
I assume that:我假设:
and df contains the output of your recur_dictify above:并且 df 包含上面 recur_dictify 的 output :
ky = frame.v1.unique() # I assume it's ['A1','B1','C1']
for k in df:
for l in ky:
if l not in df[k]:
df[k][l] = {}
You original dataframe is strange though.你原来的 dataframe 很奇怪。 The B2 entry does not appear anywhere in your result. B2 条目不会出现在结果中的任何位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.