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

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

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


当前回答

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

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

其他回答

我认为还有另一种理解方式。

对于np。数组,如果我们想要消除列,我们使用axis = 1;如果我们想消除行,我们使用axis = 0。

np.mean(np.array(np.ones(shape=(3,5,10))),axis = 0).shape # (5,10)
np.mean(np.array(np.ones(shape=(3,5,10))),axis = 1).shape # (3,10)
np.mean(np.array(np.ones(shape=(3,5,10))),axis = (0,1)).shape # (10,)

对于pandas对象,axis = 0表示按行操作,axis = 1表示按列操作。这与numpy的定义不同,我们可以检查numpy.doc和pandas.doc的定义

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

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

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

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

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

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

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

我将明确避免使用“行-wise”或“沿列”,因为人们可能会以完全错误的方式解释它们。

类比。直观地,你会期望pandas. datafframe .drop(axis='column')从N个列中删除一个列,并给出(N - 1)个列。所以你现在可以不关注rows(并从你的英语字典中删除row这个单词)。反之亦然,drop(axis='row')作用于行。

以同样的方式,sum(axis='column')在多个列上工作,并给出1列。类似地,sum(axis='row')的结果为1行。这与其最简单的定义形式是一致的,即将一组数字简化为一个数字。

一般来说,使用axis=column,您可以看到列,处理列,并获得列。忘记行。

使用axis=row,改变视角并在行上工作。

0和1只是“行”和“列”的别名。这是矩阵下标的惯例。

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

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