简体   繁体   English

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

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

i have a df as shown below我有一个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

i would like to sort df by column 'c', then groupby column 'a' and then drop ALL rows of the group if the value of column 'd'= 'y' for the last row of the group我想按列'c'对df进行排序,然后按列'a'对df进行排序,然后如果组的最后一行的列'd'='y'的值,则删除组的所有行

my expected output is我预期的 output 是

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

so group 'ABC' got deleted as after sorting by col 'c' as last row in group d = y but group 'DEF' stays as last row in DEF col d = x因此,在按 col 'c' 作为组 d = y 中的最后一行排序后,组 'ABC' 被删除,但组 'DEF' 保留为 DEF col d = x 中的最后一行

Straight from your logic:直接从你的逻辑:

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: Output:

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

Let us do filter让我们做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.

相关问题 如何根据行中的特定值和熊猫中的另一列对行进行分组? - How to group rows based on specific value in a row and another column in pandas? 如果组内的一行满足 pandas 中的特定条件,如何 select 组的所有行 - How to select all rows of group if one row within group meets certain condition in pandas 如果 pandas dataframe 中的一列缺少数据,则删除一组行 - Drop a group of rows if one column has missing data in a pandas dataframe Pandas 数据框获取掩码列的零(0)之间的所有行,并获取每组的第一行和最后一行 - Pandas dataframe get all rows between zero(0) of mask column and get first and last row of each group pandas python 删除组的最后一行 - pandas python Drop last row of group 使用 Pandas,如何删除每组的最后一行? - Using Pandas, how do I drop the last row of each group? 如何按列分组并将组中的所有值复制到pandas中的一行? - How to group by column and copy all values of a group to one row in pandas? 确定组中具有最高价值的行,并提取在Pandas中具有相同名称的所有行 - Identify the row in a group that has the highest value and extract all rows in that share its name in Pandas Pandas:对于组中的最后一行,为一列分配一个值 - Pandas: for each last row in a group, assign a column a value 您可以使用 Pandas 使用 Python 将多行按列值分组为一行吗? - Can you group multiple rows all into one row by column value with Python using pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM