๐ Scenario: You work as a data analyst for a retail company. You have sales data with product prices and quantities sold. You want to create new features to better understand the total sales value for each product.
๐ฏ Goal: Create a new feature called total_sales by multiplying price and quantity for each product in the sales data.
๐ What You'll Learn
Use pandas to create and manipulate data.
Create a DataFrame with product sales data.
Add a new column total_sales by multiplying price and quantity.
Print the updated DataFrame to see the new feature.
๐ก Why This Matters
๐ Real World
Feature engineering helps improve data quality and model performance by creating meaningful new columns from existing data.
๐ผ Career
Data scientists and analysts often create new features to help machine learning models understand data better and make better predictions.
Progress0 / 4 steps
1
Create the sales data DataFrame
Create a pandas DataFrame called sales_data with these exact columns and values: product with ['A', 'B', 'C'], price with [10, 20, 15], and quantity with [5, 3, 8].
Pandas
Hint
Use pd.DataFrame with a dictionary containing the columns and their values.
2
Set up a variable for the new feature name
Create a variable called new_feature and set it to the string 'total_sales'.
Pandas
Hint
Just assign the string 'total_sales' to the variable new_feature.
3
Create the new feature by multiplying price and quantity
Add a new column to sales_data with the name stored in new_feature. Set this column to the product of the price and quantity columns.
Pandas
Hint
Use sales_data[new_feature] to create the new column and multiply the two existing columns.
4
Print the updated DataFrame
Print the sales_data DataFrame to display the new total_sales feature.
Pandas
Hint
Use print(sales_data) to show the DataFrame with the new column.
Practice
(1/5)
1. What is the main purpose of feature engineering in pandas?
easy
A. To create new useful columns from existing data
B. To delete unnecessary rows from the dataset
C. To change the data type of columns
D. To visualize data using charts
Solution
Step 1: Understand feature engineering definition
Feature engineering means creating new columns that help models learn better.
Step 2: Compare options with definition
Only To create new useful columns from existing data matches the purpose of creating new useful columns from existing data.
Final Answer:
To create new useful columns from existing data -> Option A
Quick Check:
Feature engineering = create new columns [OK]
Hint: Feature engineering = making new columns from old data [OK]
Common Mistakes:
Confusing feature engineering with data cleaning
Thinking it only changes data types
Mixing it up with data visualization
2. Which of the following is the correct way to create a new column total as sum of columns 2a and 2b in a pandas DataFrame df?
easy
A. df['total'] <- df['2a'] + df['2b']
B. df.total = df.2a + df.2b
C. df['total'] == df['2a'] + df['2b']
D. df['total'] = df['2a'] + df['2b']
Solution
Step 1: Recall pandas syntax for new column
Use df['new_col'] = expression to create new columns.
4. What is wrong with this code to create a new column year from a datetime column date in DataFrame df?
df['year'] = df['date'].year
medium
A. There is no error; the code is correct
B. You must use dt.year to access year from datetime column
C. You should use df.year = df.date.year instead
D. The column date must be converted to string first
Solution
Step 1: Recall how to extract year from datetime column
Use df['date'].dt.year to get year from pandas datetime column.
Step 2: Identify error in code
Code uses df['date'].year which is invalid; .year is not attribute of Series.
Final Answer:
You must use dt.year to access year from datetime column -> Option B
Quick Check:
Use df['date'].dt.year for datetime year [OK]
Hint: Use .dt accessor for datetime column parts [OK]
Common Mistakes:
Using .year directly on Series instead of .dt.year
Converting datetime to string unnecessarily
Assigning to df.year instead of df['year']
5. You have a DataFrame df with columns height_cm and weight_kg. You want to create a new feature bmi using the formula bmi = weight_kg / (height_m)^2, where height in meters is height_cm divided by 100. Which code correctly creates the bmi column?
hard
A. df['bmi'] = df['weight_kg'] / (df['height_cm'] / 100) ** 2
B. df['bmi'] = df['weight_kg'] / df['height_cm'] ** 2 / 10000
C. df['bmi'] = df['weight_kg'] * (df['height_cm'] / 100) ** 2
D. df['bmi'] = df['weight_kg'] / (df['height_cm'] * 100) ** 2