python - 将多个条件 groupby + sort + sum 应用于熊猫数据框行
问题描述
我有一个包含以下列的数据框:
帐户编号、通信日期、打开日期
对于每个开立的账户,我被要求回顾该账户开立日期后 30 天内发生的所有通信,然后为这些通信分配以下分数:
Forty-twenty-forty: Attribute 40% (0.4 points) of the attribution to the first touch,
40% to the last touch, and divide the remaining 20% between all touches in between
所以我知道按功能应用和分组,但这超出了我的工资等级。我必须按帐户分组,有条件地基于 2 列相互比较,我必须这样做才能获得对应的总数,我想它们也必须进行排序,作为分配点的以下步骤对应关系取决于它们发生的顺序。
我想有效地做到这一点,因为我有很多行,我知道 apply() 可以很快,但是当我尝试做的行级操作变得有点复杂时,我很不擅长应用它。
我很感激任何帮助,因为我不擅长熊猫。
根据要求编辑
Acct, ContactDate, OpenDate, Points (what I need to calculate)
123, 1/1/2018, 1/1/2021, 0 (because correspondance not within 30 days of open)
123, 12/10/2020, 1/1/2021, 0.4 (first touch gets 0.4)
123, 12/11/2020, 1/1/2021, 0.2 (other 'touches' get 0.2/(num of touches-2) 'points')
123, 12/12/2020, 1/1/2021, 0.4 (last touch gets 0.4)
456, 1/1/2018, 1/1/2021, 0 (again, because correspondance not within 30 days of open)
456, 12/10/2020, 1/1/2021, 0.4 (first touch gets 0.4)
456, 12/11/2020, 1/1/2021, 0.1 (other 'touches' get 0.2/(num of touches-2) 'points')
456, 12/11/2020, 1/1/2021, 0.1 (other 'touches' get 0.2/(num of touches-2) 'points')
456, 12/12/2020, 1/1/2021, 0.4 (last touch gets 0.4)
解决方案
这将返回一个缩减的数据帧,因为它排除了超过 30 天的时间帧,然后将原始 df 合并到其中,将所有数据都放在一个 df 中。这假设您的日期排序是正确的,否则,您可能必须在应用下面的函数之前预先执行此操作。
df['Points'] = 0 #add column to dataframe before analysis
#df.columns
#Index(['Acct', 'ContactDate', 'OpenDate', 'Points'], dtype='object')
def points(x):
newx = x.loc[(x['OpenDate'] - x['ContactDate']) <= timedelta(days=30)] # reduce for wide > 30 days
# print(newx.Acct)
if newx.Acct.count() > 2: # check more than two dates exist
newx['Points'].iloc[0] = .4 # first row
newx['Points'].iloc[-1] = .4 # last row
newx['Points'].iloc[1:-1] = .2 / newx['Points'].iloc[1:-1].count() # middle rows / by count of those rows
return newx
elif newx.Acct.count() == 2: # placeholder for later
#edge case logic here for two occurences
return newx
elif newx.Acct.count() == 1: # placeholder for later
#edge case logic here one onccurence
return newx
# groupby Acct then clean up the indices so it can be merged back into original df
dft = df.groupby('Acct', as_index=False).apply(points).reset_index().set_index('level_1').drop('level_0', axis=1)
# merge on index
df_points = df[['Acct', 'ContactDate', 'OpenDate']].merge(dft['Points'], how='left', left_index=True, right_index=True).fillna(0)
输出:
Acct ContactDate OpenDate Points
0 123 2018-01-01 2021-01-01 0.0
1 123 2020-12-10 2021-01-01 0.4
2 123 2020-12-11 2021-01-01 0.2
3 123 2020-12-12 2021-01-01 0.4
4 456 2018-01-01 2021-01-01 0.0
5 456 2020-12-10 2021-01-01 0.4
6 456 2020-12-11 2021-01-01 0.1
7 456 2020-12-11 2021-01-01 0.1
8 456 2020-12-12 2021-01-01 0.4
推荐阅读
- c# - 如何从数据表返回枚举到列表视图?
- reactjs - 为 axios 实现 Mutex 解决方案
- vue.js - 在进入路线之前如何重定向到特定路线?Vue路由器
- javascript - 如何使用 lodash 更新对象的嵌套数组
- nginx - nginx 本地重定向从 /var/www/html/index.html 到 /var/www/html/larvel_project/public
- python - Python - 递归地遍历目录并在文本文件中打印文件或空目录
- react-native - 如何在本机反应中打开选项卡容器内的另一个屏幕?
- c# - 如何通过传递另一列来设置 DataGridView 的列?
- pine-script - 范围条和枢轴点指示器在 Tradingview/PineScript 中同时工作
- php - foreach 的无效参数供应