关于drop函数的用法

DataFrame.drop(self,labels = None,axis = 0,index = None,columns = None,level = None,inplace = False,errors ='raise' )

通过指定标签名称和轴,或者直接指定索引或列名称来直接删除行或列。

常用参数含义:

labels : 标签表示索引或列

axis : 指定轴,axis = 0(删除行) axis = 1(删除列)

index : 索引(行) labels, axis=0相当于index=labels

columns : 列 labels, axis=1相当于columns=labels

inplace :布尔类型,默认值为false。采用inplace=True之后,原数组名对应的内存值直接改变

Example

>>> df = pd.DataFrame(np.arange(12).reshape(3, 4),columns=['A', 'B', 'C', 'D'])
>>> df
A B C D
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11

删除行

>>> df.drop(index = 0, axis = 0)
A B C D
1 4 5 6 7
2 8 9 10 11

上述axis = 0 可去掉,同样也可写成

>>> df.drop(labels = 0, axis = 0)
A B C D
1 4 5 6 7
2 8 9 10 11

删除列

>>> df.drop(columns = 'A')

等同于

>>> df.drop(labels = 'A', axis = 1)
B C D
0 1 2 3
1 5 6 7
2 9 10 11