Drop one or more Columns In Pandas Dataframe

There are some small but important things that we keep on doing on a daily basis and keep on forgetting. Here’s one:

Drop one or more Columns In Pandas Dataframe

So, Let’s keep it simple and direct to the solution:
Steps:
1. Import Pandas
2. Load Data as a Pandas Dataframe
3. Drop One Column
4. Drop Multiple Columns

-> Import Pandas

# Import pandas package 
 import pandas as pd

-> Load Data as Pandas Dataframe

# Load Data 
df_data = pd.read_csv("Titanic_Original.csv")

Let’s take a look at the current Dataframe:

-> Now, Drop Column “Embarked”

# Remove Embarked Column
df_mod1 = df_data.drop('Embarked',axis = 1)
df_mod1.head()

Let’s take a look at the new Dataframe and “Embaked” column is not there anymore.

-> Finally, Drop Multiple Columns

# Remove Cabin and Parch Columns
df_mod2 = df_mod1.drop(['Cabin','Parch'],axis = 1)
df_mod2.head()

Let’s take a look at the Final Data and column “Cabin” and “Parch” are dropped.


And we are done!

0

Leave a Reply

Your email address will not be published. Required fields are marked *