【发布时间】:2021-05-28 00:43:22
【问题描述】:
customer purchase_id payment_status price currency payment_date
1 Andy 6 REPAID 100 GBP 2020-04-16
2 Randy 10 IN_PROGRESS 10000 SEK 2020-04-17
我想做一个支点,在这里我可以看到有多少新老客户来购买。 预期输出:
new_customers old_customers
Jan 1 3
Feb 5 2
我被卡住了:
df['year']=df['payment_date'].dt.year
df['month']=df['payment_date'].dt.month
df2=pd.DataFrame(df.groupby("customer", sort=False)["purchase_id"].count())
df2=number_of_purchase.reset_index()
df2.columns = ['merchant_code','number_of_purchase']
df2['repeat_customer']=np.where(df['number_of_purchase']>1,'old_customers','new_customers')
我不知道如何将df2 与df 集成在一起,在df 中,客户可以出现多次,具有不同的purchase_id、价格和付款日期。
代码的最后部分大概是这样的:
df.groupby(["year","month", "repeat_customers"])["repeat_customers"].count()
但请随意更改我的代码,输出更重要。
【问题讨论】:
标签: python pandas numpy pandas-groupby