We'll use the Titanic dataset (built into seaborn) — it has missing values, making it perfect for this exercise.
import seaborn as sns
import pandas as pd
titanic = sns.load_dataset('titanic')- Show the first 5 rows
- Show the shape of the DataFrame (rows, columns)
- Show the list of column names
- Sort the DataFrame by the
farecolumn, from highest to lowest - Show the first 5 rows of the sorted result
- Check if there are any missing values in the DataFrame (True/False)
- Count the number of missing values in each column
- Sort the DataFrame by
age(descending) and then byfare(ascending) for passengers with the same age. - Show the top 10 rows, but only the
age,fare, andclasscolumns.
- Find which column has the most missing values.
- Then create a new DataFrame that drops that column entirely, and fills the missing values in
agewith the average age. - Verify your result has zero missing values left.
Iteration 1
import seaborn as sns
import pandas as pd
titanic = sns.load_dataset('titanic')Iteration 2
titanic.head()
titanic.shape
titanic.columns.tolist()Iteration 3
titanic.sort_values('fare', ascending=False).head()Iteration 4
titanic.isna().values.any()
titanic.isna().sum()Bonus 1
titanic.sort_values(['age', 'fare'], ascending=[False, True])[['age', 'fare', 'class']].head(10)Bonus 2
missing_counts = titanic.isna().sum()
col_with_most_missing = missing_counts.idxmax()
titanic_clean = titanic.drop(columns=[col_with_most_missing])
titanic_clean['age'] = titanic_clean['age'].fillna(titanic_clean['age'].mean())
titanic_clean.isna().sum().sum() # should be 0