Are you looking to level up your Pandas skills? You've come to the right place! ๐ผ
This blog post contains a comprehensive list of 70+ practice questions designed to take you from a Pandas beginner to a data manipulation pro. We'll be using the famous Titanic Dataset โ covering everything from basic data inspection to advanced GroupBy operations and Pivot Tables.
Check out the full solutions! I've written a complete walkthrough with code and outputs for every single question above:
๐ Mastering Data Analysis with Pandas: Surviving the Titanic Dataset
[!NOTE]
Get the Dataset: Download the Titanic dataset used in this post from Kaggle:๐ฅ Titanic Dataset on Kaggle
Once downloaded, load it in Python like this:
import pandas as pd
df = pd.read_csv("titanic.csv")
๐ Phase 1: Understanding the Dataset
Before we can analyze anything, we need to know what we're dealing with. These questions will help you inspect and understand the structure of the dataset.
-
1. Import the Pandas library using the alias
pd. -
2. Load the
titanic.csvdataset into a DataFrame nameddf. - 3. Display the first 5 records of the dataset.
- 4. Display the last 5 records of the dataset.
- 5. Find the number of rows and columns in the dataset.
- 6. Display all column names.
- 7. Display the data type of each column.
-
8. Use
df.info()to examine the dataset. Identify the columns containing missing values. -
9. Use
df.describe()to obtain statistical information about numerical columns. - 10. Display statistical information for both numerical and categorical columns.
๐ฏ Phase 2: Selecting and Filtering Data
We rarely need an entire dataset at once. Learn how to slice and filter to extract exactly what you need.
-
11. Display only the
Agecolumn. -
12. Display the
Survived,Pclass,Sex,Age, andFarecolumns. -
13. Use
ilocto display the first 5 rows and first 4 columns. - 14. Display all passengers whose age is greater than 30.
- 15. Display all female passengers.
- 16. Find female passengers older than 30 years.
-
17. Display passengers belonging to passenger classes 1 and 2 using
isin(). - 18. Find passengers whose ages are between 20 and 30 years.
-
19. Use
query()to find passengers older than 40 years.
๐งน Phase 3: Missing Values and Duplicates
Real-world data is messy. Learn how to detect and handle missing values like a pro.
- 20. Find the number of missing values in each column.
- 21. Calculate the total number of missing values in the complete dataset.
- 22. Calculate the percentage of missing values in each column.
-
23. Replace missing
Agevalues with the median age. - 24. Create a new DataFrame after removing rows containing missing values.
- 25. Check the dataset for duplicate records.
- 26. Remove duplicate records and display the new shape of the dataset.
๐ Phase 4: Sorting and Frequency Analysis
Who paid the highest fare? Who were the youngest passengers? Sorting and frequency analysis answers it all.
-
27. Display all unique values in the
Sexcolumn. - 28. Count the number of male and female passengers.
- 29. Calculate the percentage of male and female passengers.
- 30. Sort passengers according to age in ascending order.
- 31. Sort passengers according to fare in descending order.
- 32. Display the five passengers who paid the highest fares.
- 33. Display the five youngest passengers.
๐งฎ Phase 5: Aggregate Functions
Averages, totals, standard deviations โ this is where you extract high-level insights from raw numbers.
- 34. Calculate the average age of passengers.
- 35. Calculate the median age.
- 36. Find the minimum and maximum passenger age.
- 37. Calculate the total fare paid by all passengers.
- 38. Find the average fare.
- 39. Calculate the standard deviation and variance of passenger ages.
-
40. Find the mode of the
Agecolumn. -
41. Using
agg(), calculate count, mean, median, min, max, and std of passenger ages. -
42. Use
agg()to calculate different statistics for bothAgeandFare.
๐ฅ Phase 6: GroupBy Operations
GroupBy is one of the most powerful tools in Pandas. Use it to segment and summarize data by categories.
-
43. Find the average age of male and female passengers using
groupby(). - 44. Calculate the survival rate for male and female passengers.
- 45. Find the number of passengers in each passenger class.
- 46. Calculate the average fare for each passenger class.
- 47. Find the average age for each passenger class.
- 48. Calculate the survival rate for each passenger class.
- 49. Find the number of survivors in each passenger class.
-
50. Group passengers by both
PclassandSexand calculate their survival rate. - 51. For each passenger class, calculate the mean, minimum, and maximum age.
- 52. For each passenger class, calculate the mean, median, and maximum fare.
๐ ๏ธ Phase 7: Creating and Transforming Columns
Go beyond reading data โ learn to engineer new features and transform existing ones.
-
53. Create a new column containing the mean age of each passenger's class using
transform(). -
54. Create an
Age_Groupcolumn with categories such as Child, Teenager, Young Adult, Adult, and Senior. - 55. Count the number of passengers in each age group.
-
56. Using
apply(), classify passengers as Minor or Adult. -
57. Using
map(), convertSurvived = 0to'Did Not Survive'andSurvived = 1to'Survived'. -
58. Use
replace()to changemaleandfemaletoMaleandFemale.
๐ Phase 8: Correlation and Crosstab
Understand how variables relate to each other. Does class affect survival? Does age affect fare?
- 59. Generate a correlation matrix for all numerical columns.
-
60. Examine the relationship between
Pclass,Fare,Age, andSurvived. -
61. Create a crosstab showing
SexversusSurvived. - 62. Create a percentage crosstab showing survival percentages for male and female passengers.
- 63. Create a crosstab showing passenger class versus survival status.
๐ Phase 9: Pivot Tables
Pivot tables are the ultimate tool for multi-dimensional summarization. Master these and you're a Pandas wizard.
- 64. Create a pivot table showing the mean survival rate by passenger class and sex.
- 65. Create a pivot table showing the average fare by passenger class and sex.
- 66. Create a pivot table showing the average age by passenger class and sex.
- 67. Create a pivot table showing the number of passengers by class and sex.
- 68. Create a pivot table showing the mean, median, and maximum fare according to passenger class and sex.
-
69. Add
margins=Trueto a survival pivot table and interpret theAllrow and column. - 70. Compare survival rates of male and female passengers across the three passenger classes.
๐ The Ultimate Challenge Task
Put everything together. This is where data analysis meets storytelling.
-
Challenge. Based on your Pandas analysis, write three observations about the Titanic dataset. Your observations should include:
- Survival by sex
- Survival by passenger class
- Average fare by passenger class
๐ Wrapping Up
How many were you able to solve without peeking? ๐ Keep practicing โ soon these Pandas operations will feel like second nature!
Happy Coding! ๐

Top comments (0)