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

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

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


当前回答

轴在编程中是形状元组中的位置。这里有一个例子:

import numpy as np

a=np.arange(120).reshape(2,3,4,5)

a.shape
Out[3]: (2, 3, 4, 5)

np.sum(a,axis=0).shape
Out[4]: (3, 4, 5)

np.sum(a,axis=1).shape
Out[5]: (2, 4, 5)

np.sum(a,axis=2).shape
Out[6]: (2, 3, 5)

np.sum(a,axis=3).shape
Out[7]: (2, 3, 4)

轴上的均值将导致该维度被移除。

参考原题,dff形状为(1,2)。使用axis=1将形状更改为(1,)。

其他回答

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

如果你对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.

这是基于@Safak的回答。 理解pandas/numpy中的轴的最好方法是创建一个3d数组,并沿着3个不同的轴检查求和函数的结果。

 a = np.ones((3,5,7))

A将是:

    array([[[1., 1., 1., 1., 1., 1., 1.],
    [1., 1., 1., 1., 1., 1., 1.],
    [1., 1., 1., 1., 1., 1., 1.],
    [1., 1., 1., 1., 1., 1., 1.],
    [1., 1., 1., 1., 1., 1., 1.]],

   [[1., 1., 1., 1., 1., 1., 1.],
    [1., 1., 1., 1., 1., 1., 1.],
    [1., 1., 1., 1., 1., 1., 1.],
    [1., 1., 1., 1., 1., 1., 1.],
    [1., 1., 1., 1., 1., 1., 1.]],

   [[1., 1., 1., 1., 1., 1., 1.],
    [1., 1., 1., 1., 1., 1., 1.],
    [1., 1., 1., 1., 1., 1., 1.],
    [1., 1., 1., 1., 1., 1., 1.],
    [1., 1., 1., 1., 1., 1., 1.]]])

现在检查数组中每个轴上元素的和:

 x0 = np.sum(a,axis=0)
 x1 = np.sum(a,axis=1)
 x2 = np.sum(a,axis=2)

会给你以下结果:

   x0 :
   array([[3., 3., 3., 3., 3., 3., 3.],
        [3., 3., 3., 3., 3., 3., 3.],
        [3., 3., 3., 3., 3., 3., 3.],
        [3., 3., 3., 3., 3., 3., 3.],
        [3., 3., 3., 3., 3., 3., 3.]])

   x1 : 
   array([[5., 5., 5., 5., 5., 5., 5.],
   [5., 5., 5., 5., 5., 5., 5.],
   [5., 5., 5., 5., 5., 5., 5.]])

  x2 :
   array([[7., 7., 7., 7., 7.],
        [7., 7., 7., 7., 7.],
        [7., 7., 7., 7., 7.]])

它指定了计算平均值的轴。默认情况下axis=0。这与numpy一致。显式指定axis时的平均使用量(在numpy中)。mean, axis==None,默认情况下,它计算扁平数组上的平均值),其中,沿行轴=0(即,以pandas为单位的索引),沿列轴=1。为了增加清晰度,可以选择指定axis='index'(而不是axis=0)或axis='columns'(而不是axis=1)。

+------------+---------+--------+
|            |  A      |  B     |
+------------+---------+---------
|      0     | 0.626386| 1.52325|----axis=1----->
+------------+---------+--------+
             |         |
             | axis=0  |
             ↓         ↓

我将明确避免使用“行-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只是“行”和“列”的别名。这是矩阵下标的惯例。

在过去的一个小时里,我也一直在试着求出坐标轴。上述所有答案中的语言,以及文档都没有任何帮助。

要回答我现在理解的问题,在Pandas中,axis = 1或0意味着在应用函数时希望保持哪个轴头不变。

注意:当我说标题时,我指的是索引名

扩展你的例子:

+------------+---------+--------+
|            |  A      |  B     |
+------------+---------+---------
|      X     | 0.626386| 1.52325|
+------------+---------+--------+
|      Y     | 0.626386| 1.52325|
+------------+---------+--------+

对于axis=1=columns:我们保持列标题不变,并通过改变数据应用平均值函数。 为了演示,我们保持列标题为常量:

+------------+---------+--------+
|            |  A      |  B     |

现在我们填充A和B值的一个集合,然后找到平均值

|            | 0.626386| 1.52325|  

然后我们填充下一组A和B值,并找到平均值

|            | 0.626386| 1.52325|

类似地,对于axis=rows,我们保持行标题不变,并不断更改数据: 为了演示,首先修复行标题:

+------------+
|      X     |
+------------+
|      Y     |
+------------+

现在填充第一组X和Y值,然后求平均值

+------------+---------+
|      X     | 0.626386
+------------+---------+
|      Y     | 0.626386
+------------+---------+

然后填充下一组X和Y值,然后找到平均值:

+------------+---------+
|      X     | 1.52325 |
+------------+---------+
|      Y     | 1.52325 |
+------------+---------+

总之,

当axis=columns时,将修复列标题并更改数据,这些数据将来自不同的行。

当axis=rows时,您将修复行标题并更改数据,这些数据将来自不同的列。