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

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

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


当前回答

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

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

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

其他回答

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

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

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

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

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

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

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

正确使用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.)

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