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

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

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

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

其他回答

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

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

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

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

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

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

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表示从左到右

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

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

这里的许多答案对我帮助很大!

如果你对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.

熊猫的设计师韦斯•麦金尼(Wes McKinney)曾大量从事金融数据方面的工作。将列视为股票名称,将指数视为每日价格。然后,您可以猜测关于此财务数据的默认行为是什么(即,axis=0)。Axis =1可以简单地认为是“另一个方向”。

例如,诸如mean()、sum()、describe()、count()等统计函数都默认按列执行,因为对每只股票执行这些函数更有意义。Sort_index (by=)也默认为column。Fillna (method='ffill')将沿着列填充,因为它是相同的股票。Dropna()默认为row,因为您可能只是想丢弃当天的价格,而不是丢弃该股票的所有价格。

类似地,方括号索引指的是列,因为更常见的是选择股票而不是选择日期。