【问题标题】:groupby and trim some rows based on conditiongroupby 并根据条件修剪一些行
【发布时间】:2020-05-08 12:02:47
【问题描述】:

我有一个类似这样的数据框:

df = pd.DataFrame({"ID":[1,1,2,2,2,3,3,3,3,3], 
               "IF_car":[1,0,0,1,0,0,0,1,0,1],
               "IF_car_history":[0,0,0,1,0,0,0,1,0,1],  
               "observation":[0,0,0,1,0,0,0,2,0,3]}) 

我想要输出,我可以在 groupby 中使用 ID 和条件在 "IF_car_history" == 1 上修剪行

tried_df =  df.groupby(['ID']).apply(lambda x: x.loc[:(x['IF_car_history'] == '1').idxmax(),:]).reset_index(drop = True)

我想在得到 ['IF_car_history'] == '1' 后删除 groupby 中的行

预期输出:

谢谢

【问题讨论】:

  • 你能添加预期的输出吗?

标签: python-3.x pandas pandas-groupby


【解决方案1】:

首先通过Series.eq比较掩码m的值,然后使用GroupBy.cumsum,对于1之前的值,通过0比较,最后通过boolean indexing过滤,但是因为id必须在最后一个@之后删除987654330@ 用于通过与[::-1] 切片交换值。

m = df['IF_car_history'].eq(1).iloc[::-1]
df1 = df[m.groupby(df['ID']).cumsum().ne(0).iloc[::-1]]
print (df1)
   ID  IF_car  IF_car_history  observation
2   2       0               0            0
3   2       1               1            1
5   3       0               0            0
6   3       0               0            0
7   3       1               1            2
8   3       0               0            0
9   3       1               1            3

【讨论】:

  • 我的预期输出与您的解决方案略有不同,但感谢您的时间。
猜你喜欢
  • 2013-03-06
  • 2023-03-24
  • 1970-01-01
  • 2021-07-10
  • 1970-01-01
  • 2017-06-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多