如何获得每n行np.arrays列的平均值,然后为组中的每个成员添加平均值到新列

康纳449

我有一个像这样的 np.arrays df:

x           y    
0      [2,0,1]
1      [0,0,4]
2      [0,0,1]
3      [2,0,2]
4      [1,0,1]
5      [0,0,2]
6      [0,1,1]
7      [0,0,0]
8      [0,0,1]

我想获得平均数组元素,我想象使用像 np.mean(x,axis=0) 这样的 3 行。手段应该是这样的:

       [.66,   0,    2]
       [  1,   0, 1.66]
       [. 0, .33,  .66]

然后我想将每个组的平均值添加到一个新列中,其中该值对组中的每个行成员重复。它应该是这样的:

x           y    
0      [2,0,1]       [.66,   0,    2]
1      [0,0,4]       [.66,   0,    2]
2      [0,0,1]       [.66,   0,    2]
3      [2,0,2]       [  1,   0, 1.66]
4      [1,0,1]       [  1,   0, 1.66]
5      [0,0,2]       [  1,   0, 1.66]
6      [0,1,1]       [. 0, .33,  .66]
7      [0,0,0]       [. 0, .33,  .66]
8      [0,0,1]       [. 0, .33,  .66]

亨利·埃克

假设有一个标准的 RangeIndex,我们可以每 3 行变成一个 DataFrame表示,然后将 DataFrame 返回到一系列列表中:apply pd.Seriesygroupbytransform

df['new_col'] = (
    df['y'].apply(pd.Series)  # TUrn into DataFrame
        .groupby(df.index // 3)  # Group rows into groups of 3
        .transform('mean')  # Calculate mean per group
        .apply(list, axis=1)  # Make DataFrame a Series of lists
)

df

   x          y                                        new_col
0  0  [2, 0, 1]                 [0.6666666666666666, 0.0, 2.0]
1  1  [0, 0, 4]                 [0.6666666666666666, 0.0, 2.0]
2  2  [0, 0, 1]                 [0.6666666666666666, 0.0, 2.0]
3  3  [2, 0, 2]                 [1.0, 0.0, 1.6666666666666667]
4  4  [1, 0, 1]                 [1.0, 0.0, 1.6666666666666667]
5  5  [0, 0, 2]                 [1.0, 0.0, 1.6666666666666667]
6  6  [0, 1, 1]  [0.0, 0.3333333333333333, 0.6666666666666666]
7  7  [0, 0, 0]  [0.0, 0.3333333333333333, 0.6666666666666666]
8  8  [0, 0, 1]  [0.0, 0.3333333333333333, 0.6666666666666666]

如果需要,DataFrame.round也可以链接:

df['new_col'] = (
    df['y'].apply(pd.Series)  # TUrn into DataFrame
        .groupby(df.index // 3)  # Group rows into groups of 3
        .transform('mean')  # Calculate mean per group
        .round(2)  # Round to 2 Decimal Places
        .apply(list, axis=1)  # Make DataFrame a Series of lists
)

df

   x          y            new_col
0  0  [2, 0, 1]   [0.67, 0.0, 2.0]
1  1  [0, 0, 4]   [0.67, 0.0, 2.0]
2  2  [0, 0, 1]   [0.67, 0.0, 2.0]
3  3  [2, 0, 2]   [1.0, 0.0, 1.67]
4  4  [1, 0, 1]   [1.0, 0.0, 1.67]
5  5  [0, 0, 2]   [1.0, 0.0, 1.67]
6  6  [0, 1, 1]  [0.0, 0.33, 0.67]
7  7  [0, 0, 0]  [0.0, 0.33, 0.67]
8  8  [0, 0, 1]  [0.0, 0.33, 0.67]

如果索引不是标准的 RangeBased 索引,我们可以使用以下方法创建自己的索引np.arange

df['new_col'] = (
    df['y'].apply(pd.Series)
        .groupby(np.arange(len(df)) // 3)  # Create Range index based df length
        .transform('mean')
        .apply(list, axis=1)
)

数据框和导入:

import numpy as np
import pandas as pd

df = pd.DataFrame({
    'x': [0, 1, 2, 3, 4, 5, 6, 7, 8],
    'y': [[2, 0, 1], [0, 0, 4], [0, 0, 1], [2, 0, 2], [1, 0, 1], [0, 0, 2],
          [0, 1, 1], [0, 0, 0], [0, 0, 1]]
})

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章