Pythonic Way to reverse nested dictionaries

后端 未结 5 1743
旧巷少年郎
旧巷少年郎 2020-12-14 11:46

I have a nested dictionary of people and item ratings, with people as the key. people may or may not share items. Example:

{
 \'Bob\' : {\'item1\':3, \'item2         


        
5条回答
  •  遥遥无期
    2020-12-14 12:15

    Pandas can provide another option. Assume data is the input dictionary.

    import pandas as pd
    output = {i:s.dropna().to_dict() for i, s in pd.DataFrame(data).T.iteritems()}
    

提交回复
热议问题