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

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

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


当前回答

熊猫的设计师韦斯•麦金尼(Wes McKinney)曾大量从事金融数据方面的工作。将列视为股票名称,将指数视为每日价格。然后,您可以猜测关于此财务数据的默认行为是什么(即,axis=0)。Axis =1可以简单地认为是“另一个方向”。

例如,诸如mean()、sum()、describe()、count()等统计函数都默认按列执行,因为对每只股票执行这些函数更有意义。Sort_index (by=)也默认为column。Fillna (method='ffill')将沿着列填充,因为它是相同的股票。Dropna()默认为row,因为您可能只是想丢弃当天的价格,而不是丢弃该股票的所有价格。

类似地,方括号索引指的是列,因为更常见的是选择股票而不是选择日期。

其他回答

在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对象。

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

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

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

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

这是基于@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.]])

The easiest way for me to understand is to talk about whether you are calculating a statistic for each column (axis = 0) or each row (axis = 1). If you calculate a statistic, say a mean, with axis = 0 you will get that statistic for each column. So if each observation is a row and each variable is in a column, you would get the mean of each variable. If you set axis = 1 then you will calculate your statistic for each row. In our example, you would get the mean for each observation across all of your variables (perhaps you want the average of related measures).

轴= 0:按列=按列=沿行

轴= 1:按行=按行=沿列

数组被设计为坐标轴=0,行被垂直放置,而坐标轴=1,列被水平放置。Axis指的是数组的尺寸。