Practice: Introduction to Pandas

« Read the chapter

Q1. Build a small table

BeginnerCoding

Create a DataFrame with columns item (pen, book, bag) and price (10, 50, 300). Print it.

Goal: Create a DataFrame.

Q2. Total of a column

BeginnerCoding

Using the DataFrame with item and price from before, print the total of the price column.

Goal: Summarise a column.

Q3. Shape of a table

BeginnerCoding

Make a DataFrame with 4 rows and 3 columns and print its shape.

Goal: Inspect shape.

Q4. Filter high scores

IntermediateCoding

Given names Asha, Ravi, Meena with scores 88, 72, 95, print the names (as a list) of students with a score above 80.

Goal: Filter with a boolean mask.

Q5. Add a percentage column

IntermediateCoding

Marks are 45, 30, 48 out of 50. Add a column pct equal to marks * 2 and print the pct list.

Goal: Create columns.

Q6. Average per group

IntermediateCoding

Rows: Pune 88, Delhi 72, Pune 96, Delhi 80. Print the average score for Pune.

Goal: Use groupby.

Q7. Fill missing values

IntermediateCoding

A Series is [1, NaN, 3]. Replace the missing value with 0 and print the result as a list of floats.

Goal: Handle missing data.

Q8. Sort descending

IntermediateCoding

Sort the Series [5, 9, 1] from largest to smallest and print it as a list.

Goal: Sort values.

Q9. Series sum

BeginnerOutput

What does this print?

Code
import pandas as pd
s = pd.Series([2, 4, 6])
print(s.sum())

Goal: Predict Series methods.

Q10. Series arithmetic

BeginnerOutput

What does this print?

Code
import pandas as pd
s = pd.Series([1, 2, 3])
print((s * 10).tolist())

Goal: Predict vectorised maths.

Q11. Label lookup

BeginnerOutput

What does this print?

Code
import pandas as pd
s = pd.Series([7, 8, 9], index=["a", "b", "c"])
print(s["b"])

Goal: Use index labels.

Q12. iloc slice

IntermediateOutput

What does this print?

Code
import pandas as pd
s = pd.Series([10, 20, 30, 40])
print(s.iloc[1:3].tolist())

Goal: Use iloc.

Q13. loc slice

IntermediateOutput

What does this print?

Code
import pandas as pd
s = pd.Series([10, 20, 30, 40])
print(s.loc[1:3].tolist())

Goal: Contrast loc and iloc.

Q14. Mask and count

IntermediateOutput

What does this print?

Code
import pandas as pd
df = pd.DataFrame({"x": [3, 8, 5, 10]})
print(len(df[df["x"] > 4]))

Goal: Filter and count.

Q15. Missing mean

IntermediateOutput

What does this print?

Code
import pandas as pd
import numpy as np
s = pd.Series([4, np.nan, 8])
print(s.mean())

Goal: Know NaN handling.

Q16. value_counts

IntermediateOutput

What does this print?

Code
import pandas as pd
s = pd.Series(["a", "b", "a", "a"])
print(s.value_counts().to_dict())

Goal: Count values.

Q17. Combine two conditions

AdvancedOutput

What does this print?

Code
import pandas as pd
df = pd.DataFrame({"age": [20, 30, 40], "score": [90, 50, 80]})
print(df[(df["age"] > 25) & (df["score"] > 60)]["age"].tolist())

Goal: Combine masks.

Q18. Broken filter

IntermediateDebugging

This code raises an error. Fix it so it prints the names with age above 20 and score above 60.

Code
import pandas as pd
df = pd.DataFrame({"name": ["A", "B", "C"], "age": [25, 19, 30], "score": [70, 90, 40]})
print(df[df["age"] > 20 and df["score"] > 60]["name"].tolist())

Goal: Fix boolean masks.

Q19. Sort is not saved

IntermediateDebugging

The program should print the scores sorted from low to high but prints them unsorted. Fix it.

Code
import pandas as pd
df = pd.DataFrame({"score": [30, 10, 20]})
df.sort_values("score")
print(df["score"].tolist())

Goal: Know methods return new objects.

Q20. Wrong column selection

BeginnerDebugging

This code raises an error when selecting two columns. Fix it so it prints (3, 2).

Code
import pandas as pd
df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]})
print(df["a", "b"].shape)

Goal: Select several columns.