Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (50.2k points)

I understand that to drop a column you use df.drop('column name', axis=1). Is there a way to drop a column using a numerical index instead of the column name?

1 Answer

0 votes
by (108k points)

 If you want to drop multiple columns like this:

cols = [1,2,4,5,12]

df.drop(df.columns[cols],axis=1,inplace=True)

inplace=True is used so that it can make the changes in the dataframe itself without doing the column dropping on a copy of the data frame. If you need to keep your original intact, refer the following code:

df_after_dropping = df.drop(df.columns[cols],axis=1)

If you do not work with inplace=True then you will have to do df = df.drop(

If you are interested in learning Python and want to become a certified Python expert, then check out Python Courses and yourself.upskill

 

Related questions

0 votes
1 answer
0 votes
1 answer
asked Aug 24, 2019 in Data Science by sourav (17.6k points)

Browse Categories

...