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

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

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


当前回答

实际上我们不需要记住轴=0轴=1代表什么。 有时,axis可以是一个元组:例如axis=(0,1)我们如何理解这样多个dim轴?

我发现如果我们理解python slice[:]是如何工作的,就会更容易。

假设我们有一个一维数组: A = [0,1,0]

a[:] # select all the elements in array a

假设我们有一个2d数组:

M = [[0, 0, 1],
     [1, 0, 0],
     [0, 2, 1],
     [2, 0, 2],
     [3, 1, 0]]
M[1,:] # M[0]=1, M[1]=* --> [1, 0, 0]
M[:,2] # M[0]=*, M[1]=2 --> [1, 0, 1, 2, 0]
M[:,:] # M[0]=*, M[1]=* --> all the elements in M are selected

当计算时:

np.sum(M, axis=0) # [sum(M[:,0]), sum(M[:,1]), sum(M[:,2])]
np.sum(M, axis=1) # [sum(M[0,:]), sum(M[1,:]), sum(M[2,:]), sum(M[3,:]), sum(M[4,:])]
np.sum(M, axis=-1) # -1 means last dim, it's the same with np.sum(M, axis=1)
np.sum(M, axis=(0,1)) # sum(M[:,:])

规则很简单,当计算时将axis中指定的暗值替换为:。

其他回答

它指定了计算平均值的轴。默认情况下axis=0。这与numpy一致。显式指定axis时的平均使用量(在numpy中)。mean, axis==None,默认情况下,它计算扁平数组上的平均值),其中,沿行轴=0(即,以pandas为单位的索引),沿列轴=1。为了增加清晰度,可以选择指定axis='index'(而不是axis=0)或axis='columns'(而不是axis=1)。

+------------+---------+--------+
|            |  A      |  B     |
+------------+---------+---------
|      0     | 0.626386| 1.52325|----axis=1----->
+------------+---------+--------+
             |         |
             | axis=0  |
             ↓         ↓

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

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

让我们看看Wiki上的表格。这是国际货币基金组织对2010年至2019年前十大国家GDP的估计。

1. 如果你想计算每个国家过去十年(2010-2019)的平均GDP,你需要做,df.mean(轴=1)。例如,如果你想计算美国从2010年到2019年的平均GDP, df。loc['美国',' 2010 ':' 2019 '].mean(轴= 1)

2. 如果我想计算所有国家每年的平均GDP(平均值),你需要做,df.mean(轴=0)。例如,如果你想计算2015年美国、中国、日本、德国和印度的平均GDP, df。loc(“美国”:“印度”,' 2015 '].mean(轴= 0) 注意:上述代码只有在使用set_index方法将“国家(或附属领土)”列设置为索引后才能工作。

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

axis=1,它将给出行和,keepdims=True将保持2D维度。 希望对你有所帮助。