我有一个pd.dataframe,看起来像这样:
cookie date channel goal_reached
cookie_1 2020-01-12 paid 0
cookie_1 2020-02-17 organic 0
cookie_1 2020-04-02 referral 1
cookie_1 2020-05-13 direct 0
cookie_1 2020-05-16 direct 0
cookie_2 2020-01-18 referral 0
cookie_2 2020-03-13 paid 1
cookie_2 2020-04-01 organic 0
cookie_2 2020-05-16 organic 0
cookie_2 2020-05-22 paid 0
cookie_3 2020-01-13 direct 0
cookie_3 2020-04-14 organic 0
cookie_3 2020-06-10 organic 0
我想按每个cookie值分组,并删除goal_reached值为1的日期之后的所有行。如果cookie没有goal_reached值1,那么我需要获取所有行。
我想要这样的最终输出:
cookie channel goal_reached
cookie_1 paid > organic > referral 1
cookie_2 referral > paid 1
cookie_3 direct > organic > organic 0
我有以下代码,但它可以通过所有行进行分组:
df = df.sort_values(['cookie', 'date'],
ascending=[False, True])
df = df.groupby('cookie', as_index=False).agg({'channel': lambda x: "%s" % ' > '.join(x), 'reg_goal': 'max'})
你可以试试这个:
df = df[df.groupby('cookie')['goal_reached'].transform(lambda x: x.cumsum().cumsum()).lt(2)]
df = df.groupby('cookie').agg({'channel': lambda x: ' > '.join(x), 'goal_reached': 'max'})
print(df)
channel goal_reached
cookie
cookie_1 paid > organic > referral 1
cookie_2 referral > paid 1
cookie_3 direct > organic > organic 0