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

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维数组中的其他维度也是如此。

有一件重要的事情要记住,当你使用均值、中值等函数时,你基本上是在做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时删除相应的行。类似的逻辑可以扩展到不同的轴和额外的行/列。

我认为,正确答案应该是“这很复杂”。

“轴”这个词本身在不同的人心中会产生不同的形象 假设y轴,它应该是一个垂直的图像。但是,现在考虑一条垂直线x=0。这也是一条垂直线,但是x轴上的值为0。

类似地,当我们说axis='index'(意思是axis=0)时,我们说的是索引所在的“垂直”方向吗?或者由索引值处理的一系列数据?熊猫往往意味着第一个意思,垂直方向。

熊猫本身也不是100%一致的,看看下面的例子,它们几乎有相同的共同主题:

# [1] piling dfs
pd.concat([df0, df1], axis='index') 
    # adding dfs on top of each other vertically like pilling up a column, 
    # but, we will use the word 'index'

# [2] for every column in df: operate on it
df.apply(foo, axis='index')
df.mean('A', axis='index')
a_boolean_df.all(axis='index')
    # apply an operation to a vertical slice of data, ie. a column, 
    # then apply the same operation to the next column on the right 
    # then to the right again... until the last column
    # but, we will use the word 'index'

# [3] delete a column or row of data
df.drop(axis='index', ...)
df.dropna(axis='index', ...)
    # this time, we are droping an index/row, a horizontal slice of data.
    # so OBVIOUSLY we will use the word 'index'

# [4] drop duplicate
df.drop_duplicates(subset=['mycolumn0', 'mycolumn1']...)
    # thank God we don't need to deal with the "axis" bs in this

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

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,)。

axis=1,它将给出行和,keepdims=True将保持2D维度。 希望对你有所帮助。