通过比较其他字段和字典值来计算字段

2024-05-19 18:41:31 发布

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

我有两个数据帧。第一个是这样的:

id, orderdate, orderid, amount,camp1, camp2, camp3
1   2020-01-01  100      100       1    0       0
2   2020-02-01  120      200       1    0       1
3   2019-12-01  130      500       0    1       0
4   2019-11-01  150      750       0    1       0
5   2020-01-01  160      1000      1    1       1

camp1, camp2, camp3部分显示客户是否参加了活动

这些活动有一个句号字典

camp_periods = {'camp1': 
                        [datetime.strptime('2019-04-08', '%Y-%m-%d'), datetime.strptime('2019-06-06', '%Y-%m-%d')],
                'camp2':
                        [datetime.strptime('2019-09-15', '%Y-%m-%d'), datetime.strptime('2019-09-28', '%Y-%m-%d')],
                'camp3':
                        [datetime.strptime('2019-11-15', '%Y-%m-%d'), datetime.strptime('2019-12-28', '%Y-%m-%d')]
                 }

如果orderdatecamp_periods字典中的活动期间之间,并且客户参加了该活动,我想创建一个表,给出每个客户的订单数量和order amounts总数


Tags: 数据iddatetime客户字典amountperiodsstrptime
1条回答
网友
1楼 · 发布于 2024-05-19 18:41:31

我不确定我是否很理解你的问题,我猜当你说订单数量和order amounts总数时,你想得到第一批n数量低于或等于给定总数order amounts的订单,我的方法如下:

数据示例:

enter image description here

from operator import or_
from functools import reduce


number_orders = 2
total_order_amounts = 3000

orderdate is between the campaign periods in the camp_periods dictionary and if the customer attended to that campaign

cond = [(df[k].astype('bool') & df['orderdate'].between(*v)) for k, v in camp_periods.items()]
cond = reduce(or_, cond)
df_cond = df[cond]
df_final = df_cond[df_cond['amount'].cumsum() <= total_order_amounts].head(number_orders)

df_final

输出:

enter image description here

相关问题 更多 >