![](/img/trans.png)
[英]pandas: how to drop all rows of a group if the last row of the group has certain column value
[英]Identify the row in a group that has the highest value and extract all rows in that share its name in Pandas
我有一个csv文件,其数据排列如下:
Letter Subject Score
0 A Knitting 78
1 A Knitting 60
2 A Knitting 42
3 B Knitting 30
4 B Knitting 48
5 C Diving 23
6 C Diving 56
7 C Diving 23
8 D Diving 78
......
将CSV读入Pandas后,我想做的事情如下:
我可以做到1和2,但是我和3和4并不能缠在一起。 有人对熊猫有经验吗?
这是一种方法:
df[~df['Letter'].isin(df.set_index('Letter').groupby('Subject')['Score'].idxmax())]
Out:
Letter Subject Score
3 B Knitting 30
4 B Knitting 48
5 C Diving 23
6 C Diving 56
7 C Diving 23
df.set_index('Letter').groupby('Subject')['Score'].idxmax()
返回每个主题组得分最高的字母。 我需要将索引临时设置为字母,以获取字母而不是索引。 输出为:
df.set_index('Letter').groupby('Subject')['Score'].idxmax()
Out:
Subject
Diving D
Knitting A
Name: Score, dtype: object
现在,使用布尔索引,您可以使用Series.isin
选择其对应字母不在列表['D', 'A']
的Series.isin
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.