python - 在 numpy 中对按变量分组的行取平均值

标签 python numpy pandas

我有一个如下所示的 numpy 数组。

array([[ 0.23810484,  0.00020161,  0.41350806,  0.2421371 ,  0.02237903,
         0.08084677,  0.00020161,  0.00221774,  0.00020161,  0.00020161],
       [ 0.04279661,  0.05974576,  0.02584746,  0.00042373,  0.00042373,
         0.00042373,  0.00042373,  0.73771186,  0.00889831,  0.12330508]])

它是 5000X10。

我还有一个 Pandas 系列对象,它的长度也是 5000。它的值是这样>

5061             Terminated
17410    Completed Negative

共有三个不同的类别。每个系列值都是第一个 numpy 数组中相应行的类别。

我想要得到的是对按系列中的类别分组的第一个数组中的每个变量取平均值。所以最后我会有一个 numpy 数组,每个类别的系列有 3 行,十列的值将是所有 5000 行的平均值。

请指教

最佳答案

您可以将 numpy 数组中的每一列添加到 pandas DataFrame 中的单独列,然后使用 DataFrame.groupby() 根据您需要的列进行分组,然后取 mean () 。示例(假设您的系列名为 series ,numpy 数组名为 narray)-

df = pd.DataFrame(series)
for i in range(10):
    df[i] = narray[:,i]

df.groupby('required_column').mean()

演示 -

In [77]: df = pd.DataFrame([[5061,'Terminated'],[17410,'Completed Negative']],columns=['index','groupcol']).set_index('index')

In [78]: df
Out[78]:
                 groupcol
index
5061           Terminated
17410  Completed Negative

In [79]: x
Out[79]:
array([[  2.38104840e-01,   2.01610000e-04,   4.13508060e-01,
          2.42137100e-01,   2.23790300e-02,   8.08467700e-02,
          2.01610000e-04,   2.21774000e-03,   2.01610000e-04,
          2.01610000e-04],
       [  4.27966100e-02,   5.97457600e-02,   2.58474600e-02,
          4.23730000e-04,   4.23730000e-04,   4.23730000e-04,
          4.23730000e-04,   7.37711860e-01,   8.89831000e-03,
          1.23305080e-01]])

In [80]: for i in range(10):
   ....:     df[i] = x[:,i]
   ....:

In [81]: df
Out[81]:
                 groupcol         0         1         2         3         4  \
index
5061           Terminated  0.238105  0.000202  0.413508  0.242137  0.022379
17410  Completed Negative  0.042797  0.059746  0.025847  0.000424  0.000424

              5         6         7         8         9
index
5061   0.080847  0.000202  0.002218  0.000202  0.000202
17410  0.000424  0.000424  0.737712  0.008898  0.123305

In [82]: df.groupby('groupcol').mean()
Out[82]:
                           0         1         2         3         4  \
groupcol
Completed Negative  0.042797  0.059746  0.025847  0.000424  0.000424
Terminated          0.238105  0.000202  0.413508  0.242137  0.022379

                           5         6         7         8         9
groupcol
Completed Negative  0.000424  0.000424  0.737712  0.008898  0.123305
Terminated          0.080847  0.000202  0.002218  0.000202  0.000202

如果你想要结果作为列表,你可以这样做 -

df.groupby('required_column').mean().values.tolist()

演示 -

In [83]: df.groupby('groupcol').mean().values.tolist()
Out[83]:
[[0.04279661,
  0.05974576,
  0.02584746,
  0.00042373,
  0.00042373,
  0.00042373,
  0.00042373,
  0.73771186,
  0.00889831,
  0.12330508],
 [0.23810484,
  0.00020161,
  0.41350806,
  0.2421371,
  0.02237903,
  0.08084677,
  0.00020161,
  0.00221774,
  0.00020161,
  0.00020161]]

关于python - 在 numpy 中对按变量分组的行取平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32945647/

相关文章:

python - tensorflow 多 GPU 训练

python - 运行连接到 Django 测试数据库的 Celery worker

python - 如何在循环中每 x 个步骤创建一个新列表?

python - 如何在 Python 中定义数据框?

pandas - 根据给定的排序顺序列表对 pandas 数据框进行排序

python - 如何规范化 Pandas 的日期

python - 通过排除表来处理 Automap 错误

带代理的 Python httplib.HTTPSConnection

python - 如何用numpy删除数组

python - 临时修复后恢复为随机种子的最佳方法?