# pandas中轴axis的问题理解

``````
import numpy as np
from pandas import Series,DataFrame#导入包
obj = Series(np.arange(5),index = ['a','b','c','d','e'])#创建Series  obj
obj
Out[2]:
a    0
b    1
c    2
d    3
e    4
dtype: int32

new_obj = obj.drop('c')#删除'c'
new_obj
Out[5]:
a    0
b    1
d    3
e    4
dtype: int32

obj
Out[6]:
a    0
b    1
c    2
d    3
e    4
dtype: int32

obj.drop(['b','d'])#删除'b','d'
Out[7]:
a    0
c    2
e    4
dtype: int32

data = DataFrame(np.arange(25).reshape(5,5),index = ['bj','tj','heb','sjz','sh'],columns=['one','two','three','four','five'])#创建DataFrame
data
Out[9]:
one  two  three  four  five
bj     0    1      2     3     4
tj     5    6      7     8     9
heb   10   11     12    13    14
sjz   15   16     17    18    19
sh    20   21     22    23    24

data.drop(['bj','tj'])#删除某行
Out[10]:
one  two  three  four  five
heb   10   11     12    13    14
sjz   15   16     17    18    19
sh    20   21     22    23    24

data.drop('one',axis=1)#删除列
Out[11]:
two  three  four  five
bj     1      2     3     4
tj     6      7     8     9
heb   11     12    13    14
sjz   16     17    18    19
sh    21     22    23    24
``````

• `axis = 0`代表的是行。
• `axis = 1`代表的是列。
这样想就明白了。但是接着问题来了，
``````data
Out[29]:
lie   one  two  three  four  five
city
bj      0    1      2     3     4
tj      5    6      7     8     9
heb    10   11     12    13    14
sjz    15   16     17    18    19
sh     20   21     22    23    24

data.mean(axis = 0)#求列平均
Out[30]:
lie
one      10.0
two      11.0
three    12.0
four     13.0
five     14.0
dtype: float64

data.mean(axis = 1)#求行平均
Out[31]:
city
bj      2.0
tj      7.0
heb    12.0
sjz    17.0
sh     22.0
dtype: float64
``````

`drop`中使用`axis=1`实际上是删掉了一列，而在`mean`中实际上是计算的行平均值，而不是列的平均值。