[英]How to convert a column in a dataframe to a nested dictionary in python?
[英]how to convert data into nested dictionary in python,dataframe
我有一個項目代碼和組件的大型數據集,每個項目代碼都與組件相關,進一步的組件成為另一個組件的項目代碼。 如何在python
中制作嵌套dictionary
item code component
a q
b w
c r
d t
e y
q u
q v
所需的 output:-
{a:{q:[u,v]},b:w,c:r etc}
如何在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'}
使用 networkx 你可以得到這樣的東西。 基於這個答案,我能夠達到這個解決方案:
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:
{'a': {'q': {'u': {}, 'v': {}}},
'q': {'u': {}, 'v': {}},
'b': {'w': {}},
'c': {'r': {}},
'd': {'t': {}},
'e': {'y': {}}}
如果你有這些數據:
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:
{'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.