【发布时间】: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