Python 熊猫中的轴是什么意思?

Python 熊猫中的轴是什么意思?,python,pandas,numpy,dataframe,Python,Pandas,Numpy,Dataframe,以下是生成数据帧的代码: 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|

以下是生成数据帧的代码:

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|
+------------+---------+--------+
然后我得到了数据帧:

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中的轴是什么意思?

它指定了计算平均值的轴。默认情况下,
轴=0
。这与明确指定
numpy.mean
轴时的
numpy.mean
用法一致(在
numpy.mean
中,默认情况下轴==None,它计算展平数组上的平均值),其中沿行(即熊猫中的索引)的
axis=0
,以及沿列的
axis=1
。为了更加清晰,可以选择指定
axis='index'
(而不是
axis=0
)或
axis='columns'
(而不是
axis=1


它指定沿其计算平均值的轴。默认情况下,
轴=0
。这与明确指定
numpy.mean
轴时的
numpy.mean
用法一致(在
numpy.mean
中,默认情况下轴==None,它计算展平数组上的平均值),其中沿行(即熊猫中的索引)的
axis=0
,以及沿列的
axis=1
。为了更加清晰,可以选择指定
axis='index'
(而不是
axis=0
)或
axis='columns'
(而不是
axis=1


熊猫公司的设计师韦斯·麦金尼(Wes McKinney)过去一直致力于金融数据的研究。将列视为股票名称,将指数视为每日价格。然后,您可以猜测该财务数据的默认行为是什么(即,
axis=0
axis=1
可以简单地认为是“另一个方向”

例如,统计函数,如
mean()
sum()
descripe()
count()
都默认为按列操作,因为对每种股票执行这些函数更有意义<代码>排序索引(按=)也默认为列
fillna(method='ffill')
将沿列填充,因为它是相同的库存
dropna()
默认为row,因为您可能只想放弃当天的价格,而不是放弃该股票的所有价格


类似地,方括号索引指的是列,因为选择股票比选择一天更常见。

熊猫的设计师韦斯·麦金尼(Wes McKinney)过去一直致力于金融数据。将列视为股票名称,将指数视为每日价格。然后,您可以猜测该财务数据的默认行为是什么(即,
axis=0
axis=1
可以简单地认为是“另一个方向”

例如,统计函数,如
mean()
sum()
descripe()
count()
都默认为按列操作,因为对每种股票执行这些函数更有意义<代码>排序索引(按=)也默认为列
fillna(method='ffill')
将沿列填充,因为它是相同的库存
dropna()
默认为row,因为您可能只想放弃当天的价格,而不是放弃该股票的所有价格


类似地,方括号索引指的是列,因为选择股票比选择一天更常见。

对于我来说,最容易理解的方法是谈论您是在计算每列(
axis=0
)还是每行(
axis=1
)的统计数据。如果你计算一个统计数据,比如说一个平均值,轴=0,你将得到每列的统计数据。因此,如果每个观察值都是一行,每个变量都在一列中,那么就可以得到每个变量的平均值。如果设置轴=1,则将计算每行的统计数据。在我们的示例中,您将获得所有变量中每个观察值的平均值(可能您需要相关度量值的平均值)

轴=0
:按列=按列=沿行


axis=1
:按行=按行=沿列

我最容易理解的方法是谈论您是在计算每列(
axis=0
)还是每行(
axis=1
)的统计数据。如果你计算一个统计数据,比如说一个平均值,轴=0,你将得到每列的统计数据。因此,如果每个观察值都是一行,每个变量都在一列中,那么就可以得到每个变量的平均值。如果设置轴=1,则将计算每行的统计数据。在我们的示例中,您将获得所有变量中每个观察值的平均值(可能您需要相关度量值的平均值)

轴=0
:按列=按列=沿行


axis=1
:按行=按行=沿列
axis
pd的情况下,指数组的维度。DataFrame
s
axis=0
指向下的维度,
axis=1
指向右的维度

示例:想象一个
ndarray
形状
(3,5,7)

a = np.ones((3,5,7))
a
是一个三维
ndarray
,即它有3个轴(“轴”是“轴”的复数形式)。
a
的配置看起来像3片面包,其中每片面包的尺寸为5×7
a[0,:,:]
将指第0个切片,
a[1,:,:]
将指第1个切片等

a.sum(轴=0)
将沿
a
的第0轴应用
sum()
。您将添加所有切片,最后得到一个形状切片
(5,7)

a.sum(轴=0)
等于

b = np.zeros((5,7))
for i in range(5):
    for j in range(7):
        b[i,j] += a[:,i,j].sum()
b
a.sum(轴=0)
w
import numpy as np

a=np.arange(120).reshape(2,3,4,5)

a.shape
Out[3]: (2, 3, 4, 5)

np.sum(a,axis=0).shape
Out[4]: (3, 4, 5)

np.sum(a,axis=1).shape
Out[5]: (2, 4, 5)

np.sum(a,axis=2).shape
Out[6]: (2, 3, 5)

np.sum(a,axis=3).shape
Out[7]: (2, 3, 4)
sums[key] = lang_sets[key].iloc[:,1:].sum(axis=0)
 a = np.ones((3,5,7))
    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.]])
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
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,)
+------------+---------+--------+
|            |  A      |  B     |
+------------+---------+---------
|      X     | 0.626386| 1.52325|
+------------+---------+--------+
|      Y     | 0.626386| 1.52325|
+------------+---------+--------+
+------------+---------+--------+
|            |  A      |  B     |
|            | 0.626386| 1.52325|  
|            | 0.626386| 1.52325|
+------------+
|      X     |
+------------+
|      Y     |
+------------+
+------------+---------+
|      X     | 0.626386
+------------+---------+
|      Y     | 0.626386
+------------+---------+
+------------+---------+
|      X     | 1.52325 |
+------------+---------+
|      Y     | 1.52325 |
+------------+---------+
In [10]: movies_df.shape
Out[10]: (1000, 11)