Python中文
首页
教程
问答
标签
搜索
登录
注册
使用Pandas Dataframe创建字典
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我有一个称为“df”的数据帧,如下所示</p> <p><a href="https://i.stack.imgur.com/toGq6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/toGq6.png" alt="enter image description here"/></a></p> <p>我的目标是编一本字典。对于dictionary,键是poid,值是lng和lat</p> <p>示例字典输出</p> <pre><code>{0: (30.2359091167, -97.7951395833), 1: (30.2691029532, -97.7493953705), 2: (30.2557309927, -97.7633857727), 3: (30.2634181234, -97.7575966669), 4: (30.2742918584, -97.7405226231), 5: (30.261599404, -97.7585805953), ... </code></pre> <p>我的解决方案</p> <pre><code>dictionary = df.groupby('poiid')['lng'].agg(set).to_dict() </code></pre> <p><strong>我的解决方案有效,但我无法将“lat”列添加到字典中。</strong></p> <p>我愿意看到其他解决办法</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>首先将索引设置为<code>poiid</code>,这样当您想要创建字典时,键将是<code>poiid</code>。然后获取所需的列并使它们<code>tuple</code>。在最后一步中,您将使用它创建一个字典</p> <pre><code>df = df.set_index('poiid')[['lng', 'lat']].apply(tuple, axis=1).to_dict() </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
当用户用PYTHON设置一个或一个不带值的URL时,他们怎么能输入一个/a的代码呢?
1 回答
当用户登录到站点时,如何显示不同的导航栏
8 回答
当用户登录时,在Flask中向用户显示处理结果
7 回答
当用户的Flask会话结束时,我如何从Redis后端中移除所有Celery结果?
4 回答
当用户的Okta配置文件字段当前为blan时,更新该字段
10 回答
当用户的付款逾期2天时,从Django模型检索数据
7 回答
当用户的消息以问号结尾时,如何让机器人说些什么?
8 回答
当用户的系统上可能也安装了Python 2.7时,如何在用户的系统上运行Python 3脚本?
2 回答
当用户确定打印数量时,使用Matplotlib打印动画
7 回答
当用户离开时是否可以删除整个网页?
5 回答
当用户给出一个单词时如何打印?
4 回答
当用户继续更改TKin中的值(使用trace方法)时,使用Entry并更新输入的条目
2 回答
当用户编辑表单字段时,从Django时间字段中删除秒数
1 回答
当用户被更改时,消息不会来自web套接字
8 回答
当用户访问表单时,如何使表单为只读,而不具有更改权限
4 回答
当用户试图更改对象的值时,使用描述符类引发RuntimeError
2 回答
当用户调整GUI的大小时,是否有方法更改GUI内容的大小?
4 回答
当用户调整风的大小时,pythontkinter小部件的大小会不均匀
9 回答
当用户购买某个类别时,是否查找其他类别的销售?
10 回答
当用户转到上一页时,Django和芹菜插入操作
7 回答