9  Pandas Basics Practice

This notebook focuses on DataFrame creation, inspection, selection, filtering, and derived columns.

import pandas as pd

users = pd.DataFrame(
    {
        "user_id": [1, 2, 3, 4, 5],
        "country": ["US", "IN", "US", "DE", "IN"],
        "plan": ["free", "pro", "free", "team", "pro"],
        "sessions": [3, 14, 5, 21, 9],
        "revenue": [0, 29, 0, 99, 29],
    }
)

users

9.1 Exercise 1

Inspect the shape, column names, and dtypes of users.

# TODO: Inspect the table in three different ways.
num_rows, num_cols = users.shape
column_names = users.columns.tolist()
data_types = users.____

print("shape:", (num_rows, num_cols))
print("columns:", column_names)
print("dtypes:\n", data_types)

9.2 Exercise 2

Select only the user_id, plan, and revenue columns.

# TODO: Keep only the three requested columns.
selected_columns = ["user_id", "____", "revenue"]
users_subset = users[__________]
users_subset

9.3 Exercise 3

Filter the rows for paid users only. A paid user has revenue greater than 0.

# TODO: Build a boolean mask, then use it to filter the rows.
paid_mask = users["revenue"] ____ 0
paid_users = users[paid_mask]
paid_users

9.4 Exercise 4

Create a new boolean column called is_power_user for users with at least 10 sessions.

# TODO: Complete the comparison that defines a power user.
users["is_power_user"] = users["sessions"] >= ____
users

9.5 Exercise 5

Create a new column called annual_revenue equal to revenue * 12.

# TODO: Create annual revenue from the monthly revenue column.
users["annual_revenue"] = users["revenue"] * ____
users

9.6 Reflection

Which of these operations felt natural, and which required more deliberate thinking?