【问题标题】:Select or drop categories based on condition根据条件选择或删除类别
【发布时间】:2022-01-17 23:06:15
【问题描述】:

我有这个示例数据集:

我要做的是查看 ID 列上的哪些类别的值严格高于 45,同时向我展示其他不是。所以它应该告诉我 ID 'a' 和 'd' 符合我的标准,而 'b' 和 'c' 不在其中。之后,我将删除行 'b' 和 'c'

最简单的方法是什么?

我试过了

def filter_func(x):
     return x['vals']>45

df.groupby('id').filter(filter_func)
df['id'].unique()

but I get this error:
filter function returned a Series, but expected a scalar bool

【问题讨论】:

    标签: python for-loop group-by categories exploratory-data-analysis


    【解决方案1】:

    你可以试试这个方法:

    df2 = df.groupby('id').min().reset_index()
    df2.loc[df2['vals'] > 45]['id']
    

    【讨论】:

      猜你喜欢
      • 2021-12-07
      • 2014-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-01
      • 2022-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多