axis=0 与 axis=1 的区分

官方帮助的解释:

轴用来为超过一维数组定义的属性,二维数据拥有两个轴:第0轴沿着行的方向垂直向下,第1轴沿着列的方向水平延申。

根据官方的说法,1表示横轴,方向从左到右;0表示纵轴,方向从上到下。当axis=1时,数组的变化是横向的,体现出列的增加或者减少。反之,当axis=0时,数组的变化是纵向的,体现出行的增加或减少。

下图为dataframe中axis为0和1时的图示:

                                  

实例:

df = pd.DataFrame([[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]],
                  columns=['col0','col1','col2','col3'])
>>>df

               

df.mean(axis=1)
>>>
0    1.0
1    2.0
2    3.0
dtype: float64

df.mean(axis=0)
>>>
col0    2.0
col1    2.0
col2    2.0
col3    2.0
dtype: float64

df.drop('col2',axis=1)
>>>

         

df.drop(0,axis=0)
>>>

        

用法:DataFrame.drop(labels=None,axis=0, index=None, columns=None, inplace=False)

所以,axis的重点在于方向,而不是行和列,具体体现到各种用法也是如此。

转载于:https://blog.csdn.net/brucewong0516/article/details/79030994

np.arange(24).reshape(2,3,4)
>>>
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])

#生成面板数据
c = pd.Panel(np.arange(24).reshape(2,3,4))
c
<class 'pandas.core.panel.Panel'>
Dimensions: 2 (items) x 3 (major_axis) x 4 (minor_axis)
Items axis: 0 to 1
Major_axis axis: 0 to 2
Minor_axis axis: 0 to 3

#对Items axis轴的数据进行操作,也就是panel里面的0轴:
c.sum(axis = 0)
>>>
    0   1   2   3
0  12  14  16  18
1  20  22  24  26
2  28  30  32  34

#对Major_axis axis轴的数据进行操作
c.sum(axis = 1)
>>>
    0   1
0  12  48
1  15  51
2  18  54
3  21  57

#对Minor_axis axis轴的数据进行操作
c.sum(axis = 2)
>>>
    0   1
0   6  54
1  22  70
2  38  86

 

转载于:https://blog.csdn.net/brucewong0516/article/details/79030994

猜你喜欢

转载自blog.csdn.net/guoyang768/article/details/84818774