Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

Look at the following code:

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

Could you kindly clarify how does number 1 does? 

From the documentation, I comprehend that DataFrame.drop function drops the wanted column named 'label' from the dataframe and returns a new dataframe without this column. Be that as it may, I don't comprehend what does this specific number parameter 1 does.

1 Answer

0 votes
by (26.4k points)

It is actually the parameter axis in drop. It is equivalent to axis=1. Also, it implies you need to eliminate columns from DataFrame which are indicated in the first parameter labels

labels are discarded on most occasions. 

The parameter axis can also be eliminated if need eliminates the row with its index, in light of the fact that naturally axis=0. Parameter axis=1 is at times supplanted by 1, because that less text, yet it is worse readable.

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

Join the python online course fast, to learn python concepts in detail and get certified.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...