Python中文
首页
教程
问答
标签
搜索
登录
注册
查询Pandas datafram
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我有以下数据:</p> <pre><code>Id | PrimaryName | SecondaryName | Value ---+-------------+---------------+------- 0 | PN0 | SN0 | 3 1 | PN0 | SN1 | 5 2 | PN0 | SN2 | 6 3 | PN1 | SN3 | 5 4 | PN1 | SN4 | 6 5 | PN1 | SN5 | 7 6 | PN2 | SN6 | 1 7 | PN2 | SN7 | 2 8 | PN2 | SN8 | 3 </code></pre> <p>实际上,它类似于一个键-值对,其中<code>SecondaryName</code>作为键,<code>Value</code>作为,hm,value,带有附加列<code>PrimaryName</code>。 我的任务是,通过只查看每个<code>PrimaryName</code>的最大值的两个条目,来确定两个最大的<code>PrimaryName</code></p> <p>例如,对于<code>PN0</code>,两个最大值是5和6,对于<code>PN1</code>是6和7,对于{<cd8>}是2和3。这意味着最大的<code>PrimaryName</code>是<code>PN0</code>,得分为11,而{<cd7>}的得分为13。<br/> <strong>理想的结果只是<code>PrimaryName</code>s=>;<code>['PN1', 'PN0']</code></strong></p> <p>像一个很容易回答问题的人:</p> ^{pr2}$ <p>但现在我不得不用Python,特别是pandas来重复。在</p> <p>到目前为止,我只想到了一个问题的以下部分:</p> <pre><code>df.groupby('PrimaryName')[['PrimaryName', 'Value']] </code></pre> <p>我怀疑,要像我在这条C#行<code>g.OrderByDescending(e => e.Value).Take(2).Sum(e => e.Value)</code>中所做的那样执行计算,我必须用一个临时列定义一个新的dataframe,但我不确定具体如何定义。在</p> <p>有人能帮我吗?在</p> <hr/> <p>实际上这个问题还有一点。这项任务是熊猫课程的一部分,而且,从<code>groupby</code>是下周的主题这一事实来看,我可能走错了路,或者至少我错过了一些简单明了的东西。在</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>你可以的</p> <pre><code>In [5181]: (df.groupby(['PrimaryName'])['Value'] .nlargest(2) .sum(level=0) .nlargest(2) .index.tolist()) Out[5181]: ['PN1', 'PN0'] </code></pre> <hr/> <p>细节</p> ^{pr2}$
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何将Python中的列表复制到给定的目标中?
1 回答
如何将python中的列表插入SQL表
10 回答
如何将python中的列表转换为numpy数组以放入十位
3 回答
如何将python中的列表输入javascript?
6 回答
如何将python中的列表返回给dag?
1 回答
如何将Python中的列表项重新排列成成对的元组/列表?
1 回答
如何将Python中的初始化对象序列化为XML?
1 回答
如何将python中的十进制字符串转换为数字?
6 回答
如何将Python中的原始输入文本转换为Tkinter中的标签?
3 回答
如何将python中的反斜杠命令转换为在Linux上运行
5 回答
如何将python中的命令行参数转换为字典?
5 回答
如何将python中的图像值传递到kivy中的kv文件?
7 回答
如何将Python中的图像数组(枕头对象)上传到Google云
9 回答
如何将Python中的图像编码为Base64?
5 回答
如何将python中的图像调整为灰度低分辨率,如MNIST时尚数据?
10 回答
如何将python中的多个html输出保存到单个文件(或多个)中?
5 回答
如何将Python中的多个ifelse语句重构为一个函数?
8 回答
如何将Python中的多处理与Django结合使用,从xml文件创建数千个模型实例?
7 回答
如何将python中的多级API响应转换为dataframe
2 回答
如何将python中的多线程编程模型转换为异步/等待模型?
10 回答