在熊猫中创建一个带有多个列的嵌套词典
我正在尝试以这种方式对数据进行分组 - {10:{10:[POL],5:[CARL]}
现在,我已经根据年龄和数据列对数据进行了分组。现在,我也试图将评分包括在其中。因此{年龄:{评分:[数据],评分:[data]}
这是我现在分组的方式,
df.groupby(“ age”)[“ data”]。agg(list).to_dict()
[table )链接] https://i.sstatic.net/pc8cw.png
I'm trying to group the data in this way - {10:{10:[Pole], 5:[Carl]}
Right now, I have grouped data based on age and data column. Now I'm trying to include rating in it as well. So {Age:{Rating:[Data], Rating:[Data]}
This is how I'm grouping now,
df.groupby("Age")["Data"].agg(list).to_dict()
[Table Link] https://i.sstatic.net/PC8Cw.png
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
分组可以由Groupby完成,但是构建字典只能通过自定义功能完成。 create_dict用于基于分组键动态创建嵌套词典,其中最内在的元素设置了值
Grouping can be done by groupby, but building dictionaries can only be done by custom functions. create_dict is used to dynamically create nested dictionaries based on grouping keys, with the innermost element setting the value
尝试以下操作:
输出:
对于不同的输入:
结果:
我不确定这就是您要寻找的。可能删除:)
try this:
output:
for different input:
result:
I'm, not sure this is what you're looking for. might delete :)