Mar*_*ary 1 python dictionary pandas
我正在尝试使用以下数据集创建字典:
id value
1 a
1 b
1 c
2 e
2 f
2 g
3 h
3 g
3 l
Run Code Online (Sandbox Code Playgroud)
输出应该是这样的
{1: [a,b,c], 2:[e, f, g], 3: [h, g, l]}
Run Code Online (Sandbox Code Playgroud)
我知道有关如何创建字典的一些参考,但它们都没有提供这样的输出.
谢谢.
Groupby和表单列表然后创建一个字典
df.groupby('id')['value'].apply(list).to_dict()
# {1: ['a', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'g', 'l']}
Run Code Online (Sandbox Code Playgroud)