简体   繁体   English

如何将数据转换为 python、dataframe 中的嵌套字典

[英]how to convert data into nested dictionary in python,dataframe

I have a large dataset of item code and component each item code correlate with component and further component become item code of another component.我有一个项目代码和组件的大型数据集,每个项目代码都与组件相关,进一步的组件成为另一个组件的项目代码。 how can I make a nested dictionary in python如何在python中制作嵌套dictionary

item code   component
a             q
b             w
c             r
d             t
e             y
q             u
q             v

desired output:-所需的 output:-

{a:{q:[u,v]},b:w,c:r etc}

How can I achieve this nested dictionary in python , I have large data I used defaultdict but it gave me only a dictionary not a nested dictionary如何在python中实现这个嵌套dictionary ,我有大数据我使用defaultdict但它只给了我一个dictionary而不是嵌套dictionary

In [108]: df = pd.DataFrame({'item_code': list('abcdeqq'), 'component': list('qwrtyuv')})

In [109]: import networkx as nx

In [110]: g = nx.DiGraph([(k,v) for k,v in zip(df['item_code'], df['component'])])

In [111]: {k:v if len(v) > 1 else v[0] for k,v in nx.convert.to_dict_of_lists(g).items() if v}
Out[111]: {'a': 'q', 'q': ['u', 'v'], 'b': 'w', 'c': 'r', 'd': 't', 'e': 'y'}

Using networkx you can get something like this.使用 networkx 你可以得到这样的东西。 Based on this answer I am able to reach to this solution:基于这个答案,我能够达到这个解决方案:

import networkx
G = nx.DiGraph()
G.add_edges_from(df.values)

def comb_tup(li_tup):
    d = {}
    crnt = d  # memo the crnt subtree
    stck = []  # stack of (sub)trees along current path
    for k, v in li_tup:
        while stck and k not in crnt:
            crnt = stck.pop()
        if k not in crnt:
            crnt[k] = {}
        stck.append(crnt)
        crnt = crnt[k]
        crnt[v] = {}
    return d
    
final_di = {}
for node in G.nodes:
    vi = list(nx.dfs_edges(G,node))
    d = comb_tup(vi)
    if len(d.keys()):
        for k,v in d.items():
            final_di[k] = v

final_di: final_di:

{'a': {'q': {'u': {}, 'v': {}}},
 'q': {'u': {}, 'v': {}},
 'b': {'w': {}},
 'c': {'r': {}},
 'd': {'t': {}},
 'e': {'y': {}}}

If you have this data:如果你有这些数据:

   item_code    component
0   a           q
1   b           w
2   c           r
3   d           t
4   e           y
5   q           u
6   q           v
7   u           x

final_di: final_di:

{'a': {'q': {'u': {'x': {}}, 'v': {}}},
 'q': {'u': {'x': {}}, 'v': {}},
 'b': {'w': {}},
 'c': {'r': {}},
 'd': {'t': {}},
 'e': {'y': {}},
 'u': {'x': {}}}

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

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