使用 groupby 变换查找特定年份中每组的平均比率

问题描述 投票:0回答:1

我正在尝试找到更好/更快的方法来做到这一点。我有一个相当大的数据集(约 2 亿行),每行都有单独的日期。我想找到 2018 年到 2019 年每组的平均年费率。我知道我可以用结果创建一个小的 df 并将其合并回来,但是,我试图找到一种使用转换的方法。不确定合并是否会更快。一句台词加分。

样本数据

rng = np.random.default_rng(seed=123)
df = pd.DataFrame({'group':rng.choice(list('ABCD'), 100),
                   'date':[(pd.to_datetime('2018')+pd.Timedelta(days=x)).normalize() for x in rng.integers(0, 365*5, 100)],
                   'foo':rng.integers(1, 100, 100),
                   'bar':rng.integers(50, 200, 100)})
df['year'] = df['date'].dt.year

这有效

#find average 2018 and 2019 'foo' and 'bar'
for col in ['foo', 'bar']:
    for y in [2018, 2019]:
        df[col+'_'+str(y)+'_total'] = df.groupby('group')['year'].transform(lambda x: df.loc[x.where(x==y).dropna().index, col].sum())

#find 2018 and 2019 rates
for y in [2018, 2019]:
    df['rate_'+str(y)] =  df['foo_'+str(y)+'_total'].div(df['bar_'+str(y)+'_total'])

#find average rate
df['2018_2019_avg_rate'] = df[['rate_2018', 'rate_2019']].mean(axis=1)

我尝试过的方法不太有效(我正在使用

apply
来测试它是否有效,然后再切换到
transform

#gives yearly totals for each year and each column, but further 'apply'ing to find rates then averaging isn't working after I switch to transform
df.groupby(['group', 'year'])['year'].apply(lambda x: df.loc[x.where(x.between(2018, 2019)).dropna().index, ['foo', 'bar']].sum())

#close but is averaging too early
df.groupby(['group', 'year'])['year'].apply(lambda x: df.loc[i, 'foo'].sum()/denom if (denom:=df.loc[i:=x.where(x.between(2018, 2019)).dropna().index, 'bar'].sum())>0 else np.nan)
python pandas
1个回答
0
投票

您无法使用

groupby.transform
有效地执行多个过滤/聚合。你必须循环。

更有效的方法是将

pivot_table
+
merge
:

cols = ['foo', 'bar']
years = [2018, 2019]

tmp = (df[df['year'].isin(years)]
       .pivot_table(index='group', columns='year',
                    values=cols, aggfunc='sum')
       [cols]
       .pipe(lambda x: x.join(pd.concat({'rate': x['foo'].div(x['bar'])}, axis=1)))
      )

avg_rate = tmp['rate'].mean(axis=1)

tmp.columns = tmp.columns.map(lambda x: f'{x[0]}_{x[1]}_total')

tmp[f'{"_".join(map(str, years))}_avg_rate'] = avg_rate

out = df.merge(tmp, left_on='group', right_index=True)

输出:

   group       date  foo  bar  year  foo_2018_total  foo_2019_total  bar_2018_total  bar_2019_total  rate_2018_total  rate_2019_total  2018_2019_avg_rate
0      A 2022-03-11   59   91  2022             343             270             972             875         0.352881         0.308571            0.330726
1      C 2018-08-22   56   52  2018             175             325             331             902         0.528701         0.360310            0.444506
2      C 2019-04-24   47   89  2019             175             325             331             902         0.528701         0.360310            0.444506
3      A 2019-04-16   43  102  2019             343             270             972             875         0.352881         0.308571            0.330726
4      D 2019-11-25    3   56  2019             126             222             224             696         0.562500         0.318966            0.440733
5      A 2018-01-06   86  148  2018             343             270             972             875         0.352881         0.308571            0.330726
...
99     B 2018-02-25   32   90  2018             253             204             703             400         0.359886         0.510000            0.434943
© www.soinside.com 2019 - 2024. All rights reserved.