如何计算数据框中每一行的条件聚合度量?

发布于 2025-01-18 08:30:02 字数 1626 浏览 1 评论 0原文

我有一个这样的表...

日期PlayerId进球
六月 1 日A1
六月 14A1
六月 15B2
六月 28A1
七月 6 日B0
七月 17 日A1

我想计算一名球员的进球数前 30 天(不是 30 场比赛)。最终结果应如下所示...

DatePlayerIdgoalgoal_Prev_30
June 1A10
June 14A11
June 15B20
June 28A12
July 6thB02
July 17thA11

我创建了一个用于过滤的 for 循环识别数据框中的单行,然后根据行的特征过滤数据框,然后计算过滤后的数据框中的目标总和,将其附加到一个列表,最终分配给Goals_Prev_30列。代码看起来像......

30_day_goals = []
for i in range(len(df)):
    row = df.iloc[i]
    filtered_df = df[(df['Date'] < row['Date']) & (df['Date'] >= row['Date'])- pd.to_timedelta(30,unit='d')) & (df['PlayerId'] == row['PlayerId'])]
    total = filtered_df['Goals'].sum()
    30_day_goals.append(total)

df['Goals_Prev_30'] = 30_day_goals

这个解决方案有效,但速度很慢。它每秒可以处理大约 30 行,但这不是一个可行的解决方案,因为我有多个类似的度量,并且有超过 120 万行。这意味着每项测量大约需要 11 小时才能完成。

如何以更有效的方式解决这个问题?

I have a table like this...

DatePlayerIdGoals
June 1A1
June 14A1
June 15B2
June 28A1
July 6thB0
July 17thA1

I would like to calculate the amount of goals a player had scored in the 30 days prior (NOT 30 games). The final results should look like...

DatePlayerIdGoalsGoals_Prev_30
June 1A10
June 14A11
June 15B20
June 28A12
July 6thB02
July 17thA11

I created a for loop that filters that identifies a single row in the dataframe, then filters the dataframe by characteristics of the row, then calculates the sum of goals in the filtered dataframe, appends it to a list, which is finally assigned to the Goals_Prev_30 column. The code looks like...

30_day_goals = []
for i in range(len(df)):
    row = df.iloc[i]
    filtered_df = df[(df['Date'] < row['Date']) & (df['Date'] >= row['Date'])- pd.to_timedelta(30,unit='d')) & (df['PlayerId'] == row['PlayerId'])]
    total = filtered_df['Goals'].sum()
    30_day_goals.append(total)

df['Goals_Prev_30'] = 30_day_goals

This solution works, but it's slow. It can do around 30 rows a second, however it's not a viable solution as I have multiple measures that are similar and there are over 1.2M rows. This means it will take around 11hrs per measure to complete.

How can this problem be solved in a more efficient manner?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜雨飘雪 2025-01-25 08:30:02

我将您的解决方案更改为每个组的自定义函数,并使用广播创建的掩码和每个组的 Goals 列的 sum 值(如果匹配):

#if necessary
#df['Date'] = pd.to_datetime(df['Date'], format='%B %d')

def f(x):
    d1 = x['Date']
    d2 = d1 - pd.to_timedelta(30,unit='d')
    a1 = d1.to_numpy()
    a2 =  d2.to_numpy()
    
    m = (a1 < a1[:, None]) & (a1 >=a2[:, None])
    x['Goals_Prev_30'] = np.where(m, x['Goals'], 0).sum(axis=1)
    return x
 

df = df.groupby('PlayerId').apply(f)

print (df)
        Date PlayerId  Goals  Goals_Prev_30
0 1900-06-01        A      1              0
1 1900-06-14        A      1              1
2 1900-06-15        B      2              0
3 1900-06-28        A      1              2
4 1900-07-06        B      0              2
5 1900-07-17        A      1              1

I change your solution to custom function per groups with mask created by broadcasting and sum values of Goals column per groups if match:

#if necessary
#df['Date'] = pd.to_datetime(df['Date'], format='%B %d')

def f(x):
    d1 = x['Date']
    d2 = d1 - pd.to_timedelta(30,unit='d')
    a1 = d1.to_numpy()
    a2 =  d2.to_numpy()
    
    m = (a1 < a1[:, None]) & (a1 >=a2[:, None])
    x['Goals_Prev_30'] = np.where(m, x['Goals'], 0).sum(axis=1)
    return x
 

df = df.groupby('PlayerId').apply(f)

print (df)
        Date PlayerId  Goals  Goals_Prev_30
0 1900-06-01        A      1              0
1 1900-06-14        A      1              1
2 1900-06-15        B      2              0
3 1900-06-28        A      1              2
4 1900-07-06        B      0              2
5 1900-07-17        A      1              1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文