I am trying to make a diagram tree in graphviz.Digraph, I am using Pandas dataframe. By the below query, I am getting the processid’s and their dependents id’s in a form of a dictionary
JavaScript
x
6
1
dependent = df.groupby('dependent_processid')['Processid'].apply(list).to_dict()
2
print(dependent)
3
4
#output:
5
{6720: [6721], 6721: [6722, 6724, 6725], 6725: [6723, 6726], 6753: [7177]}
6
But I want the data in below format:
JavaScript
1
5
1
6720-> {6721}
2
6721-> {6722, 6724, 6725}
3
6725-> {6723, 6726}
4
and so on .
5
Can someone please help me return pandas dataframe output in such format?
Advertisement
Answer
Are you wanting this:
JavaScript
1
7
1
def demo(k,v):
2
return f'{k} -> {v}'
3
4
dependent = df.groupby('dependent_processid')['Processid'].apply(set).to_dict()
5
for k, v in dependent.items():
6
print(demo(k,v))
7
Output:
JavaScript
1
5
1
6720-> {6721}
2
6721-> {6722, 6724, 6725}
3
6725-> {6723, 6726}
4
5