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

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

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


当前回答

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

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

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

其他回答

我是这样理解的:

比如说,如果你的操作需要在数据框架中从左到右/从右到左,你显然是在合并列。你在不同的列上操作。 这是轴=1

例子

df = pd.DataFrame(np.arange(12).reshape(3,4),columns=['A', 'B', 'C', 'D'])
print(df)
   A  B   C   D
0  0  1   2   3
1  4  5   6   7
2  8  9  10  11 

df.mean(axis=1)

0    1.5
1    5.5
2    9.5
dtype: float64

df.drop(['A','B'],axis=1,inplace=True)

    C   D
0   2   3
1   6   7
2  10  11

这里需要注意的是,我们是在列上操作

类似地,如果您的操作需要在数据帧中从上到下/从下到上遍历,那么您正在合并行。轴为0。

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

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

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

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

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

学分:Github

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=的问题在于它在两种主要不同情况下的使用:

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

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

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