user2896120:
私はこのようなデータフレームそのルックスを持っています:
Friend A Friend B Score
0 Walter John 22
1 Walter Jack 32
2 Fred John 10
3 Caleb Jack 5
私がプロデュースしたいことはこれです:
{"Walter": {"John": 22, "Jack": 32}, "Fred": {"John": 10}, "Caleb": {"Jack": 5}}
私はこれまで持っていることはこれです:
x = Network.groupby("Friend A")[["Friend A", "Friend B", "Score"]]
私は次のことをやってみました:
x.apply(list).to_dict()
しかし、これは私が取得したいのですが、結果ではありません。どのように私はこの結果を達成することができますか?
ランディ:
In [17]: df.groupby("Friend A").apply(lambda x: x.set_index("Friend B")['Score'].to_dict()).to_dict()
Out[17]:
{'Caleb': {'Jack': 5},
'Fred': {'John': 10},
'Walter': {'Jack': 32, 'John': 22}}