Examples - FE
Simple Python programs to understand Feature Engineering (FE) clearly.
Program 1: Creating a New Feature
import pandas as pd
data = {
"Height": [1.70, 1.65, 1.80],
"Weight": [70, 60, 85]
}
df = pd.DataFrame(data)
# Creating BMI feature
df["BMI"] = df["Weight"] / (df["Height"] ** 2)
print(df)
Output:
Height Weight BMI
0 1.70 70 24.221453
1 1.65 60 22.038567
2 1.80 85 26.234568
Output Concept
A new column BMI is created using Height and Weight.
Program 2: Extracting Date Features
import pandas as pd
data = {
"Order_Date": ["2026-01-15", "2026-03-22", "2026-05-07"]
}
df = pd.DataFrame(data)
df["Order_Date"] = pd.to_datetime(df["Order_Date"])
df["Year"] = df["Order_Date"].dt.year
df["Month"] = df["Order_Date"].dt.month
df["Day"] = df["Order_Date"].dt.day
df["Weekday"] = df["Order_Date"].dt.day_name()
print(df)
Output:
Order_Date Year Month Day Weekday
0 2026-01-15 2026 1 15 Thursday
1 2026-03-22 2026 3 22 Sunday
2 2026-05-07 2026 5 7 Thursday
Output Concept
From one date column, we create Year, Month, Day, and Weekday features.
Program 3: Creating Interaction Features
import pandas as pd
data = {
"Length": [10, 20, 15],
"Width": [5, 8, 6]
}
df = pd.DataFrame(data)
# Creating area feature
df["Area"] = df["Length"] * df["Width"]
print(df)
Output:
Length Width Area
0 10 5 50
1 20 8 160
2 15 6 90
Output Concept
A new feature Area is created by combining Length and Width.
Program 4: Aggregation Features
import pandas as pd
data = {
"Customer": ["A", "A", "B", "B", "C"],
"Purchase_Amount": [1000, 1500, 2000, 2500, 3000]
}
df = pd.DataFrame(data)
customer_summary = df.groupby("Customer")["Purchase_Amount"].agg(
Total_Spending="sum",
Average_Spending="mean",
Purchase_Count="count"
).reset_index()
print(customer_summary)
Output:
Customer Total_Spending Average_Spending Purchase_Count
0 A 2500 1250.0 2
1 B 4500 2250.0 2
2 C 3000 3000.0 1
Output Concept
Customer-level features like Total Spending, Average Spending, and Purchase Count are created.
Program 5: Binning Numerical Data
import pandas as pd
data = {
"Age": [15, 22, 35, 48, 67]
}
df = pd.DataFrame(data)
df["Age_Group"] = pd.cut(
df["Age"],
bins=[0, 18, 35, 60, 100],
labels=["Teen", "Young Adult", "Adult", "Senior"]
)
print(df)
Output:
Age Age_Group
0 15 Teen
1 22 Young Adult
2 35 Young Adult
3 48 Adult
4 67 Senior
Output Concept
Continuous age values are converted into age groups.
Program 6: Complete Feature Engineering Example
import pandas as pd
data = {
"Name": ["John", "Alex", "Ravi"],
"Height": [1.70, 1.65, 1.80],
"Weight": [70, 60, 85],
"Join_Date": ["2024-01-10", "2024-03-15", "2024-05-20"],
"Salary": [50000, 60000, 90000]
}
df = pd.DataFrame(data)
# BMI feature
df["BMI"] = df["Weight"] / (df["Height"] ** 2)
# Date features
df["Join_Date"] = pd.to_datetime(df["Join_Date"])
df["Join_Year"] = df["Join_Date"].dt.year
df["Join_Month"] = df["Join_Date"].dt.month
# Salary category
df["Salary_Level"] = pd.cut(
df["Salary"],
bins=[0, 55000, 80000, 150000],
labels=["Low", "Medium", "High"]
)
print(df)
Output:
Name Height Weight Join_Date Salary BMI Join_Year Join_Month \
0 John 1.70 70 2024-01-10 50000 24.221453 2024 1
1 Alex 1.65 60 2024-03-15 60000 22.038567 2024 3
2 Ravi 1.80 85 2024-05-20 90000 26.234568 2024 5
Salary_Level
0 Low
1 Medium
2 High
Output Concept
This program creates multiple useful features from raw data:
- BMI
- Join Year
- Join Month
- Salary Level
Keywords
Feature Engineering Programs, Feature Engineering using Python, Feature Engineering using Pandas, Python Feature Engineering Tutorial, Feature Creation in Python, Date Feature Extraction Python, Aggregation Features Python, Interaction Features Python, Data Binning in Python, Machine Learning Feature Engineering Programs