Skip to content

Instantly share code, notes, and snippets.

@luisjunco
Created June 18, 2026 05:06
Show Gist options
  • Select an option

  • Save luisjunco/24c1648a8e8b71504d1bb3356074b40d to your computer and use it in GitHub Desktop.

Select an option

Save luisjunco/24c1648a8e8b71504d1bb3356074b40d to your computer and use it in GitHub Desktop.
Exercise to practice Pandas fundamentals

Practice: Pandas Fundamentals


Instructions

We'll use the Titanic dataset (built into seaborn) — it has missing values, making it perfect for this exercise.

Iteration 1: Import the dataset

import seaborn as sns
import pandas as pd

titanic = sns.load_dataset('titanic')

Iteration 2: Explore the data

  • Show the first 5 rows
  • Show the shape of the DataFrame (rows, columns)
  • Show the list of column names

Iteration 3: Sort the data

  • Sort the DataFrame by the fare column, from highest to lowest
  • Show the first 5 rows of the sorted result

Iteration 4: Check missing values

  • Check if there are any missing values in the DataFrame (True/False)
  • Count the number of missing values in each column

Bonus 1

  • Sort the DataFrame by age (descending) and then by fare (ascending) for passengers with the same age.
  • Show the top 10 rows, but only the age, fare, and class columns.

Bonus 2

  • Find which column has the most missing values.
  • Then create a new DataFrame that drops that column entirely, and fills the missing values in age with the average age.
  • Verify your result has zero missing values left.




Solutions

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment