![](/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.