python - 数据框中的冗余值用作键,另一列用作值
问题描述
我有一个像
name count title
a 1 c1
a 2 c2
a 3 b1
b 4 f1
b 5 f2
c 6 g1
我想将它转移到一个name
作为键和title
作为值的字典。
我希望输出像
{a: [c1, c2, b1], b: [f1, f2], c: [g1]}
我试过这个:
sites = {}
features = []
for index,row in df.iterrows():
sites[row["name"]] = features.append(row["title"])
并得到了一个字典,其None
值为
{a: None, b: None, c: None}
为什么我会得到None
,我怎样才能正确地做到这一点?
我也试过这个:
your_dict = df.groupby(["Name"])["title"].apply(list).to_dict().get("Name")
也获得了None
价值。
解决方案
your_dict = df.groupby(["name"])["title"].apply(list).to_dict()
推荐阅读
- javascript - 是否可以为多实例 tinyMCE 的每个实例定义一个强制根块?
- uibutton - 您可以在关闭之外更改 IBAction 按钮的状态吗?- 斯威夫特 4
- php - 基于 OpenWRT 网络的 MP3 播放器
- node.js - 我想使用 NodeJS 中的 redis 库在 Redis 中的 hset 特定键中设置 Expiry
- python - 使用 .GET[] 时 /count/ 处的 MultiValueDictKeyError
- php - 上传时在 CodeIgniter 中调整图像大小并将调整后的图像保存在目录中
- javascript - 使用节点 js 和 socket.io 在 mysql 数据库中插入新记录时如何通知和更新客户端
- flutter - 在颤振应用程序中实现地点选择器对话框
- yii2 - 和以前一样,即使对于经过身份验证的用户,请求也总是重定向到登录页面
- mysql - 如何选择包含多个标签的 id?