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

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

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


当前回答

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

其他回答

在Pandas上有两种最常见的axis用法:

用作索引,如df。iloc [0, 1] 用作函数内的参数,如df.mean(axis=1)

当使用作为索引时,我们可以解释为axis=0代表行,axis=1代表列,即df。iloc(行、列)。所以,df。Iloc[0,1]表示从第0行和第1列中选择数据,在本例中,它返回1.52325。

当使用作为参数时,axis=0表示垂直跨行选择对象,而axis=1表示水平跨列选择对象。

因此,df.mean(axis=1)表示水平计算跨列的平均值,它返回:

0    1.074821
dtype: float64

轴的一般用途是用于选择要操作的特定数据。而理解轴的关键,是把“选择”和“操作”的过程分开。

我们用一种额外的情况来解释:df。下降(A轴= 1)

该操作是df.drop(),它需要目标对象的名称 列,在这里是A。它和df。mean()不一样 对数据内容进行操作。 选择的是列的名称,而不是列的数据内容。由于所有列名都是水平排列在列之间的,所以我们使用axis=1来选择name对象。

总之,我们最好把“选择”和“操作”分开,对以下问题有一个清晰的认识:

选择什么对象 是怎么安排的

实际上我们不需要记住轴=0轴=1代表什么。 有时,axis可以是一个元组:例如axis=(0,1)我们如何理解这样多个dim轴?

我发现如果我们理解python slice[:]是如何工作的,就会更容易。

假设我们有一个一维数组: A = [0,1,0]

a[:] # select all the elements in array a

假设我们有一个2d数组:

M = [[0, 0, 1],
     [1, 0, 0],
     [0, 2, 1],
     [2, 0, 2],
     [3, 1, 0]]
M[1,:] # M[0]=1, M[1]=* --> [1, 0, 0]
M[:,2] # M[0]=*, M[1]=2 --> [1, 0, 1, 2, 0]
M[:,:] # M[0]=*, M[1]=* --> all the elements in M are selected

当计算时:

np.sum(M, axis=0) # [sum(M[:,0]), sum(M[:,1]), sum(M[:,2])]
np.sum(M, axis=1) # [sum(M[0,:]), sum(M[1,:]), sum(M[2,:]), sum(M[3,:]), sum(M[4,:])]
np.sum(M, axis=-1) # -1 means last dim, it's the same with np.sum(M, axis=1)
np.sum(M, axis=(0,1)) # sum(M[:,:])

规则很简单,当计算时将axis中指定的暗值替换为:。

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

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

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

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

记住轴1(列)与轴0(行)的简单方法之一是您期望的输出。

如果你希望每行都有输出,使用axis='columns', 另一方面,如果你想为每一列输出,你可以使用axis='rows'。