Peter Babinec Peter Babinec - 2 months ago 8
Python Question

Integer parameter in pandas DataFrame.drop function

In following line of code

X = np.array(df.drop(['label'], 1))


Could you please explain what does number
1
do?

From documentation I understand that
DataFrame.drop
function drops desired column named
'label'
from dataframe and returns new dataframe without this column. But I dont understand what does this particular integer parameter
1
do.

Answer

It is parameter axis in drop. It is same as axis=1. And it means you need remove columns from DataFrame which are specify in first parameter labels:

labels is omited most times.
Parameter axis can be removed if need remove row with index, because by default axis=0. Parameter axis=1 is sometimes replaced by 1, because less text, but it is worse readable.

Sample:

import pandas as pd

df = pd.DataFrame({'label':[1,2,3],
                   'label1':[4,5,6],
                   'label2':[7,8,9]})

print (df)
   label  label1  label2
0      1       4       7
1      2       5       8
2      3       6       9

print (df.drop(['label'], 1))
   label1  label2
0       4       7
1       5       8
2       6       9

#most commonly used
print (df.drop(['label'], axis=1))
   label1  label2
0       4       7
1       5       8
2       6       9

print (df.drop(labels=['label'], axis=1))
   label1  label2
0       4       7
1       5       8
2       6       9
Comments