簡體   English   中英

pandas:如果組的最后一行具有特定的列值,如何刪除組的所有行

[英]pandas: how to drop all rows of a group if the last row of the group has certain column value

我有一個df,如下所示

    a    c    d
0  ABC   0.4  y
1  ABC   0.3  x
2  DEF   0.3  x
3  DEF   0.2  x
4  DEF   0.5  x
5  DEF   0.4  y

我想按列'c'對df進行排序,然后按列'a'對df進行排序,然后如果組的最后一行的列'd'='y'的值,則刪除組的所有行

我預期的 output 是

    a    c    d
2  DEF   0.2  x
3  DEF   0.3  x
4  DEF   0.4  y
5  DEF   0.5  x

因此,在按 col 'c' 作為組 d = y 中的最后一行排序后,組 'ABC' 被刪除,但組 'DEF' 保留為 DEF col d = x 中的最后一行

直接從你的邏輯:

mask = (df.sort_values('c')     # sort the values by `c`
          .groupby('a')['d']    # groupby `a` and look at `d`
          .transform('last')    # select the last rows
          .ne('y')              # check if last rows are `y`
          .reindex(df.index)    # reindex as the original data
       )

df = df[mask]

Output:

     a    c  d
2  DEF  0.3  x
3  DEF  0.2  x
4  DEF  0.5  x
5  DEF  0.4  y

讓我們做filter

df=df.groupby('a').filter(lambda x : x.at[x['c'].idxmax(),'d']!='y')
Out[278]: 
     a    c  d
2  DEF  0.3  x
3  DEF  0.2  x
4  DEF  0.5  x
5  DEF  0.4  y

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM