pandas: Remove a column#
import pandas as pd
Loading data into Pandas DataFrame#
The dataset is from Kaggle - Pokemon.
data = pd.read_csv('data/Pokemon.csv')
data
Here, if you do not use the
columns
parameter, you need to specify the axis (0 for rows and 1 for columns).
# Here, 'Total' column is the sum of HP,Attack,Defense, SP. Atk, SP. Def, Speed.
dropped_data = data.drop('Total', axis=1)
dropped_data.head(3)
Alternatively, you can use the
columns
parameter to specify the column(s) to drop without needing to specify the axis.
dropped_data = data.drop(columns='Total')
dropped_data.head(3)
inplace=True
modifies the original DataFrame without needing to reassign it.
data.drop(columns='Total', inplace=True)
data.head(3)