Groupby,只计算在特定时间点调用客户的次数

2024-04-29 05:19:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我的问题与Groupby count only when a certain value is present in one of the column in pandas密切相关

假设我有一个按not_unique_iddate_of_call排序的数据帧

rng = pd.date_range('2015-02-24', periods=8, freq='D')
df = pd.DataFrame(
    {
    "unique_id": ["K0", "K1", "K2", "K3", "K4", "K5", "K6","K7"],
    "not_unique_id": ["A000", "A111", "A222", "A222", "A222", "A222", "A222","A333"],
    "date_of_call": rng,
    "customer_reached": [1,0,0,1,1,1,1,1],
    }
    ) 
df.sort_values(['not_unique_id','date_of_call'], inplace=True, ascending=False)
df.reset_index(drop=True, inplace=True) # reset index

df.head(10)

enter image description here

现在我想添加一个新专栏,告诉我过去成功呼叫客户的频率。换句话说:计算过去到达客户的次数,并将结果保存在新列中。对于所提供的示例,结果如下:

enter image description here

你会怎么做


1条回答
网友
1楼 · 发布于 2024-04-29 05:19:50

如果所有日期时间都是唯一的且已排序,则可以通过以下方式更改顺序:首先索引,然后使用shift^{}聚合自定义函数,最后替换缺少的值并将列转换为整数:

df['new'] = (df.iloc[::-1]
               .groupby('not_unique_id')['customer_reached']
               .apply(lambda x: x.shift().cumsum())
               .fillna(0)
               .astype(int))
print (df)
  unique_id not_unique_id date_of_call  customer_reached  new
0        K7          A333   2015-03-03                 1    0
1        K6          A222   2015-03-02                 1    3
2        K5          A222   2015-03-01                 1    2
3        K4          A222   2015-02-28                 1    1
4        K3          A222   2015-02-27                 1    0
5        K2          A222   2015-02-26                 0    0
6        K1          A111   2015-02-25                 0    0
7        K0          A000   2015-02-24                 1    0

或如有可能,变更单:

df.sort_values(['not_unique_id','date_of_call'], inplace=True)


df['new'] = (df.groupby('not_unique_id')['customer_reached']
               .apply(lambda x: x.shift().cumsum())
               .fillna(0)
               .astype(int))
print (df)
  unique_id not_unique_id date_of_call  customer_reached  new
0        K0          A000   2015-02-24                 1    0
1        K1          A111   2015-02-25                 0    0
2        K2          A222   2015-02-26                 0    0
3        K3          A222   2015-02-27                 1    0
4        K4          A222   2015-02-28                 1    1
5        K5          A222   2015-03-01                 1    2
6        K6          A222   2015-03-02                 1    3
7        K7          A333   2015-03-03                 1    0

相关问题 更多 >