I have this table :
JavaScript
x
9
1
Head Relation Tail
2
0 9 1 0
3
1 10 1 11
4
2 9 0 23
5
3 10 1 61
6
4 9 0 12
7
5 10 0 66
8
6 10 0 61
9
I have to create a dictionary with Head key and values equal to the relations but not repeated and for each value of the relations I have to insert the corresponding tail.
example:
JavaScript
1
2
1
{9 : {1: [10], 0:[23,12]}, 10 : {1:[11,61], 0:[66,61]}}
2
I don’t really know how to do it. Is there someone who can help me?
Second Example Input:
JavaScript
1
7
1
Head Relation Tail
2
0 207 1 269
3
1 207 1 61
4
2 207 0 62
5
3 208 1 269
6
4 290 0 269
7
the output:
JavaScript
1
3
1
{207: {0: [62], 1: [269,61]}, 208: {0: nan, 1: [269]},
2
290: {0: [269], 1: nan}}
3
I would like to remove the nans
Advertisement
Answer
You could use pivot_table
and to_dict
:
JavaScript
1
4
1
(df.pivot_table(index='Head', columns='Relation', values='Tail', aggfunc=list)
2
.to_dict('index')
3
)
4
Or, the other way around:
JavaScript
1
4
1
(df.pivot_table(index='Relation', columns='Head', values='Tail', aggfunc=list)
2
.to_dict()
3
)
4
output:
JavaScript
1
2
1
{9: {0: [23, 12], 1: [0]}, 10: {0: [66, 61], 1: [11, 61]}}
2
post-processing the output to remove NaNs:
JavaScript
1
7
1
d = (df.pivot_table(columns='Head', index='Relation', values='Tail', aggfunc=list)
2
.to_dict()
3
)
4
5
d2 = {k: {k2:v2 for k2,v2 in v.items() if pd.isna(v2) is not True}
6
for k,v in d.items()}
7