0
0
Data Analysis Pythondata~15 mins

Extracting with str.extract (regex) in Data Analysis Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Extracting with str.extract (regex)
📖 Scenario: You work in a store and have a list of product codes mixed with letters and numbers. You want to pull out just the numbers from each code to analyze sales data.
🎯 Goal: Build a small program that extracts the numeric part from each product code using str.extract with a regular expression.
📋 What You'll Learn
Create a pandas Series called product_codes with the exact values: 'A123', 'B456', 'C789', 'D012'
Create a regex pattern string called pattern that matches one or more digits
Use str.extract on product_codes with pattern to get the numbers
Print the extracted numbers as a pandas Series
💡 Why This Matters
🌍 Real World
Stores and businesses often have product codes mixing letters and numbers. Extracting parts of these codes helps analyze sales or inventory.
💼 Career
Data analysts and scientists use regex extraction to clean and prepare text data for analysis in many industries.
Progress0 / 4 steps
1
Create the product codes data
Create a pandas Series called product_codes with these exact values: 'A123', 'B456', 'C789', 'D012'.
Data Analysis Python
Hint

Use pd.Series and pass a list of the exact strings.

2
Create the regex pattern
Create a string variable called pattern that matches one or more digits using the regex '(\d+)'.
Data Analysis Python
Hint

The regex '(\d+)' matches one or more digits and captures them.

3
Extract numbers using str.extract
Use str.extract on product_codes with the pattern to extract the numbers. Save the result in a variable called numbers.
Data Analysis Python
Hint

Use product_codes.str.extract(pattern) and select the first column with [0] to get a Series.

4
Print the extracted numbers
Print the numbers Series to display the extracted numeric parts.
Data Analysis Python
Hint

Use print(numbers) to show the extracted numbers.