【问题标题】:Pandas delete all duplicate rows in one column if values in another column is higher than a threshold如果另一列中的值高于阈值,Pandas 会删除一列中的所有重复行
【发布时间】:2021-12-24 05:26:35
【问题描述】:

我有一个数据框,其中 A 列中有重复值,而 B 列中有不同的值。

如果 A 列重复值之一在 B 列中的值高于 15,我想删除行。

原始数据帧

A Column B Column
1 10
1 14
2 10
2 20
3 5
3 10

所需的数据帧

A Column B Column
1 10
1 14
3 5
3 10

【问题讨论】:

    标签: python pandas group-by


    【解决方案1】:

    这是使用groupby()transform() 的另一种方式

    df.loc[~df['B Column'].gt(15).groupby(df['A Column']).transform('any')]
    

    【讨论】:

      【解决方案2】:

      这行得通:

      dfnew = df.groupby('A Column').filter(lambda x: x['B Column'].max()<=15 )
      dfnew.reset_index(drop=True, inplace=True) 
      dfnew = dfnew[['A Column','B Column']] 
      print(dfnew)
      

      输出:

         A Column  B Column
      0         1        10
      1         1        14
      2         3         5
      3         3        10
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-04-19
        • 1970-01-01
        • 2017-12-17
        • 2018-10-29
        • 2019-10-10
        • 1970-01-01
        • 2022-11-04
        • 1970-01-01
        相关资源
        最近更新 更多