在数据帧上使用滚动而不使用apply时遇到问题,这很慢

2024-09-30 18:28:45 发布

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

我有一个如下所示的数据框:

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']
  1. 因为我不想包括当天的奖品,所以我试图关闭右侧的滚动,但是这不起作用(例如,取出上面的closed='right'将做完全相同的事情)。结果,我在最后一行做了减法
  2. 我使用的实际DB是巨大的,我需要在不同的点进行许多滚动求和,但是速度太慢了。有人告诉我,我可以不使用.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

有什么想法吗


Tags: 数据rightiddfdatadatesumclosed
1条回答
网友
1楼 · 发布于 2024-09-30 18:28:45

改进了以前的答案,并成功解决了groupby的排序问题

df = pd.read_csv("data.csv")
df["Date"] = pd.to_datetime(df['Date'], format='%m-%d-%y')
df["CurrentWon"] = df["Prize"] * df["IfWon"]

result = df.groupby("ID").rolling("7D", on = 'Date', closed = 'right').CurrentWon.sum().reset_index()
result.rename(columns={"CurrentWon": "PreviousWon"}, inplace=True)
df = df.merge(result, on=["ID", "Date"])
df["PreviousWon"] -= df["CurrentWon"]
print(df)

产出:

   ID       Date  Prize  IfWon  CurrentWon  PreviousWon
0   1 2020-01-01      5      1           5          0.0
1   2 2020-01-01      8      1           8          0.0
2   1 2020-01-03      3      0           0          5.0
3   1 2020-01-04     10      1          10          5.0
4   1 2020-01-07      5      0           0         15.0
5   2 2020-01-10      5      1           5          0.0
6   3 2020-01-10     10      1          10          0.0

相关问题 更多 >