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

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

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


当前回答

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

“轴”这个词本身在不同的人心中会产生不同的形象 假设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

其他回答

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

熊猫:

轴=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维的纸张上是不可能的。

这些答案确实有助于解释这一点,但对于非程序员(例如,像我这样第一次在数据科学课程背景下学习Python的人)来说,它仍然不是完全直观的。我仍然发现使用术语“沿着”或“每个”wrt的行和列是令人困惑的。

对我来说更有意义的是这样说:

轴0将作用于每个COLUMN中的所有row 轴1将作用于每个ROW中的所有COLUMNS

0轴上的均值是每列中所有行的均值,1轴上的均值是每行中所有列的均值。

从根本上说,这和@zhangxaochen和@Michael的意思是一样的,只是用一种更容易让我内化的方式。

我的想法是:Axis = n,其中n = 0,1等意味着矩阵沿该轴折叠(折叠)。所以在一个二维矩阵中,当你沿着0(行)折叠时,你实际上是一次对一列进行操作。对于高阶矩阵也是如此。

这与对矩阵中维数的正常引用不同,其中0 ->行和1 ->列。对于N维数组中的其他维度也是如此。

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:按行=按行=沿列

Axis指的是数组的维度,在pd的情况下。DataFrames轴=0是指向下方的维度,轴=1是指向右侧的维度。

示例:考虑一个形状为(3,5,7)的ndarray。

a = np.ones((3,5,7))

A是一个三维ndarray,即它有3个轴(“axis”是“axis”的复数)。a的构型看起来就像3片面包每片的尺寸都是5乘7。A[0,:,:]表示第0个切片,A[1,:,:]表示第1个切片,等等。

a.s sum(axis=0)将沿着a的第0个轴应用sum()。你将添加所有的切片,最终得到一个形状(5,7)的切片。

a.s sum(axis=0)等价于

b = np.zeros((5,7))
for i in range(5):
    for j in range(7):
        b[i,j] += a[:,i,j].sum()

B和a.sum(轴=0)看起来都是这样的

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.]])

在警局里。DataFrame,轴的工作方式与numpy相同。数组:axis=0将对每一列应用sum()或任何其他约简函数。

注意:在@zhangxaochen的回答中,我发现“沿着行”和“沿着列”这两个短语有点让人困惑。Axis =0表示“沿每列”,Axis =1表示“沿每行”。