drop

Use drop to remove labels from an axis. Most of the time you use this to remove columns from a dataframe. Use the titanic dataset to illustrate. Here's the raw data.


import pandas as pd
import seaborn as sns

df = sns.load_dataset('titanic')
df.head(5)
          
survived pclass sex age sibsp parch fare embarked class who adult_male deck embark_town alive alone
0 0 3 male 22.0 1 0 7.2500 S Third man True NaN Southampton no False
1 1 1 female 38.0 1 0 71.2833 C First woman False C Cherbourg yes False
2 1 3 female 26.0 0 0 7.9250 S Third woman False NaN Southampton yes True
3 1 1 female 35.0 1 0 53.1000 S First woman False C Southampton yes False
4 0 3 male 35.0 0 0 8.0500 S Third man True NaN Southampton no True

remove columns

Use the drop function to remove columns from your data frame. Pass the list of column names in as the first argument, and set axis=1 to make it apply this over your column names. Returns a copy of the data frame with those columns removed.


(
    df
    .drop(['sibsp', 'parch'], axis=1)
).head(5)
          
survived pclass sex age fare embarked class who adult_male deck embark_town alive alone
0 0 3 male 22.0 7.2500 S Third man True NaN Southampton no False
1 1 1 female 38.0 71.2833 C First woman False C Cherbourg yes False
2 1 3 female 26.0 7.9250 S Third woman False NaN Southampton yes True
3 1 1 female 35.0 53.1000 S First woman False C Southampton yes False
4 0 3 male 35.0 8.0500 S Third man True NaN Southampton no True