Drop a Column from a Pandas DataFrame

Drop Columns removes specified columns from your DataFrame, helping you focus on relevant data by eliminating unnecessary fields. Remove unwanted columns using drop(). Specify the columns to drop with the columns parameter.

Here, inplace=True changes the existing df directly.

Let us see an example to drop a column from a Pandas DataFrame:

df = pd.DataFrame({'A':[1,2], 'B':[3,4], 'C':[5,6]})
df.drop(columns=['C'], inplace=True)
print(df)

Output

   A  B
0  1  3
1  2  4

This example creates a DataFrame with three columns and drops column ‘C’ using drop(). The original DataFrame is updated because of inplace=True.


If you liked the tutorial, spread the word and share the link and our website, Studyopedia, with others:


For Videos, Join Our YouTube Channel: Join Now


Read More:

Rename Columns in a Pandas DataFrame
Change Datatype in Pandas
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment