how to drop columns in pandas

Do you want to know the details about “how to drop columns in pandas”. If yes, you’re in the correct article.

how to drop columns in pandas

#To delete the column without having to reassign df
df.drop('column_name', axis=1, inplace=True) 
# Let df be a dataframe
# Let new_df be a dataframe after dropping a column

new_df = df.drop(labels="column_name", axis=1)

# Or if you don't want to change the name of the dataframe
df = df.drop(labels="column_name", axis=1)

# Or to remove several columns
df = df.drop(['list_of_column_names'], axis=1)

# axis=0 for 'rows' and axis=1 for columns
note: df is your dataframe

df = df.drop('coloum_name',axis=1)
# Import pandas package
import pandas as pd

# create a dictionary with five fields each
data = 
'A':['A1', 'A2', 'A3', 'A4', 'A5'],
'B':['B1', 'B2', 'B3', 'B4', 'B5'],
'C':['C1', 'C2', 'C3', 'C4', 'C5'],
'D':['D1', 'D2', 'D3', 'D4', 'D5'],
'E':['E1', 'E2', 'E3', 'E4', 'E5'] 

# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
#drop the 'A' column from your dataframe df 
df.drop(['A'],axis=1,inplace=True)
df

#-->df contains 'B','C','D' and 'E'
#in this example you will change your dataframe , if you don't want to ,
#just remove the in place parameter and assign your result to an other variable 

df1=df.drop(['B'],axis=1)
#-->df1 contains 'C','D','E'
df1
df = df.drop(['B', 'C'], axis=1)
df.drop(['colonna da togliere'], axis=1)

Final Thoughts

I hope this tutorial helps you to know about “how to drop columns in pandas”. If you have any queries regarding this article please let us know via the comment section. Share this post with your friends & family via social networks.

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coder Diksha. Here, I post about programming to help developers.

Share on:

Leave a Comment