Hou*_*und 1 python dictionary pivot-table pandas
我有这个数据透视表:
[in]:unit_d
[out]:
units
store_nbr item_nbr
1 9 27396
28 4893
40 254
47 2409
51 925
89 157
93 1103
99 492
2 5 55104
11 655
44 117125
85 106
93 653
Run Code Online (Sandbox Code Playgroud)
我想要一个字典,其中'store_nbr'作为键,'item_nbr'作为值.
所以,{'1': [9, 28, 40,...,99], '2': [5, 11 ,44, 85, 93], ...}
我在groupby这里使用,重置索引后使其成为列:
>>> d = unit_d.reset_index()
>>> {k: v.tolist() for k, v in d.groupby("store_nbr")["item_nbr"]}
{1: [9, 28, 40, 47, 51, 89, 93, 99], 2: [5, 11, 44, 85, 93]}
Run Code Online (Sandbox Code Playgroud)