我有一个数据框,如下所示:
ID Date Prize IfWon
1 01-01-20 5 1
2 01-01-20 8 1
1 01-03-20 3 0
1 01-04-20 10 1
1 01-07-20 5 0
2 01-10-20 5 1
3 01-10-20 10 1
我想添加一个新列,对于给定的 ID,该列将包括他们在该日期之前 7 天内赢得的所有奖金的总和,但不包括该日期。目标是拥有一个如下所示的数据框:
ID Date Prize IfWon PrevWon
1 01-01-20 5 1 0
2 01-01-20 8 1 0
1 01-03-20 3 0 5
1 01-04-20 10 1 5
1 01-07-20 5 0 15
2 01-10-20 5 1 0
3 01-10-20 10 1 0
我必须执行的代码如下,它可以工作,但我有两个问题:
def get_rolling_prize_sum(grp, freq):
return grp.rolling(freq, on = 'Date', closed = 'right')['CurrentWon'].sum()
processed_data_df['CurrentWon'] = processed_data_df['Prize'] * processed_data_df['IfWon'] # gets deleted later
processed_data_df['PrevWon'] = processed_data_df.groupby('ID', group_keys=False).apply(get_rolling_prize_sum, '7D').astype(float) - processed_data_df['CurrentWon']
因为我不想包括当天的奖品,所以我试图关闭右侧的滚动,但这不起作用(例如,取出上面的 close = 'right' 会做完全相同的事情) 。因此,我最终在最后一行进行了减法。
我使用的实际数据库很大,我需要在不同的点进行许多滚动求和,但它的速度非常慢。有人告诉我,我可以在不使用 .apply 的情况下直接使用 .rolling 来完成此操作,但我无法使其正常工作。我的尝试如下,有错误,我会注意到该错误花了几分钟才产生,这是唯一重要的计算,所以看起来好像它正在执行其中的一部分,然后稍后失败:
# Not using closed right here, just subtracting
processed_data_df['PrevWon'] = processed_data_df.groupby('ID', group_keys=False).rolling('7D', on = 'Date')['CurrentWon'].sum() - processed_data_df['CurrentWon']
ValueError: cannot join with no overlapping index names
有任何想法吗?
慕码人8056858
相关分类