Skip to article frontmatterSkip to article content

Exercise 4 - Introduction to Pandas

  • πŸ† 20 points available
  • ✏️ Last updated on 9/16/2025

▢️ First, run the code cell below to import unittest, a module used for 🧭 Check Your Work sections and the autograder.

# DO NOT MODIFY THE CODE IN THIS CELL
import unittest

tc = unittest.TestCase()

🎯 Challenge 1: Import Pandas and NumPy¢

πŸ‘‡ TasksΒΆ

  • βœ”οΈ Import the following Python packages.
    1. pandas: Use alias pd.
    2. numpy: Use alias np.
# YOUR CODE BEGINS

# YOUR CODE ENDS

🧭 Check Your Work¢

Run the code cell below to test your solution.

  • βœ”οΈ If the code cell runs without errors, you’re good to move on.
  • ❌ If the code cell produces an error, review your code and fix any mistakes.
_test_case = "import-pandas-numpy"
_points = 5

tc.assertTrue(
    "pd" in globals(), "Check whether you have correctly import Pandas with an alias."
)
tc.assertTrue(
    "np" in globals(), "Check whether you have correctly import NumPy with an alias."
)

🎯 Challenge 2: Create a Pandas Series¢

πŸ‘‡ TasksΒΆ

  • βœ”οΈ Create a new Pandas Series named sample_series with the following four values: -20, -10, 10, 20

πŸš€ HintΒΆ

The code below creates a new Pandas Series with the values 1 and 2.

my_new_series = pd.Series([1, 2])
# YOUR CODE BEGINS

# YOUR CODE ENDS

print(sample_series)

🧭 Check Your Work¢

Run the code cell below to test your solution.

  • βœ”οΈ If the code cell runs without errors, you’re good to move on.
  • ❌ If the code cell produces an error, review your code and fix any mistakes.
_test_case = "create-a-pandas-series"
_points = 5

pd.testing.assert_series_equal(sample_series, pd.Series(x * 10 for x in [-2, -1, 1, 2]))

🎯 Challenge 3: Create a Pandas DataFrame¢

πŸ‘‡ TasksΒΆ

  • βœ”οΈ You are given two lists - brands and rankings that contain the names of make-up products and the number of reviews on Sephora.com.
  • βœ”οΈ Using the two lists, create a new Pandas DataFrame named df_brands that has the following two columns:
    1. brand: Names of the brands
    2. ranking: Ranking of the brands
  • βœ”οΈ Note that the column names are singular.

πŸš€ HintΒΆ

The code below creates a new Pandas DataFrame from two series.

my_new_dataframe = pd.DataFrame({
    "column_one": my_series1,
    "column_two": my_series2
})

πŸ”‘ Expected OutputΒΆ

brandranking
0Apple1
1Amazon2
2Google3
brands = ["Apple", "Amazon", "Google"]
rankings = [1, 2, 3]

# YOUR CODE BEGINS

# YOUR CODE ENDS

display(df_brands)

🧭 Check Your Work¢

Run the code cell below to test your solution.

  • βœ”οΈ If the code cell runs without errors, you’re good to move on.
  • ❌ If the code cell produces an error, review your code and fix any mistakes.
_test_case = "create-a-pandas-dataframe-1"
_points = 5

pd.testing.assert_frame_equal(
    df_brands.reset_index(drop=True),
    pd.DataFrame(
        {"brand": {0: "Apple", 1: "Amazon", 2: "Google"}, "ranking": {0: 1, 1: 2, 2: 3}}
    ),
)

🎯 Challenge 4: Create a Pandas DataFrame¢

πŸ‘‡ TasksΒΆ

  • βœ”οΈ You are given two lists - product_names and num_reviews that contain the names of make-up products and the number of reviews on Sephora.com.
  • βœ”οΈ Using the two lists, create a new Pandas DataFrame named df_top_products that has the following two columns:
    1. "product_name": Names of the products
    2. "num_review": Number of reviews
  • βœ”οΈ Note that the column names are singular.

πŸš€ HintΒΆ

The code below creates a new Pandas DataFrame from two series.

my_new_dataframe = pd.DataFrame({
    "column_one": my_series1,
    "column_two": my_series2
})
product_names = [
    "Laneige Lip Sleeping Mask",
    "The Ordinary Hyaluronic Acid 2% + B5",
    "Laneige Lip Glowy Balm",
    "Chanel COCO MADEMOISELLE Eau de Parfum",
]

num_reviews = [12715, 2274, 2766, 724]

# YOUR CODE BEGINS

# YOUR CODE ENDS

display(df_top_products)

🧭 Check Your Work¢

Run the code cell below to test your solution.

  • βœ”οΈ If the code cell runs without errors, you’re good to move on.
  • ❌ If the code cell produces an error, review your code and fix any mistakes.
_test_case = "create-a-pandas-dataframe-2"
_points = 5

pd.testing.assert_frame_equal(
    df_top_products.reset_index(drop=True),
    pd.DataFrame(
        {
            "product_name": {
                0: "Laneige Lip Sleeping Mask",
                1: "The Ordinary Hyaluronic Acid 2% + B5",
                2: "Laneige Lip Glowy Balm",
                3: "Chanel COCO MADEMOISELLE Eau de Parfum",
            },
            "num_review": {0: 12715, 1: 2274, 2: 2766, 3: 724},
        }
    ),
)