In [1]:
import seaborn as sns
import pandas as pd
%matplotlib inline
pd.options.display.max_rows = 10
data = sns.load_dataset("titanic")
In [2]:
data
Out[2]:
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
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
886 0 2 male 27.0 0 0 13.0000 S Second man True NaN Southampton no True
887 1 1 female 19.0 0 0 30.0000 S First woman False B Southampton yes True
888 0 3 female NaN 1 2 23.4500 S Third woman False NaN Southampton no False
889 1 1 male 26.0 0 0 30.0000 C First man True C Cherbourg yes True
890 0 3 male 32.0 0 0 7.7500 Q Third man True NaN Queenstown no True

891 rows × 15 columns

Deleting the last column by it's name

The last column in the data above is the column "alone" and we are going to use the drop method shown below to delete the column, you may have notices the "1" parameter after the colum name, this is the axis parameter. 1 is for columns and 0 is for rows.

The way I try to remember this is 1 looks like a column so 1 is for columns.

In [10]:
data.drop(labels = "alone",axis = 1)
Out[10]:
survived pclass sex age sibsp parch fare embarked class who adult_male deck embark_town alive
0 0 3 male 22.0 1 0 7.2500 S Third man True NaN Southampton no
1 1 1 female 38.0 1 0 71.2833 C First woman False C Cherbourg yes
2 1 3 female 26.0 0 0 7.9250 S Third woman False NaN Southampton yes
3 1 1 female 35.0 1 0 53.1000 S First woman False C Southampton yes
4 0 3 male 35.0 0 0 8.0500 S Third man True NaN Southampton no
... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
886 0 2 male 27.0 0 0 13.0000 S Second man True NaN Southampton no
887 1 1 female 19.0 0 0 30.0000 S First woman False B Southampton yes
888 0 3 female NaN 1 2 23.4500 S Third woman False NaN Southampton no
889 1 1 male 26.0 0 0 30.0000 C First man True C Cherbourg yes
890 0 3 male 32.0 0 0 7.7500 Q Third man True NaN Queenstown no

891 rows × 14 columns

We can of course drop multiple columns by passing in a list of column names inside labels

  • lets delete class, who and fare to illustrate this point, and you can always use the inplace parameter to delete the columns and save them.
In [13]:
data.drop(labels =['class','who','fare'], axis = 1 )
Out[13]:
survived pclass sex age sibsp parch embarked adult_male deck embark_town alive alone
0 0 3 male 22.0 1 0 S True NaN Southampton no False
1 1 1 female 38.0 1 0 C False C Cherbourg yes False
2 1 3 female 26.0 0 0 S False NaN Southampton yes True
3 1 1 female 35.0 1 0 S False C Southampton yes False
4 0 3 male 35.0 0 0 S True NaN Southampton no True
... ... ... ... ... ... ... ... ... ... ... ... ...
886 0 2 male 27.0 0 0 S True NaN Southampton no True
887 1 1 female 19.0 0 0 S False B Southampton yes True
888 0 3 female NaN 1 2 S False NaN Southampton no False
889 1 1 male 26.0 0 0 C True C Cherbourg yes True
890 0 3 male 32.0 0 0 Q True NaN Queenstown no True

891 rows × 12 columns

In [ ]:
data.drop(labels =['class','who','fare'], axis = 1 ,inplace = True)

Bonus: What if you wanted to delete the last column of your data without passing the label name.

well remember columns parameter, we can do many things with that and pass the result to the drop method.

Let's look at a solid example

In [22]:
## very usefull one liner for you to understand columns and drop method.
#1.get the position of the last two columns by accessing len() which gives the length or the number of elements in the list.
#2.subtract 2 to get the last remaining
#3.pass the integer as slice to the columns method to get the actual labels of the columns
#4. finally used the labels to delete column names , by the drop method, 

data.drop(data.columns[len(data.columns)-2:],1)
Out[22]:
survived pclass sex age sibsp parch fare embarked class who adult_male deck embark_town
0 0 3 male 22.0 1 0 7.2500 S Third man True NaN Southampton
1 1 1 female 38.0 1 0 71.2833 C First woman False C Cherbourg
2 1 3 female 26.0 0 0 7.9250 S Third woman False NaN Southampton
3 1 1 female 35.0 1 0 53.1000 S First woman False C Southampton
4 0 3 male 35.0 0 0 8.0500 S Third man True NaN Southampton
... ... ... ... ... ... ... ... ... ... ... ... ... ...
886 0 2 male 27.0 0 0 13.0000 S Second man True NaN Southampton
887 1 1 female 19.0 0 0 30.0000 S First woman False B Southampton
888 0 3 female NaN 1 2 23.4500 S Third woman False NaN Southampton
889 1 1 male 26.0 0 0 30.0000 C First man True C Cherbourg
890 0 3 male 32.0 0 0 7.7500 Q Third man True NaN Queenstown

891 rows × 13 columns