{
"cells": [
{
"cell_type": "markdown",
"id": "91d5c3d6",
"metadata": {},
"source": [
"# 💡 Pandas Practice Exercise 1: Solutions & Walkthroughs\n",
"\n",
"This notebook contains **complete, verified solutions** for all 20 problems in `02_pandas_exercise_1.ipynb`.\n",
"Every code cell is pre-executed with visible outputs for instant verification."
]
},
{
"cell_type": "markdown",
"id": "c0a0512b",
"metadata": {},
"source": [
"### Problem 1: Import Pandas and Check Version\n",
"\n",
"Import pandas with the alias `pd` and numpy as `np`. Print their installed versions."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "2987e763",
"metadata": {
"execution": {
"iopub.execute_input": "2026-10-03T05:00:09.514904Z",
"iopub.status.busy": "2026-10-03T05:00:09.514762Z",
"iopub.status.idle": "2026-10-03T05:00:09.976004Z",
"shell.execute_reply": "2026-10-03T05:00:09.975637Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Pandas version: 3.0.6\n",
"NumPy version: 2.5.3\n"
]
}
],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"\n",
"print(f\"Pandas version: {pd.__version__}\")\n",
"print(f\"NumPy version: {np.__version__}\")"
]
},
{
"cell_type": "markdown",
"id": "ca0a4450",
"metadata": {},
"source": [
"### Problem 2: Create a Series with Custom Index\n",
"\n",
"Create a `pd.Series` containing the populations (in millions) of five cities: `{'Tokyo': 37.4, 'Delhi': 29.3, 'Shanghai': 26.3, 'Sao Paulo': 21.8, 'Mexico City': 21.6}`. Give the Series the name `'Population_M'`."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "7e4defbd",
"metadata": {
"execution": {
"iopub.execute_input": "2026-10-03T05:00:09.977505Z",
"iopub.status.busy": "2026-10-03T05:00:09.977341Z",
"iopub.status.idle": "2026-10-03T05:00:09.983078Z",
"shell.execute_reply": "2026-10-03T05:00:09.982801Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Tokyo 37.4\n",
"Delhi 29.3\n",
"Shanghai 26.3\n",
"Sao Paulo 21.8\n",
"Mexico City 21.6\n",
"Name: Population_M, dtype: float64\n",
"\n",
"Tokyo Population: 37.4M\n"
]
}
],
"source": [
"cities = ['Tokyo', 'Delhi', 'Shanghai', 'Sao Paulo', 'Mexico City']\n",
"populations = [37.4, 29.3, 26.3, 21.8, 21.6]\n",
"\n",
"pop_series = pd.Series(populations, index=cities, name='Population_M')\n",
"print(pop_series)\n",
"print(f\"\\nTokyo Population: {pop_series['Tokyo']}M\")"
]
},
{
"cell_type": "markdown",
"id": "12459b6b",
"metadata": {},
"source": [
"### Problem 3: Create a DataFrame from a Dictionary\n",
"\n",
"Create a DataFrame `students_df` representing 5 students with the following columns:\n",
"- `'Name'`: `['Alice', 'Bob', 'Charlie', 'Diana', 'Evan']`\n",
"- `'Age'`: `[20, 22, 21, 23, 20]`\n",
"- `'Major'`: `['CS', 'Math', 'Physics', 'CS', 'Biology']`\n",
"- `'GPA'`: `[3.8, 3.2, 3.9, 2.9, 3.5]`\n",
"- `'Passed'`: `[True, True, True, False, True]`"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "ed877919",
"metadata": {
"execution": {
"iopub.execute_input": "2026-10-03T05:00:09.984301Z",
"iopub.status.busy": "2026-10-03T05:00:09.984187Z",
"iopub.status.idle": "2026-10-03T05:00:09.992275Z",
"shell.execute_reply": "2026-10-03T05:00:09.992011Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Name | \n",
" Age | \n",
" Major | \n",
" GPA | \n",
" Passed | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" Alice | \n",
" 20 | \n",
" CS | \n",
" 3.8 | \n",
" True | \n",
"
\n",
" \n",
" | 1 | \n",
" Bob | \n",
" 22 | \n",
" Math | \n",
" 3.2 | \n",
" True | \n",
"
\n",
" \n",
" | 2 | \n",
" Charlie | \n",
" 21 | \n",
" Physics | \n",
" 3.9 | \n",
" True | \n",
"
\n",
" \n",
" | 3 | \n",
" Diana | \n",
" 23 | \n",
" CS | \n",
" 2.9 | \n",
" False | \n",
"
\n",
" \n",
" | 4 | \n",
" Evan | \n",
" 20 | \n",
" Biology | \n",
" 3.5 | \n",
" True | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Name Age Major GPA Passed\n",
"0 Alice 20 CS 3.8 True\n",
"1 Bob 22 Math 3.2 True\n",
"2 Charlie 21 Physics 3.9 True\n",
"3 Diana 23 CS 2.9 False\n",
"4 Evan 20 Biology 3.5 True"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"data = {\n",
" 'Name': ['Alice', 'Bob', 'Charlie', 'Diana', 'Evan'],\n",
" 'Age': [20, 22, 21, 23, 20],\n",
" 'Major': ['CS', 'Math', 'Physics', 'CS', 'Biology'],\n",
" 'GPA': [3.8, 3.2, 3.9, 2.9, 3.5],\n",
" 'Passed': [True, True, True, False, True]\n",
"}\n",
"\n",
"students_df = pd.DataFrame(data)\n",
"display(students_df)"
]
},
{
"cell_type": "markdown",
"id": "21e7cdcf",
"metadata": {},
"source": [
"### Problem 4: Inspect DataFrame Dimensions and Data Types\n",
"\n",
"Using `students_df`, print:\n",
"1. The total number of rows and columns (shape)\n",
"2. The column names\n",
"3. The data type (`dtype`) of each column\n",
"4. A summary of non-null counts and memory using `.info()`"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "0e9b1e96",
"metadata": {
"execution": {
"iopub.execute_input": "2026-10-03T05:00:09.993358Z",
"iopub.status.busy": "2026-10-03T05:00:09.993272Z",
"iopub.status.idle": "2026-10-03T05:00:10.001886Z",
"shell.execute_reply": "2026-10-03T05:00:10.001579Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Shape (rows, cols): (5, 5)\n",
"Columns: ['Name', 'Age', 'Major', 'GPA', 'Passed']\n",
"\n",
"Column Data Types:\n",
"Name str\n",
"Age int64\n",
"Major str\n",
"GPA float64\n",
"Passed bool\n",
"dtype: object\n",
"\n",
"DataFrame Info:\n",
"\n",
"RangeIndex: 5 entries, 0 to 4\n",
"Data columns (total 5 columns):\n",
" # Column Non-Null Count Dtype \n",
"--- ------ -------------- ----- \n",
" 0 Name 5 non-null str \n",
" 1 Age 5 non-null int64 \n",
" 2 Major 5 non-null str \n",
" 3 GPA 5 non-null float64\n",
" 4 Passed 5 non-null bool \n",
"dtypes: bool(1), float64(1), int64(1), str(2)\n",
"memory usage: 297.0 bytes\n"
]
}
],
"source": [
"print(f\"Shape (rows, cols): {students_df.shape}\")\n",
"print(f\"Columns: {list(students_df.columns)}\")\n",
"print(\"\\nColumn Data Types:\")\n",
"print(students_df.dtypes)\n",
"print(\"\\nDataFrame Info:\")\n",
"students_df.info()"
]
},
{
"cell_type": "markdown",
"id": "ac5d917b",
"metadata": {},
"source": [
"### Problem 5: Series vs DataFrame Column Selection\n",
"\n",
"1. Extract the `'GPA'` column as a 1D `Series` and store it in `gpa_series`.\n",
"2. Extract the `'Name'` and `'GPA'` columns as a 2D `DataFrame` and store it in `name_gpa_df`.\n",
"3. Print the types of both variables."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "9c82c5ba",
"metadata": {
"execution": {
"iopub.execute_input": "2026-10-03T05:00:10.003102Z",
"iopub.status.busy": "2026-10-03T05:00:10.002989Z",
"iopub.status.idle": "2026-10-03T05:00:10.008365Z",
"shell.execute_reply": "2026-10-03T05:00:10.008073Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Type of gpa_series: \n",
"Type of name_gpa_df: \n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Name | \n",
" GPA | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" Alice | \n",
" 3.8 | \n",
"
\n",
" \n",
" | 1 | \n",
" Bob | \n",
" 3.2 | \n",
"
\n",
" \n",
" | 2 | \n",
" Charlie | \n",
" 3.9 | \n",
"
\n",
" \n",
" | 3 | \n",
" Diana | \n",
" 2.9 | \n",
"
\n",
" \n",
" | 4 | \n",
" Evan | \n",
" 3.5 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Name GPA\n",
"0 Alice 3.8\n",
"1 Bob 3.2\n",
"2 Charlie 3.9\n",
"3 Diana 2.9\n",
"4 Evan 3.5"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"gpa_series = students_df['GPA']\n",
"name_gpa_df = students_df[['Name', 'GPA']]\n",
"\n",
"print(f\"Type of gpa_series: {type(gpa_series)}\")\n",
"print(f\"Type of name_gpa_df: {type(name_gpa_df)}\")\n",
"display(name_gpa_df)"
]
},
{
"cell_type": "markdown",
"id": "20e715c7",
"metadata": {},
"source": [
"### Problem 6: Positional Slicing with .iloc\n",
"\n",
"Using `students_df`:\n",
"1. Select the first 3 rows and the first 3 columns using `.iloc`.\n",
"2. Select the very last row using negative integer indexing."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "4bd669e2",
"metadata": {
"execution": {
"iopub.execute_input": "2026-10-03T05:00:10.009539Z",
"iopub.status.busy": "2026-10-03T05:00:10.009449Z",
"iopub.status.idle": "2026-10-03T05:00:10.013541Z",
"shell.execute_reply": "2026-10-03T05:00:10.013297Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"First 3 rows and 3 columns:\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Name | \n",
" Age | \n",
" Major | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" Alice | \n",
" 20 | \n",
" CS | \n",
"
\n",
" \n",
" | 1 | \n",
" Bob | \n",
" 22 | \n",
" Math | \n",
"
\n",
" \n",
" | 2 | \n",
" Charlie | \n",
" 21 | \n",
" Physics | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Name Age Major\n",
"0 Alice 20 CS\n",
"1 Bob 22 Math\n",
"2 Charlie 21 Physics"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Last row:\n",
"Name Evan\n",
"Age 20\n",
"Major Biology\n",
"GPA 3.5\n",
"Passed True\n",
"Name: 4, dtype: object\n"
]
}
],
"source": [
"# 1. First 3 rows and first 3 columns\n",
"subset_iloc = students_df.iloc[0:3, 0:3]\n",
"print(\"First 3 rows and 3 columns:\")\n",
"display(subset_iloc)\n",
"\n",
"# 2. Last row\n",
"print(\"\\nLast row:\")\n",
"print(students_df.iloc[-1])"
]
},
{
"cell_type": "markdown",
"id": "1015f7a2",
"metadata": {},
"source": [
"### Problem 7: Label-Based Selection with .loc\n",
"\n",
"1. Create a new DataFrame `df_named` by setting `'Name'` as the index of `students_df`.\n",
"2. Use `.loc` to retrieve the `'Major'` and `'GPA'` for `'Charlie'`.\n",
"3. Slice from `'Bob'` to `'Diana'` (inclusive) for all columns."
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "9acf21a1",
"metadata": {
"execution": {
"iopub.execute_input": "2026-10-03T05:00:10.014721Z",
"iopub.status.busy": "2026-10-03T05:00:10.014612Z",
"iopub.status.idle": "2026-10-03T05:00:10.020519Z",
"shell.execute_reply": "2026-10-03T05:00:10.020236Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Charlie's Major and GPA:\n",
"Major Physics\n",
"GPA 3.9\n",
"Name: Charlie, dtype: object\n",
"\n",
"Slice from Bob to Diana (inclusive):\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Age | \n",
" Major | \n",
" GPA | \n",
" Passed | \n",
"
\n",
" \n",
" | Name | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" | Bob | \n",
" 22 | \n",
" Math | \n",
" 3.2 | \n",
" True | \n",
"
\n",
" \n",
" | Charlie | \n",
" 21 | \n",
" Physics | \n",
" 3.9 | \n",
" True | \n",
"
\n",
" \n",
" | Diana | \n",
" 23 | \n",
" CS | \n",
" 2.9 | \n",
" False | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Age Major GPA Passed\n",
"Name \n",
"Bob 22 Math 3.2 True\n",
"Charlie 21 Physics 3.9 True\n",
"Diana 23 CS 2.9 False"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"df_named = students_df.set_index('Name')\n",
"\n",
"# Charlie's record\n",
"charlie_info = df_named.loc['Charlie', ['Major', 'GPA']]\n",
"print(\"Charlie's Major and GPA:\")\n",
"print(charlie_info)\n",
"\n",
"# Slice from Bob to Diana\n",
"print(\"\\nSlice from Bob to Diana (inclusive):\")\n",
"display(df_named.loc['Bob':'Diana'])"
]
},
{
"cell_type": "markdown",
"id": "9fbbe2cb",
"metadata": {},
"source": [
"### Problem 8: Single Condition Filtering\n",
"\n",
"Filter `students_df` to find all students who have a `'GPA'` strictly greater than `3.4`."
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "b73ce24b",
"metadata": {
"execution": {
"iopub.execute_input": "2026-10-03T05:00:10.021601Z",
"iopub.status.busy": "2026-10-03T05:00:10.021512Z",
"iopub.status.idle": "2026-10-03T05:00:10.025546Z",
"shell.execute_reply": "2026-10-03T05:00:10.025329Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Name | \n",
" Age | \n",
" Major | \n",
" GPA | \n",
" Passed | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" Alice | \n",
" 20 | \n",
" CS | \n",
" 3.8 | \n",
" True | \n",
"
\n",
" \n",
" | 2 | \n",
" Charlie | \n",
" 21 | \n",
" Physics | \n",
" 3.9 | \n",
" True | \n",
"
\n",
" \n",
" | 4 | \n",
" Evan | \n",
" 20 | \n",
" Biology | \n",
" 3.5 | \n",
" True | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Name Age Major GPA Passed\n",
"0 Alice 20 CS 3.8 True\n",
"2 Charlie 21 Physics 3.9 True\n",
"4 Evan 20 Biology 3.5 True"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"high_achievers = students_df[students_df['GPA'] > 3.4]\n",
"display(high_achievers)"
]
},
{
"cell_type": "markdown",
"id": "549f934a",
"metadata": {},
"source": [
"### Problem 9: Multi-Condition Filtering with Logical Operators\n",
"\n",
"Find all students who satisfy BOTH conditions:\n",
"- `'Major'` is `'CS'`\n",
"- `'Passed'` is `True`\n",
"Remember to use the `&` operator and enclose conditions in parentheses `()`."
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "5b5b8802",
"metadata": {
"execution": {
"iopub.execute_input": "2026-10-03T05:00:10.026606Z",
"iopub.status.busy": "2026-10-03T05:00:10.026523Z",
"iopub.status.idle": "2026-10-03T05:00:10.030270Z",
"shell.execute_reply": "2026-10-03T05:00:10.030037Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Name | \n",
" Age | \n",
" Major | \n",
" GPA | \n",
" Passed | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" Alice | \n",
" 20 | \n",
" CS | \n",
" 3.8 | \n",
" True | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Name Age Major GPA Passed\n",
"0 Alice 20 CS 3.8 True"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"cs_passed = students_df[(students_df['Major'] == 'CS') & (students_df['Passed'] == True)]\n",
"display(cs_passed)"
]
},
{
"cell_type": "markdown",
"id": "e445078b",
"metadata": {},
"source": [
"### Problem 10: Filtering with .isin() and .between()\n",
"\n",
"1. Find all students whose `'Major'` is either `'Math'` or `'Physics'` using `.isin()`.\n",
"2. Find all students whose `'Age'` is between 21 and 23 (inclusive) using `.between()`."
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "b218bb3e",
"metadata": {
"execution": {
"iopub.execute_input": "2026-10-03T05:00:10.031389Z",
"iopub.status.busy": "2026-10-03T05:00:10.031289Z",
"iopub.status.idle": "2026-10-03T05:00:10.038036Z",
"shell.execute_reply": "2026-10-03T05:00:10.037803Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Math or Physics Majors:\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Name | \n",
" Age | \n",
" Major | \n",
" GPA | \n",
" Passed | \n",
"
\n",
" \n",
" \n",
" \n",
" | 1 | \n",
" Bob | \n",
" 22 | \n",
" Math | \n",
" 3.2 | \n",
" True | \n",
"
\n",
" \n",
" | 2 | \n",
" Charlie | \n",
" 21 | \n",
" Physics | \n",
" 3.9 | \n",
" True | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Name Age Major GPA Passed\n",
"1 Bob 22 Math 3.2 True\n",
"2 Charlie 21 Physics 3.9 True"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Age between 21 and 23:\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Name | \n",
" Age | \n",
" Major | \n",
" GPA | \n",
" Passed | \n",
"
\n",
" \n",
" \n",
" \n",
" | 1 | \n",
" Bob | \n",
" 22 | \n",
" Math | \n",
" 3.2 | \n",
" True | \n",
"
\n",
" \n",
" | 2 | \n",
" Charlie | \n",
" 21 | \n",
" Physics | \n",
" 3.9 | \n",
" True | \n",
"
\n",
" \n",
" | 3 | \n",
" Diana | \n",
" 23 | \n",
" CS | \n",
" 2.9 | \n",
" False | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Name Age Major GPA Passed\n",
"1 Bob 22 Math 3.2 True\n",
"2 Charlie 21 Physics 3.9 True\n",
"3 Diana 23 CS 2.9 False"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"math_physics = students_df[students_df['Major'].isin(['Math', 'Physics'])]\n",
"print(\"Math or Physics Majors:\")\n",
"display(math_physics)\n",
"\n",
"age_21_23 = students_df[students_df['Age'].between(21, 23)]\n",
"print(\"\\nAge between 21 and 23:\")\n",
"display(age_21_23)"
]
},
{
"cell_type": "markdown",
"id": "b42ca088",
"metadata": {},
"source": [
"### Problem 11: Add Calculated Columns (Vectorized)\n",
"\n",
"Add two new columns to a copy of `students_df`:\n",
"1. `'GPA_Percentage'`: Convert the 4.0 GPA scale to percentage (`GPA / 4.0 * 100`).\n",
"2. `'Honors'`: Boolean column indicating whether `GPA >= 3.8`."
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "f6236f84",
"metadata": {
"execution": {
"iopub.execute_input": "2026-10-03T05:00:10.039111Z",
"iopub.status.busy": "2026-10-03T05:00:10.039012Z",
"iopub.status.idle": "2026-10-03T05:00:10.043622Z",
"shell.execute_reply": "2026-10-03T05:00:10.043380Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Name | \n",
" Age | \n",
" Major | \n",
" GPA | \n",
" Passed | \n",
" GPA_Percentage | \n",
" Honors | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" Alice | \n",
" 20 | \n",
" CS | \n",
" 3.8 | \n",
" True | \n",
" 95.0 | \n",
" True | \n",
"
\n",
" \n",
" | 1 | \n",
" Bob | \n",
" 22 | \n",
" Math | \n",
" 3.2 | \n",
" True | \n",
" 80.0 | \n",
" False | \n",
"
\n",
" \n",
" | 2 | \n",
" Charlie | \n",
" 21 | \n",
" Physics | \n",
" 3.9 | \n",
" True | \n",
" 97.5 | \n",
" True | \n",
"
\n",
" \n",
" | 3 | \n",
" Diana | \n",
" 23 | \n",
" CS | \n",
" 2.9 | \n",
" False | \n",
" 72.5 | \n",
" False | \n",
"
\n",
" \n",
" | 4 | \n",
" Evan | \n",
" 20 | \n",
" Biology | \n",
" 3.5 | \n",
" True | \n",
" 87.5 | \n",
" False | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Name Age Major GPA Passed GPA_Percentage Honors\n",
"0 Alice 20 CS 3.8 True 95.0 True\n",
"1 Bob 22 Math 3.2 True 80.0 False\n",
"2 Charlie 21 Physics 3.9 True 97.5 True\n",
"3 Diana 23 CS 2.9 False 72.5 False\n",
"4 Evan 20 Biology 3.5 True 87.5 False"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"df_copy = students_df.copy()\n",
"df_copy['GPA_Percentage'] = (df_copy['GPA'] / 4.0) * 100\n",
"df_copy['Honors'] = df_copy['GPA'] >= 3.8\n",
"display(df_copy)"
]
},
{
"cell_type": "markdown",
"id": "566d9710",
"metadata": {},
"source": [
"### Problem 12: Renaming Columns\n",
"\n",
"Rename `'Major'` to `'Field_of_Study'` and `'Passed'` to `'Graduation_Ready'` in `students_df`."
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "96ed6022",
"metadata": {
"execution": {
"iopub.execute_input": "2026-10-03T05:00:10.044726Z",
"iopub.status.busy": "2026-10-03T05:00:10.044635Z",
"iopub.status.idle": "2026-10-03T05:00:10.048188Z",
"shell.execute_reply": "2026-10-03T05:00:10.047954Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Name | \n",
" Age | \n",
" Field_of_Study | \n",
" GPA | \n",
" Graduation_Ready | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" Alice | \n",
" 20 | \n",
" CS | \n",
" 3.8 | \n",
" True | \n",
"
\n",
" \n",
" | 1 | \n",
" Bob | \n",
" 22 | \n",
" Math | \n",
" 3.2 | \n",
" True | \n",
"
\n",
" \n",
" | 2 | \n",
" Charlie | \n",
" 21 | \n",
" Physics | \n",
" 3.9 | \n",
" True | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Name Age Field_of_Study GPA Graduation_Ready\n",
"0 Alice 20 CS 3.8 True\n",
"1 Bob 22 Math 3.2 True\n",
"2 Charlie 21 Physics 3.9 True"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"df_renamed = students_df.rename(columns={\n",
" 'Major': 'Field_of_Study',\n",
" 'Passed': 'Graduation_Ready'\n",
"})\n",
"display(df_renamed.head(3))"
]
},
{
"cell_type": "markdown",
"id": "3108b983",
"metadata": {},
"source": [
"### Problem 13: Dropping Columns Safely\n",
"\n",
"Drop the `'Passed'` column from `students_df` without modifying the original in-place. Verify that the original `students_df` still has all 5 columns."
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "dfafb781",
"metadata": {
"execution": {
"iopub.execute_input": "2026-10-03T05:00:10.049275Z",
"iopub.status.busy": "2026-10-03T05:00:10.049185Z",
"iopub.status.idle": "2026-10-03T05:00:10.051304Z",
"shell.execute_reply": "2026-10-03T05:00:10.051074Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"New DataFrame columns: ['Name', 'Age', 'Major', 'GPA']\n",
"Original DataFrame columns: ['Name', 'Age', 'Major', 'GPA', 'Passed']\n"
]
}
],
"source": [
"df_without_passed = students_df.drop(columns=['Passed'])\n",
"print(\"New DataFrame columns: \", list(df_without_passed.columns))\n",
"print(\"Original DataFrame columns:\", list(students_df.columns))"
]
},
{
"cell_type": "markdown",
"id": "88b1417e",
"metadata": {},
"source": [
"### Problem 14: Detecting Missing Data\n",
"\n",
"Consider this dataset with missing values:\n",
"```python\n",
"exam_data = {\n",
" 'Student': ['Sam', 'Alex', 'Jordan', 'Taylor', 'Morgan'],\n",
" 'Midterm': [85.0, np.nan, 90.0, 72.0, np.nan],\n",
" 'Final': [88.0, 79.0, np.nan, 68.0, 95.0],\n",
" 'Attendance': [95, 80, 85, np.nan, 90]\n",
"}\n",
"exam_df = pd.DataFrame(exam_data)\n",
"```\n",
"1. Count total missing values (`NaN`) per column.\n",
"2. Print the total number of missing values in the entire DataFrame."
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "a2843333",
"metadata": {
"execution": {
"iopub.execute_input": "2026-10-03T05:00:10.052491Z",
"iopub.status.busy": "2026-10-03T05:00:10.052400Z",
"iopub.status.idle": "2026-10-03T05:00:10.055642Z",
"shell.execute_reply": "2026-10-03T05:00:10.055402Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Missing values per column:\n",
"Student 0\n",
"Midterm 2\n",
"Final 1\n",
"Attendance 1\n",
"dtype: int64\n",
"\n",
"Total missing in whole table: 4\n"
]
}
],
"source": [
"exam_data = {\n",
" 'Student': ['Sam', 'Alex', 'Jordan', 'Taylor', 'Morgan'],\n",
" 'Midterm': [85.0, np.nan, 90.0, 72.0, np.nan],\n",
" 'Final': [88.0, 79.0, np.nan, 68.0, 95.0],\n",
" 'Attendance': [95, 80, 85, np.nan, 90]\n",
"}\n",
"exam_df = pd.DataFrame(exam_data)\n",
"\n",
"print(\"Missing values per column:\")\n",
"print(exam_df.isna().sum())\n",
"print(f\"\\nTotal missing in whole table: {exam_df.isna().sum().sum()}\")"
]
},
{
"cell_type": "markdown",
"id": "395277bd",
"metadata": {},
"source": [
"### Problem 15: Imputing Missing Values\n",
"\n",
"Fill the missing values in `exam_df`:\n",
"1. Fill missing `'Midterm'` scores with the average (mean) of the existing Midterm scores.\n",
"2. Fill missing `'Final'` scores with the median of existing Final scores.\n",
"3. Fill missing `'Attendance'` with `100`."
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "a6a573da",
"metadata": {
"execution": {
"iopub.execute_input": "2026-10-03T05:00:10.056707Z",
"iopub.status.busy": "2026-10-03T05:00:10.056619Z",
"iopub.status.idle": "2026-10-03T05:00:10.062034Z",
"shell.execute_reply": "2026-10-03T05:00:10.061818Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Student | \n",
" Midterm | \n",
" Final | \n",
" Attendance | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" Sam | \n",
" 85.000000 | \n",
" 88.0 | \n",
" 95.0 | \n",
"
\n",
" \n",
" | 1 | \n",
" Alex | \n",
" 82.333333 | \n",
" 79.0 | \n",
" 80.0 | \n",
"
\n",
" \n",
" | 2 | \n",
" Jordan | \n",
" 90.000000 | \n",
" 83.5 | \n",
" 85.0 | \n",
"
\n",
" \n",
" | 3 | \n",
" Taylor | \n",
" 72.000000 | \n",
" 68.0 | \n",
" 100.0 | \n",
"
\n",
" \n",
" | 4 | \n",
" Morgan | \n",
" 82.333333 | \n",
" 95.0 | \n",
" 90.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Student Midterm Final Attendance\n",
"0 Sam 85.000000 88.0 95.0\n",
"1 Alex 82.333333 79.0 80.0\n",
"2 Jordan 90.000000 83.5 85.0\n",
"3 Taylor 72.000000 68.0 100.0\n",
"4 Morgan 82.333333 95.0 90.0"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"exam_imputed = exam_df.copy()\n",
"\n",
"exam_imputed['Midterm'] = exam_imputed['Midterm'].fillna(exam_imputed['Midterm'].mean())\n",
"exam_imputed['Final'] = exam_imputed['Final'].fillna(exam_imputed['Final'].median())\n",
"exam_imputed['Attendance'] = exam_imputed['Attendance'].fillna(100)\n",
"\n",
"display(exam_imputed)"
]
},
{
"cell_type": "markdown",
"id": "98f43fe3",
"metadata": {},
"source": [
"### Problem 16: Sorting Values & Resetting Index\n",
"\n",
"Using `students_df`:\n",
"1. Sort students by `'GPA'` descending (highest GPA first).\n",
"2. Reset the row index so it starts cleanly from `0, 1, 2, ...`.\n",
"3. Print the top 3 students."
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "0c12e898",
"metadata": {
"execution": {
"iopub.execute_input": "2026-10-03T05:00:10.063120Z",
"iopub.status.busy": "2026-10-03T05:00:10.063034Z",
"iopub.status.idle": "2026-10-03T05:00:10.067304Z",
"shell.execute_reply": "2026-10-03T05:00:10.067069Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Top Students by GPA:\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Name | \n",
" Age | \n",
" Major | \n",
" GPA | \n",
" Passed | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" Charlie | \n",
" 21 | \n",
" Physics | \n",
" 3.9 | \n",
" True | \n",
"
\n",
" \n",
" | 1 | \n",
" Alice | \n",
" 20 | \n",
" CS | \n",
" 3.8 | \n",
" True | \n",
"
\n",
" \n",
" | 2 | \n",
" Evan | \n",
" 20 | \n",
" Biology | \n",
" 3.5 | \n",
" True | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Name Age Major GPA Passed\n",
"0 Charlie 21 Physics 3.9 True\n",
"1 Alice 20 CS 3.8 True\n",
"2 Evan 20 Biology 3.5 True"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"top_students = students_df.sort_values(by='GPA', ascending=False).reset_index(drop=True)\n",
"print(\"Top Students by GPA:\")\n",
"display(top_students.head(3))"
]
},
{
"cell_type": "markdown",
"id": "8a3c3b5c",
"metadata": {},
"source": [
"### Problem 17: Summary Statistics and Value Counts\n",
"\n",
"Using `students_df`:\n",
"1. Compute the mean, median, min, max, and standard deviation of `'GPA'`.\n",
"2. Count how many students belong to each `'Major'` using `.value_counts()`.\n",
"3. Calculate the percentage of students in each major."
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "663aa6a8",
"metadata": {
"execution": {
"iopub.execute_input": "2026-10-03T05:00:10.068386Z",
"iopub.status.busy": "2026-10-03T05:00:10.068291Z",
"iopub.status.idle": "2026-10-03T05:00:10.072131Z",
"shell.execute_reply": "2026-10-03T05:00:10.071871Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"GPA Mean: 3.46\n",
"GPA Median: 3.50\n",
"GPA Min: 2.90\n",
"GPA Max: 3.90\n",
"GPA Std: 0.42\n",
"\n",
"Student count per Major:\n",
"Major\n",
"CS 2\n",
"Math 1\n",
"Physics 1\n",
"Biology 1\n",
"Name: count, dtype: int64\n",
"\n",
"Major Percentage Share:\n",
"Major\n",
"CS 40.0\n",
"Math 20.0\n",
"Physics 20.0\n",
"Biology 20.0\n",
"Name: proportion, dtype: float64\n"
]
}
],
"source": [
"print(f\"GPA Mean: {students_df['GPA'].mean():.2f}\")\n",
"print(f\"GPA Median: {students_df['GPA'].median():.2f}\")\n",
"print(f\"GPA Min: {students_df['GPA'].min():.2f}\")\n",
"print(f\"GPA Max: {students_df['GPA'].max():.2f}\")\n",
"print(f\"GPA Std: {students_df['GPA'].std():.2f}\")\n",
"\n",
"print(\"\\nStudent count per Major:\")\n",
"print(students_df['Major'].value_counts())\n",
"\n",
"print(\"\\nMajor Percentage Share:\")\n",
"print((students_df['Major'].value_counts(normalize=True) * 100).round(1))"
]
},
{
"cell_type": "markdown",
"id": "1fedaf54",
"metadata": {},
"source": [
"### Problem 18: GroupBy Single Column Aggregation\n",
"\n",
"Group `students_df` by `'Major'` and compute the average `'GPA'` and average `'Age'` for each major."
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "ffa2cb9f",
"metadata": {
"execution": {
"iopub.execute_input": "2026-10-03T05:00:10.073178Z",
"iopub.status.busy": "2026-10-03T05:00:10.073087Z",
"iopub.status.idle": "2026-10-03T05:00:10.077680Z",
"shell.execute_reply": "2026-10-03T05:00:10.077436Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" GPA | \n",
" Age | \n",
"
\n",
" \n",
" | Major | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" | Biology | \n",
" 3.50 | \n",
" 20.0 | \n",
"
\n",
" \n",
" | CS | \n",
" 3.35 | \n",
" 21.5 | \n",
"
\n",
" \n",
" | Math | \n",
" 3.20 | \n",
" 22.0 | \n",
"
\n",
" \n",
" | Physics | \n",
" 3.90 | \n",
" 21.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" GPA Age\n",
"Major \n",
"Biology 3.50 20.0\n",
"CS 3.35 21.5\n",
"Math 3.20 22.0\n",
"Physics 3.90 21.0"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"major_stats = students_df.groupby('Major')[['GPA', 'Age']].mean()\n",
"display(major_stats)"
]
},
{
"cell_type": "markdown",
"id": "1524e687",
"metadata": {},
"source": [
"### Problem 19: Named Aggregations with .groupby().agg()\n",
"\n",
"Group `students_df` by `'Major'` and compute:\n",
"1. `'Student_Count'`: total number of students in that major (`count`)\n",
"2. `'Avg_GPA'`: average GPA in that major (`mean`)\n",
"3. `'Highest_GPA'`: highest GPA in that major (`max`)\n",
"Reset the index so `'Major'` becomes a regular column."
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "dc83b46f",
"metadata": {
"execution": {
"iopub.execute_input": "2026-10-03T05:00:10.078737Z",
"iopub.status.busy": "2026-10-03T05:00:10.078653Z",
"iopub.status.idle": "2026-10-03T05:00:10.085875Z",
"shell.execute_reply": "2026-10-03T05:00:10.085452Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Major | \n",
" Student_Count | \n",
" Avg_GPA | \n",
" Highest_GPA | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" Biology | \n",
" 1 | \n",
" 3.50 | \n",
" 3.5 | \n",
"
\n",
" \n",
" | 1 | \n",
" CS | \n",
" 2 | \n",
" 3.35 | \n",
" 3.8 | \n",
"
\n",
" \n",
" | 2 | \n",
" Math | \n",
" 1 | \n",
" 3.20 | \n",
" 3.2 | \n",
"
\n",
" \n",
" | 3 | \n",
" Physics | \n",
" 1 | \n",
" 3.90 | \n",
" 3.9 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Major Student_Count Avg_GPA Highest_GPA\n",
"0 Biology 1 3.50 3.5\n",
"1 CS 2 3.35 3.8\n",
"2 Math 1 3.20 3.2\n",
"3 Physics 1 3.90 3.9"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"agg_summary = students_df.groupby('Major').agg(\n",
" Student_Count=('Name', 'count'),\n",
" Avg_GPA=('GPA', 'mean'),\n",
" Highest_GPA=('GPA', 'max')\n",
").reset_index()\n",
"\n",
"display(agg_summary)"
]
},
{
"cell_type": "markdown",
"id": "d345e0a7",
"metadata": {},
"source": [
"### Problem 20: String Manipulation and Datetime Parsing\n",
"\n",
"Given the following enrollment log:\n",
"```python\n",
"logs_data = {\n",
" 'Raw_Student': [' ALICE ', 'bob smith', ' CHARLIE brown ', 'diana '],\n",
" 'Date_Enrolled': ['2026-01-10', '2026-02-15', '2026-03-01', '2026-03-22']\n",
"}\n",
"logs_df = pd.DataFrame(logs_data)\n",
"```\n",
"1. Clean `'Raw_Student'` by trimming whitespace and capitalizing each word to Title Case.\n",
"2. Convert `'Date_Enrolled'` to datetime objects.\n",
"3. Extract the `'Month_Name'` and `'Day_of_Week'` into new columns."
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "acaeabb6",
"metadata": {
"execution": {
"iopub.execute_input": "2026-10-03T05:00:10.086976Z",
"iopub.status.busy": "2026-10-03T05:00:10.086887Z",
"iopub.status.idle": "2026-10-03T05:00:10.093354Z",
"shell.execute_reply": "2026-10-03T05:00:10.093109Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Clean_Student | \n",
" Date_Enrolled | \n",
" Month_Name | \n",
" Day_of_Week | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" Alice | \n",
" 2026-01-10 | \n",
" January | \n",
" Saturday | \n",
"
\n",
" \n",
" | 1 | \n",
" Bob Smith | \n",
" 2026-02-15 | \n",
" February | \n",
" Sunday | \n",
"
\n",
" \n",
" | 2 | \n",
" Charlie Brown | \n",
" 2026-03-01 | \n",
" March | \n",
" Sunday | \n",
"
\n",
" \n",
" | 3 | \n",
" Diana | \n",
" 2026-03-22 | \n",
" March | \n",
" Sunday | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Clean_Student Date_Enrolled Month_Name Day_of_Week\n",
"0 Alice 2026-01-10 January Saturday\n",
"1 Bob Smith 2026-02-15 February Sunday\n",
"2 Charlie Brown 2026-03-01 March Sunday\n",
"3 Diana 2026-03-22 March Sunday"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"logs_data = {\n",
" 'Raw_Student': [' ALICE ', 'bob smith', ' CHARLIE brown ', 'diana '],\n",
" 'Date_Enrolled': ['2026-01-10', '2026-02-15', '2026-03-01', '2026-03-22']\n",
"}\n",
"logs_df = pd.DataFrame(logs_data)\n",
"\n",
"# 1. Clean strings\n",
"logs_df['Clean_Student'] = logs_df['Raw_Student'].str.strip().str.title()\n",
"\n",
"# 2. Convert to datetime\n",
"logs_df['Date_Enrolled'] = pd.to_datetime(logs_df['Date_Enrolled'])\n",
"\n",
"# 3. Extract calendar features\n",
"logs_df['Month_Name'] = logs_df['Date_Enrolled'].dt.month_name()\n",
"logs_df['Day_of_Week'] = logs_df['Date_Enrolled'].dt.day_name()\n",
"\n",
"display(logs_df[['Clean_Student', 'Date_Enrolled', 'Month_Name', 'Day_of_Week']])"
]
}
],
"metadata": {
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}