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

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

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


当前回答

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

其他回答

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

我以前也很困惑,但我记得是这样的。

它指定将更改的数据帧的维度,或者将在其上执行操作。

让我们通过一个例子来理解这一点。 我们有一个数据框架df,它的形状是(5,10),这意味着它有5行10列。

现在,当我们使用df。mean(axis=1)时,它意味着维数1将被改变,这意味着它将有相同的行数,但不同的列数。因此得到的结果将是(5,1)的形状。

类似地,如果我们使用df.mean(axis=0),这意味着维度0将被改变,这意味着行数将被改变,但列数将保持不变,因此结果将是形状(1,10)。

试着把这个和问题中提供的例子联系起来。

正确使用axis=的问题在于它在两种主要不同情况下的使用:

用于计算累积值或重新排列(如排序)数据。 用于操纵(“玩”)实体(例如数据帧)。

这个答案背后的主要思想是为了避免混淆,我们选择一个数字或一个名称来指定特定的轴,以更清楚、直观和描述性的为准。

Pandas基于NumPy, NumPy基于数学,特别是n维矩阵。下面是三维空间中数学中常用的轴的名称:

这张图仅用于记忆坐标轴的序数:

x轴为0, y轴为1,和 z轴为2。

z轴仅用于面板;对于数据框架,我们将把我们的兴趣限制在带有x轴(0,垂直)和y轴(1,水平)的绿色二维基本平面上。

这都是关于axis= parameter的潜在值的数字。

轴的名称是“索引”(你可以使用别名“行”)和“列”,为了解释这些名称和(轴的)序数之间的关系并不重要,因为每个人都知道“行”和“列”是什么意思(这里的每个人-我想-都知道“索引”在pandas中的意思)。

现在,我的建议是

If you want to compute an accumulated value, you may compute it from values located along axis 0 (or along axis 1) — use axis=0 (or axis=1). Similarly, if you want to rearrange values, use the axis number of the axis, along which are located data for rearranging (e.g. for sorting). If you want to manipulate (e.g. concatenate) entities (e.g. dataframes) — use axis='index' (synonym: axis='rows') or axis='columns' to specify the resulting change — index (rows) or columns, respectively. (For concatenating, you will obtain either a longer index (= more rows), or more columns, respectively.)

我对熊猫还是个新手。但这是我对熊猫轴的理解:


恒变方向


0列行向下|


1行列向右——>


所以要计算一列的均值,这一列应该是常数,但它下面的行可以改变(变化)所以它是axis=0。

类似地,要计算一行的平均值,特定的行是常数,但它可以遍历不同的列(变化),axis=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.