【问题标题】:Unique values in the past 12 months within a Groupby in PandasPandas 中 Groupby 过去 12 个月内的唯一值
【发布时间】:2020-05-08 02:01:54
【问题描述】:

如何在 groupby 中滚动 12M 并返回每行的唯一值(最好在列表中)?

目前我有一个如下所示的熊猫数据框。我希望按 itemId 对它们进行分组,并用过去 12 个月内的唯一卖方 ID 列表替换卖方 ID(基于有效日期)。 EffectiveDate 是 monthEnd 格式。基本上我想查看每个月的每个 itemId,他们是过去 12 个月窗口内的唯一卖家 ID。

            itemId   sellerId   effectiveDate
    1975245 2585893  31280      2005-12-31
    1975246 2585893  31280      2006-02-28
    1975247 2585893  5407       2006-06-30
    1975248 2585893  5407       2006-08-31
    1975249 2585893  5407       2006-09-30
    1975250 2585893  5407       2006-11-30
    1975254 2585893  5407       2007-05-31
    1975257 2585893  5407       2007-06-30
    1975258 2585893  5407       2007-07-31
    1975259 2585893  5407       2008-03-31
    ...

我想把它变成如下内容:

            itemId  uniqueSellerIds effectiveDate
    1975245 2585893 [31280]         2005-12-31
    1975246 2585893 [31280]         2006-02-28
    1975247 2585893 [5407,31280]    2006-06-30
    1975248 2585893 [5407,31280]    2006-08-31
    ...

我尝试过使用 groupby 然后滚动方法,但它不起作用。谢谢您的帮助。

【问题讨论】:

    标签: python pandas dataframe pandas-groupby


    【解决方案1】:

    使用dt.year 怎么样?

    new_df = df.groupby([df["effectiveDate"].dt.year, df["itemId"]])["sellerId"].agg(list).to_frame()
    
    print(new_df)
                                        sellerId
    effectiveDate     itemId                      
    2005              1975245 2585893  [31280]
    2006              1975246 2585893  [31280]
                      1975247 2585893   [5407]
                      1975248 2585893   [5407]
                      1975249 2585893   [5407]
                      1975250 2585893   [5407]
    2007              1975254 2585893   [5407]
                      1975257 2585893   [5407]
                      1975258 2585893   [5407]
    2008              1975259 2585893   [5407]
    

    【讨论】:

      【解决方案2】:

      我将原来的DataFrame修改成这样:

          itemId          sellerId   effectiveDate
          19752572585893  31280      2005-12-31
          19752572585893  31280      2006-02-28
          19752592585894  31280      2008-01-31
          19752592585894  5407       2007-07-31
          19752592585894  5407       2008-03-31
          19752592585894  5407       2008-01-31
      

      从那里我将其过滤为每个itemId 的最近一年:

      df['effectiveDate'] = pd.to_datetime(df['effectiveDate'])
      filtered = df[df.groupby(by=['itemId']).apply(lambda g: 
                                                    g['effectiveDate'] >= 
                                                    g['effectiveDate'].max() - 
                                                    pd.Timedelta(days=365)).values]                                                            
      

      然后我像这样组合sellerIds:

      filtered.groupby(by=['itemId'])['sellerId'].agg(lambda x: x.unique().tolist())     
      

      剩下的就是获取最大日期并将其加入到过滤和组合的数据中:

      max_dates = filtered.groupby(by=['itemId'])['effectiveDate'].max()
      modified_df = pd.concat([compressed,max_dates],axis=1)  
      

      结果:

                           sellerId effectiveDate
      itemId                                     
      19752572585893        [31280]    2006-02-28
      19752592585894  [31280, 5407]    2008-03-31
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-09-27
        • 2013-09-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多