下面是我生成一个数据框架的代码:

import pandas as pd
import numpy as np

dff = pd.DataFrame(np.random.randn(1,2),columns=list('AB'))

然后我得到了数据框架:

+------------+---------+--------+
|            |  A      |  B     |
+------------+---------+---------
|      0     | 0.626386| 1.52325|
+------------+---------+--------+

当我输入命令时:

dff.mean(axis=1)

我得到:

0    1.074821
dtype: float64

根据pandas的参考,axis=1代表列,我希望命令的结果是

A    0.626386
B    1.523255
dtype: float64

我的问题是:轴在熊猫中是什么意思?


当前回答

这里的许多答案对我帮助很大!

如果你对Python中的axis和R中的MARGIN的不同行为感到困惑(比如在apply函数中),你可以找到我写的一篇感兴趣的博客文章:https://accio.github.io/programming/2020/05/19/numpy-pandas-axis.html。

从本质上讲:

Their behaviours are, intriguingly, easier to understand with three-dimensional array than with two-dimensional arrays. In Python packages numpy and pandas, the axis parameter in sum actually specifies numpy to calculate the mean of all values that can be fetched in the form of array[0, 0, ..., i, ..., 0] where i iterates through all possible values. The process is repeated with the position of i fixed and the indices of other dimensions vary one after the other (from the most far-right element). The result is a n-1-dimensional array. In R, the MARGINS parameter let the apply function calculate the mean of all values that can be fetched in the form of array[, ... , i, ... ,] where i iterates through all possible values. The process is not repeated when all i values have been iterated. Therefore, the result is a simple vector.

其他回答

我的想法是:Axis = n,其中n = 0,1等意味着矩阵沿该轴折叠(折叠)。所以在一个二维矩阵中,当你沿着0(行)折叠时,你实际上是一次对一列进行操作。对于高阶矩阵也是如此。

这与对矩阵中维数的正常引用不同,其中0 ->行和1 ->列。对于N维数组中的其他维度也是如此。

我将明确避免使用“行-wise”或“沿列”,因为人们可能会以完全错误的方式解释它们。

类比。直观地,你会期望pandas. datafframe .drop(axis='column')从N个列中删除一个列,并给出(N - 1)个列。所以你现在可以不关注rows(并从你的英语字典中删除row这个单词)。反之亦然,drop(axis='row')作用于行。

以同样的方式,sum(axis='column')在多个列上工作,并给出1列。类似地,sum(axis='row')的结果为1行。这与其最简单的定义形式是一致的,即将一组数字简化为一个数字。

一般来说,使用axis=column,您可以看到列,处理列,并获得列。忘记行。

使用axis=row,改变视角并在行上工作。

0和1只是“行”和“列”的别名。这是矩阵下标的惯例。

有一件重要的事情要记住,当你使用均值、中值等函数时,你基本上是在做numpy聚合。可以将聚合看作是获得最终的单个输出,该输出可以是列输出、行输出,也可以是整个数据集的单个数字。

当我们说数组中的聚合时,用numpy。Sum (data, axis = 0),我们真正的意思是我们想要删除这个特定的轴(这里是0轴)。

示例:对于这个特定的数据集,如果我们通过axis = 0计算和,我们实际上对删除(聚集)零轴感兴趣。一旦我们移除零轴,沿着零轴的聚合将导致[1,4,3]等于8,[2,3,6]等于11,[5,7,9]等于21。类似的逻辑可以扩展到axis = 1。

对于drop, concat和其他一些函数,我们实际上不是 聚合结果。

我用于直觉的心智模型:

假设当轴= 0时,我们在第一列的每个单元格中放置了袋鼠/青蛙;如果轴= 1,则沿着第一行放置了袋鼠/青蛙。

情况:轴= 0时

把加绿色的形状想象成青蛙。

轴0表示沿着行移动

Sum:假设我们正在计算Sum,那么首先它们将计算它们的位置(r1c1, r2c1, r3c1)[1,4,3] =[8]的和。然后它们的下一个移动也是沿着轴为0的那一行。他们的新位置在下一张图片中(下图)。

删除:如果在一行中它们遇到(r1c1, r2c1, r3c1)中的任何NaN,它们将删除对应的行,因为axis = 0

求和:现在,它们将计算它们的位置(r1c2, r2c2, r3c2)[2,3,6] =[11]的和,类似地,它们将沿着行向前移动一步,并计算第三列[21]的和。

删除:如果在一行中它们遇到(r1c2, r2c2, r3c2)中的任何NaN,它们将在axis = 0时删除相应的行。类似的逻辑可以扩展到不同的轴和额外的行/列。

这里的许多答案对我帮助很大!

如果你对Python中的axis和R中的MARGIN的不同行为感到困惑(比如在apply函数中),你可以找到我写的一篇感兴趣的博客文章:https://accio.github.io/programming/2020/05/19/numpy-pandas-axis.html。

从本质上讲:

Their behaviours are, intriguingly, easier to understand with three-dimensional array than with two-dimensional arrays. In Python packages numpy and pandas, the axis parameter in sum actually specifies numpy to calculate the mean of all values that can be fetched in the form of array[0, 0, ..., i, ..., 0] where i iterates through all possible values. The process is repeated with the position of i fixed and the indices of other dimensions vary one after the other (from the most far-right element). The result is a n-1-dimensional array. In R, the MARGINS parameter let the apply function calculate the mean of all values that can be fetched in the form of array[, ... , i, ... ,] where i iterates through all possible values. The process is not repeated when all i values have been iterated. Therefore, the result is a simple vector.

让我们看看Wiki上的表格。这是国际货币基金组织对2010年至2019年前十大国家GDP的估计。

1. 如果你想计算每个国家过去十年(2010-2019)的平均GDP,你需要做,df.mean(轴=1)。例如,如果你想计算美国从2010年到2019年的平均GDP, df。loc['美国',' 2010 ':' 2019 '].mean(轴= 1)

2. 如果我想计算所有国家每年的平均GDP(平均值),你需要做,df.mean(轴=0)。例如,如果你想计算2015年美国、中国、日本、德国和印度的平均GDP, df。loc(“美国”:“印度”,' 2015 '].mean(轴= 0) 注意:上述代码只有在使用set_index方法将“国家(或附属领土)”列设置为索引后才能工作。