来自多索引groupby的字典

时间:2018-09-21 15:17:56

标签: python pandas

>>> df = pd.DataFrame({'a': [1,1,1,2,2,3,3,3,3,4,4,5,5], 'b': [0,1,1,0,1,0,0,1,1,1,0,1,0], 'v': [2,4,3,7,6,5,9,3,2,4,5,2,3]})
>>> df
    a  b  v
0   1  0  2
1   1  1  4
2   1  1  3
3   2  0  7
4   2  1  6
5   3  0  5
6   3  0  9
7   3  1  3
8   3  1  2
9   4  1  4
10  4  0  5
11  5  1  2
12  5  0  3
>>> df.groupby(by =['a', 'b']).groups
{(2, 0): [3], (5, 0): [12], (3, 0): [5, 6], (5, 1): [11], (1, 0): [0], (3, 1): [7, 8], (4, 1): [9], (1, 1): [1, 2], (2, 1): [4], (4, 0): [10]}

q1。如何获得一个字典,而不是每个组的索引将返回每个组的column v的值?

q2。有没有办法得到这样的嵌套词典?

{1: {0:[2], 1: [4,3]}, 2: {0: [7], 1: [6]} ... } 

1 个答案:

答案 0 :(得分:4)

您应该使用to_dict

df.groupby(by =['a', 'b']).v.apply(list).unstack().to_dict('index')
Out[1042]: 
{1: {0: [2], 1: [4, 3]},
 2: {0: [7], 1: [6]},
 3: {0: [5, 9], 1: [3, 2]},
 4: {0: [5], 1: [4]},
 5: {0: [3], 1: [2]}}