python - DataFrames - 平均列

标签 python pandas numpy dataframe

我在 Pandas 中有以下数据框

Column 1   Column 2  Column3   Column 4
   2           2         2         4
   1           2         2         3

我希望创建一个数据框,其中包含第 1 列和第 2 列、第 3 列和第 4 列等的平均值。

  ColumnAvg(12)      ColumnAvg(34)
      2                   3
      1.5                 1.5

我用的是这个,但它正在对所有内容进行平均。

df.mean(axis=1)

有什么方法可以在对每一行进行平均时添加列标题。 如果没有,另一种方法是创建两个数组,对它们进行平均,然后创建一个新的数据框。

最佳答案

你用 axisgroupby 并传递列表

out = df.groupby([1,1,2,2],axis=1).mean()
     1    2
0  2.0  3.0
1  1.5  2.5

关于python - DataFrames - 平均列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63974864/

相关文章:

python - 数据框中基于另一列中值的位置的新列

python - 了解scrapy框架架构

python - 通过对 pandas 数据框中的单个列进行分组来创建新列

python - 将整数列表作为参数并返回运行总计列表的函数

python - Panda 相当于 R 的 order(),用于排列数据框列

python - numpy-->PIL int 类型问题

python-3.x - 如何在 pandas 上应用双总和分组?

python - 使用 MRMR 进行特征选择

python - 这是创建 numpy 数组的只读 View 的正确方法吗?

python - 循环 clump_masked 索引