如何优化 DataFrame 的分组并对组执行操作

How to optimize grouping of a DataFrame and performing operations on the groups

这是我的数据框示例:

d = {'group': ['a', 'a', 'a', 'b', 'b', 'b', 'b', 'b', 'c', 'd', 'd'], \
     'round': [3, 3, 2, 1, 3, 1, 3, 3, 3, 2, 1], \
     'score': [0.3, 0.1, 0.6, 0.8, 0.2, 0.5, 0.5, 0.6, 0.4, 0.9, 0.1]}
df = pd.DataFrame(d)
df
    group   round   score
0   a          3    0.3
1   a          3    0.1
2   a          2    0.6
3   b          1    0.8
4   b          3    0.2
5   b          1    0.5
6   b          3    0.5
7   b          3    0.6
8   c          3    0.4
9   d          2    0.9
10  d          1    0.1

我的实际数据框有 6 列和 > 1,000,000 行。我正在尝试找出执行以下操作的最快方法:

对于每组,找出得分的平均值,并对 3 轮中的每一轮进行一些计算。如果没有分数,写'NA'.

我不确定制作列表列表然后将其转换为数据框或制作新数据框并填充它是否会更快,所以我先使用列表:

def test_df(data):
    value_counts = data['group'].value_counts().to_dict()
    avgs = []

    for key, val in value_counts.items():
        row = data[data['group'] == key]
        
        x = [key]
        
        if val < 2:
            x.extend([10 * row['score'].values[0] + 1 if i == row['round'].values[0] else 'NA' for i in range (1,4)])
        
        else:
            x.extend([(10 * row[row['round'] == i]['score'].mean() + 1) if len(row[row['round'] == i]) > 0 else 'NA' for i in range(1, 4)])
            
        avgs.append(x)     
            
    return avgs

这里我创建了一个单独的案例,因为我的数据中大约 80% 的组只有一行,所以我想这可能会加快速度?

这个 returns 格式正确的结果 [group, round 1, round 2, round 3]

[['b', 7.5, 'NA', 5.333333333333333],
 ['a', 'NA', 7.0, 3.0],
 ['d', 2.0, 10.0, 'NA'],
 ['c', 'NA', 'NA', 5.0]]

但看起来在实际数据帧上需要很长时间... 有没有人有更好的想法?

在我看来,您基本上是在 groupby/mean 和一个支点。

import pandas as pd
d = {'group': ['a', 'a', 'a', 'b', 'b', 'b', 'b', 'b', 'c', 'd', 'd'], \
     'round': [3, 3, 2, 1, 3, 1, 3, 3, 3, 2, 1], \
     'score': [0.3, 0.1, 0.6, 0.8, 0.2, 0.5, 0.5, 0.6, 0.4, 0.9, 0.1]}
df = pd.DataFrame(d)

df = (df.groupby(['group','round'])['score'].mean()*10+1).reset_index()
df.pivot_table(index='group',columns='round',values='score', fill_value='NA').reset_index().values

输出

array([['a', 'NA', 7.0, 3.0],
       ['b', 7.5, 'NA', 5.333333333333333],
       ['c', 'NA', 'NA', 5.0],
       ['d', 2.0, 10.0, 'NA']], dtype=object)

不平衡的数据集可能会显示不同的结果,但我使用 blow 脚本进行了测试,发现即使使用 pandas 数据帧,结果也显示出不错的性能。但是,您始终可以将其与本机 python 数据结构进行比较。

import random
import datetime
import pandas as pd

def generate_data():  # augmentation
    data = {'group': [], 'round': [], 'score': []}
    for index in range(10 ** 6):  # sample size
        data['group'].append(random.choice(['a', 'b', 'c', 'd']))
        data['round'].append(random.randrange(1, 4))
        data['score'].append(round(random.random(), 1))
    return data

def calc_with_native_ds(data):  # native python data structure
    pass

def calc_with_pandas_df(df):  # pandas dataframe
    return df.groupby(['group', 'round']).mean()

if __name__ == '__main__':

    data = generate_data()
    df = pd.DataFrame(data)
    print(df.shape)

    start_datetime = datetime.datetime.now()

    # calc_with_native_ds(data)
    calc_with_pandas_df(df)

    end_datetime = datetime.datetime.now()
    elapsed_time = round((end_datetime - start_datetime).total_seconds(), 5)
    print(f"elapsed_time: {elapsed_time}")