Exercises Solutions

Solution 1

Problem: Load the dataset 05_random_students.csv and display the first 10 rows.

import pandas as pd

df = pd.read_csv("05_random_students.csv")
print(df.head(10))

Solution 2

Problem: Calculate the average MathScore for students older than 25.

avg_mathscore = df[df["Age"] > 25]["MathScore"].mean()
print("Average MathScore for students older than 25:", avg_mathscore)

Solution 3

Problem: Use groupby to compute the average ScienceScore by age group (18–22, 23–26, 27–30).

# Define age groups
bins = [17, 22, 26, 30]
labels = ["18-22", "23-26", "27-30"]
df["AgeGroup"] = pd.cut(df["Age"], bins=bins, labels=labels)

# Group by AgeGroup and calculate mean ScienceScore
avg_science_by_group = df.groupby("AgeGroup")["ScienceScore"].mean()
print(avg_science_by_group)

Solution 4

Problem: Merge two DataFrames: one with student IDs and names, another with student IDs and EnglishScore.

students = pd.DataFrame({
    "StudentID": [1, 2, 3],
    "Name": ["Alice", "Bob", "Charlie"]
})

scores = pd.DataFrame({
    "StudentID": [1, 2, 3],
    "EnglishScore": [85, 90, 95]
})

df_merged = pd.merge(students, scores, on="StudentID")
print(df_merged)

⬆️목차이동