so I have the following dictionary:
user_dict = {'user1': {'id1': {('word1', 'word2'): 0.99, ('word3', 'word4'): 0.16},
'id2': {('word5', 'word6'): 0.73, ('word7', 'word8'): 0.69}},
'user2': {'id3': {('word9', 'word10'): 0.59, ('word11', 'word12'): 0.13},
'id4': {('word13', 'word14'): 0.41, ('word14', 'word15'): 0.74}}}
For my purpose I would like to convert the nested dictionary into a pandas dataframe of the form:
user | id | w1 | w2 | score
---------------------------------------
user1 | id1 | word1 | word2 | 0.99
| | word3 | word4 | 0.16
| id2 | word5 | word6 | 0.73 and so on.
I've tried a few ways before, and this is my current solution:
df = pd.Series({(i,j): user_dict[i][j]
for i in user_dict.keys()
for j in user_dict[i].keys()}).rename_axis(['user', 'id']).reset_index(name='Col3')
So the output is:
user | id | Col3
-------------------------------------------------------------------
user1 | id1 | {('word1', 'word2'): 0.99, ('word3', 'word4'): 0.16)}
user1 | id2 | {('word5', 'word6'): 0.73, ('word7', 'word8'): 0.69)} and so on.
Can someone tell me what I am doing wrong with the last columns?