Creating and Transforming Features
Existing columns often contain information that becomes more useful after transformation. Creating derived features can make important relationships easier for a model to learn.
Creating Numerical Features
Suppose a retail dataset contains product prices and quantities.
import pandas as pd
df = pd.DataFrame({
"Product": ["Laptop", "Phone", "Tablet"],
"Price": [55000, 30000, 25000],
"Quantity": [2, 5, 3],
"Discount": [5000, 3000, 2000]
})
df["Gross_Amount"] = df["Price"] * df["Quantity"]
df["Net_Amount"] = df["Gross_Amount"] - df["Discount"]
print(df)
The Gross_Amount and Net_Amount columns provide derived information that may be more directly related to a business objective than the original columns alone.
Before creating a feature, confirm that the calculation matches the meaning of the data. For instance, determine whether a discount applies per item or to the entire order.
Extracting Date Features
Dates can contain useful information about seasonality, timing, and customer behavior.
df = pd.DataFrame({
"Order_Date": [
"2026-01-15",
"2026-02-20",
"2026-03-10"
]
})
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["Day_Of_Week"] = df["Order_Date"].dt.dayofweek
print(df)
The dayofweek property represents Monday as 0 and Sunday as 6.
Depending on the problem, you might also derive order age, time since the previous purchase, or whether a transaction occurred during a weekend. Ensure that date-based features use only information that would have been available at prediction time.
Applying Mathematical Transformations
Some numerical variables are highly skewed, meaning that many observations are concentrated in one region while a small number have much larger values.
A logarithmic transformation can reduce positive skewness:
import numpy as np
import pandas as pd
df = pd.DataFrame({
"Income": [25000, 30000, 45000, 60000, 250000]
})
df["Log_Income"] = np.log1p(df["Income"])
print(df)
The log1p() function calculates the natural logarithm of one plus the input. It is useful for non-negative values, particularly when zeros are present. The transformation changes the scale of the variable, so its interpretation should be considered carefully.
Python for Data Science
Vishalini A








