gpt4 book ai didi

python - Dask DataFrame 计算多列分组内的平均值

转载 作者:行者123 更新时间:2023-12-03 16:51:10 27 4
gpt4 key购买 nike

我有一个如图所示的数据框,我想要做的是沿“试验”列取平均值。它为每 subject , conditionsample (当所有这三列的值为 1 时),取沿列试验(100 行)的数据的平均值。
我在 Pandas 中所做的如下

sub_erp_pd= pd.DataFrame()
for j in range(1,4):
sub_c=subp[subp['condition']==j]
for i in range(1,3073):
sub_erp_pd=sub_erp_pd.append(sub_c[sub_c['sample']==i].mean(),ignore_index=True)
但这需要很多时间..
所以我想使用 dask 而不是 Pandas。
但是在 dask 我在创建空数据框时遇到问题。就像我们在 Pandas 中创建一个空的数据框并将数据附加到它。
image of data frame
正如@edesz 所建议的,我改变了我的方法
编辑
%%time
sub_erp=pd.DataFrame()
for subno in progressbar.progressbar(range(1,82)):
try:
sub=pd.read_csv('../input/data/{}.csv'.format(subno,subno),header=None)
except:
sub=pd.read_csv('../input/data/{}.csv'.format(subno,subno),header=None)
sub_erp=sub_erp.append(sub.groupby(['condition','sample'], as_index=False).mean())
使用 pandas 读取文件需要 13.6 秒,而使用 dask 读取文件需要 61.3 毫秒。但是在 dask 中,我在追加时遇到了麻烦。
注意 - 原始问题的标题是创建一个空的 dask 数据框并向其附加值。

最佳答案

如果我理解正确,你需要

  • 使用 groupby (阅读更多 here )以便对 subject 进行分组, conditionsample
  • 这会将在这三列中的每一列中具有相同值的所有行收集到一个组中

  • 使用 .mean() 取平均值
  • 这将为您提供每个组内的平均值


  • Generate一些虚拟数据
    df = df = pd.DataFrame(np.random.randint(0,100,size=(100, 3)),
    columns=['trial','condition','sample'])
    df.insert(0,'subject',[1]*10 + [2]*30 + [5]*60)

    print(df.head())
    subject trial condition sample
    0 1 71 96 34
    1 1 2 89 66
    2 1 90 90 81
    3 1 93 43 18
    4 1 29 82 32
    Pandas 接近
    聚合取 mean
    df_grouped = df.groupby(['subject','condition','sample'], as_index=False)['trial'].mean()

    print(df_grouped.head(15))
    subject condition sample trial
    0 1 18 24 89
    1 1 43 18 93
    2 1 67 47 81
    3 1 82 32 29
    4 1 85 28 97
    5 1 88 13 48
    6 1 89 59 23
    7 1 89 66 2
    8 1 90 81 90
    9 1 96 34 71
    10 2 0 81 19
    11 2 2 39 58
    12 2 2 59 94
    13 2 5 42 13
    14 2 9 42 4
    Dask 方法
    步骤 1. 进口
    import dask.dataframe as dd
    from dask.diagnostics import ProgressBar
    步骤 2. 转换 Pandas DataFrame到达斯克 DataFrame , 使用 .from_pandas
    ddf = dd.from_pandas(df, npartitions=2)
    步骤 3. 聚合并取 mean
    ddf_grouped = (
    ddf.groupby(['subject','condition','sample'])['trial']
    .mean()
    .reset_index(drop=False)
    )

    with ProgressBar():
    df_grouped = ddf_grouped.compute()
    [ ] | 0% Completed | 0.0s
    [########################################] | 100% Completed | 0.1s

    print(df_grouped.head(15))
    subject condition sample trial
    0 1 18 24 89
    1 1 43 18 93
    2 1 67 47 81
    3 1 82 32 29
    4 1 85 28 97
    5 1 88 13 48
    6 1 89 59 23
    7 1 89 66 2
    8 1 90 81 90
    9 1 96 34 71
    10 2 0 81 19
    11 2 2 39 58
    12 2 2 59 94
    13 2 5 42 13
    14 2 9 42 4
    重要提示:此答案中的方法不使用创建空的 Dask DataFrame 并向其附加值的方法,以计算主题、条件和试验分组内的平均值。相反,此答案提供了一种替代方法(使用 GROUP BY )来获得所需的最终结果(计算主题、条件和试验分组内的平均值)。

    关于python - Dask DataFrame 计算多列分组内的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55665170/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com