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

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

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


当前回答

让我们想象一下(你会永远记住),

熊猫:

轴=0表示沿着“索引”。这是一个行运算。

假设,要对dataframe1和dataframe2执行concat()操作, 我们将从dataframe1中取出第一行并放入新的DF中,然后我们从dataframe1中取出另一行并放入新的DF中,我们重复这个过程,直到我们到达dataframe1的底部。然后,我们对dataframe2执行相同的过程。

基本上,将dataframe2堆叠在dataframe1之上,反之亦然。

在桌子或地板上堆一堆书

轴=1表示沿着“列”。这是一个按列的运算。

假设,要对dataframe1和dataframe2执行concat()操作, 我们将取出第一个完整的列(a.k.)。第一个系列)的dataframe1,并放置到新的DF,然后我们拿出dataframe1的第二列,并保持相邻的(侧),我们必须重复这个操作,直到所有列完成。然后,我们在dataframe2上重复相同的过程。 基本上, 横向堆叠dataframe2。

把书摆放在书架上。

更重要的是,与矩阵相比,数组更好地表示嵌套的n维结构!所以下面可以帮助你更直观地看到轴是如何在一维以上的情况下发挥重要作用的。此外,你实际上可以打印/写入/绘制/可视化任何n-dim数组,但在矩阵表示(3-dim)中书写或可视化相同的内容在超过3维的纸张上是不可能的。

其他回答

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=0。

类似地,要计算一行的平均值,特定的行是常数,但它可以遍历不同的列(变化),axis=1。

轴= 0表示从上到下 轴= 1表示从左到右

sums[key] = lang_sets[key].iloc[:,1:].sum(axis=0)

给定的例子是取column == key中所有数据的和。

比如说,如果你用df。然后你将得到一个元组,其中包含数据帧中的行数和列数作为输出。

In [10]: movies_df.shape
Out[10]: (1000, 11)

在上面的例子中,在movies数据帧中有1000行和11列,其中'row'在索引0位置中提到,'column'在索引1位置中提到。因此'axis=1'表示列,'axis=0'表示行。

学分:Github

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