【问题标题】:pivot table for new and old customer新老客户数据透视表
【发布时间】: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')

我不知道如何将df2df 集成在一起,在df 中,客户可以出现多次,具有不同的purchase_id、价格和付款日期。 代码的最后部分大概是这样的:

df.groupby(["year","month", "repeat_customers"])["repeat_customers"].count()

但请随意更改我的代码,输出更重要。

【问题讨论】:

    标签: python pandas numpy pandas-groupby


    【解决方案1】:
    • 您的样本数据没有足够的特征,所以我生成了一个与结构匹配的随机数据集
    • 使用月份开始要简单得多,因此只需转到这些
    • 新客户需要一个定义,我从您的代码中暗示了它的定义
    • 用这个定义直接计算
    • 最终将结果重塑为您想要的结构
    • 您可能希望在输出 DF 中格式化月份
    import numpy as np
    d = pd.date_range("01-Jan-2020", periods=10, freq="W")
    c = ['tenetur', 'quae', 'rem', 'maxime', 'sunt']
    df = pd.DataFrame({"customer":np.random.choice(c, len(d)),
                 "purchase_id":np.random.randint(1,10, len(d)),
                 "payment_status":np.random.choice(["REPAID","IN_PROGRESS"],len(d)),
                 "price":np.random.randint(100,10000, len(d)),
                 "currency":np.random.choice(["GBP","SEK"],len(d)),
                 "payment_date":d})
    
    # only interested with month start
    df2 = (df.assign(ms=df.payment_date - pd.to_timedelta(df.payment_date.dt.day-1, "d"),
               # find first time a customer made a purchase
              fms=lambda dfa: dfa.groupby("customer")["ms"].transform("first"),
               # if month of purchase and first month customer made a purchase are same, new ...
              new_customer=lambda dfa: np.where(dfa.ms==dfa.fms, "new_customers", "old_customers")
             )
     # with the prep it's a simple count
     .groupby(["ms","new_customer"])["customer"].count()
     # format the results
     .to_frame().unstack(1).fillna(0).droplevel(0, axis=1).rename_axis("", axis=1)
    )
    
    

    df

    customer purchase_id payment_status price currency payment_date
    0 sunt 5 REPAID 9228 SEK 2020-01-05 00:00:00
    1 tenetur 6 IN_PROGRESS 1458 SEK 2020-01-12 00:00:00
    2 maxime 7 IN_PROGRESS 9798 GBP 2020-01-19 00:00:00
    3 sunt 1 IN_PROGRESS 2418 SEK 2020-01-26 00:00:00
    4 maxime 8 IN_PROGRESS 6608 GBP 2020-02-02 00:00:00
    5 sunt 4 IN_PROGRESS 2341 GBP 2020-02-09 00:00:00
    6 rem 7 REPAID 8961 GBP 2020-02-16 00:00:00
    7 quae 2 REPAID 7068 GBP 2020-02-23 00:00:00
    8 maxime 1 IN_PROGRESS 4872 SEK 2020-03-01 00:00:00
    9 tenetur 1 REPAID 2860 GBP 2020-03-08 00:00:00

    df2

    ms new_customers old_customers
    2020-01-01 00:00:00 4 0
    2020-02-01 00:00:00 2 2
    2020-03-01 00:00:00 0 2

    【讨论】:

      猜你喜欢
      • 2015-08-12
      • 2016-09-07
      • 2016-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-10
      • 2018-12-30
      相关资源
      最近更新 更多