Python中文
首页
教程
问答
标签
搜索
登录
注册
pandas aggregate dataframe只返回一列
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>怎么了。在</p> <p>我有一个像这样的熊猫数据帧(df):</p> <pre><code> foo id1 bar id2 0 8.0 1 NULL 1 1 5.0 1 NULL 1 2 3.0 1 NULL 1 3 4.0 1 1 2 4 7.0 1 3 2 5 9.0 1 4 3 6 5.0 1 2 3 7 7.0 1 3 1 ... </code></pre> <p>我想按id1和id2分组,试着得到foo和bar的平均值。在</p> <p>我的代码:</p> ^{pr2}$ <p>我得到的几乎是我所期望的:</p> <pre><code> foo id1 id2 1 1 5.750000 2 7.000000 2 1 3.500000 2 1.500000 3 1 6.000000 2 5.333333 </code></pre> <p>“foo”列中的值正是我要查找的平均值(意味着),但我的列“bar”在哪里?在</p> <p>因此,如果是SQL,我要查找的结果如下: “<em>按id1,id2从数据帧组中选择avg(foo),avg(bar);</em>” (很抱歉,我更像一个sql人,对熊猫还不熟悉,但我现在需要它。)</p> <p>我也尝试过:</p> <pre><code>groupedFrame = res.groupby(["id1","id2"]) aggrFrame = groupedFrame.aggregate(numpy.mean) </code></pre> <p>这给了我完全相同的结果,仍然缺少“bar”列。在</p> <p>我阅读的网站:</p> <ul> <li><a href="http://wesmckinney.com/blog/groupby-fu-improvements-in-grouping-and-aggregating-data-in-pandas/" rel="nofollow noreferrer">http://wesmckinney.com/blog/groupby-fu-improvements-in-grouping-and-aggregating-data-in-pandas/</a></li> <li><a href="https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.aggregate.html" rel="nofollow noreferrer">https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.aggregate.html</a></li> <li>以及groupby的文档,但我不能在这里发布链接。在</li> </ul> <p>我做错什么了?-谢谢你。在</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>如前所述,您应该在取平均值之前替换空值</p> <pre><code>df.replace("NULL",-1).groupby(["id1","id2"])["foo","bar"].mean() </code></pre> <p>输出</p> ^{pr2}$
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何添加虚拟方法
3 回答
如何添加表示整数的擦边字符串?
3 回答
如何添加要在Bokeh中使用的新font.ttf文件?
1 回答
如何添加要显示的矩阵XY轴编号和XY轴
6 回答
如何添加计数?
1 回答
如何添加计数器函数?
3 回答
如何添加计数器列来计算数据帧中另一列中的特定值?
2 回答
如何添加计数器来跟踪while循环中的月份和年份?
7 回答
如何添加计数并删除countplot的顶部和右侧脊椎?
4 回答
如何添加计时器wx.应用程序更新窗口对象的主循环?
7 回答
如何添加评论到帖子?PostDetailVew,Django 2.1.5
2 回答
如何添加评论拉梅尔亚姆
10 回答
如何添加诸如矩阵Python/Pandas之类的数据帧?
1 回答
如何添加谷歌地点自动完成到Flask?
1 回答
如何添加超时、python discord bot
8 回答
如何添加超过1dp的检查
3 回答
如何添加距离方法
10 回答
如何添加跟随游戏的敌人精灵
6 回答
如何添加路径以便python可以找到程序?
9 回答
如何添加身份验证/安全性以使用happybase访问HBase?
8 回答