Exploring Relationships Between Variables
EDA is not limited to examining one variable at a time. Investigating relationships between variables helps reveal patterns that may explain differences in performance or behavior.
Comparing Two Numerical Variables
A scatter plot displays the relationship between two numerical variables.
import matplotlib.pyplot as plt
advertising_spend = [1000, 1500, 2000, 2500, 3000, 3500]
sales_revenue = [8000, 10000, 13000, 15000, 18000, 19000]
plt.scatter(advertising_spend, sales_revenue)
plt.xlabel("Advertising Spend")
plt.ylabel("Sales Revenue")
plt.title("Advertising Spend vs Sales Revenue")
plt.show()
An upward pattern suggests that higher advertising expenditure is associated with higher revenue in this example. However, association alone does not prove that advertising caused the increase. Other variables may also influence revenue.
Calculating Correlation
Correlation measures the strength and direction of a linear relationship between two numerical variables.
import pandas as pd
df = pd.DataFrame({
"Advertising": [1000, 1500, 2000, 2500, 3000, 3500],
"Sales": [8000, 10000, 13000, 15000, 18000, 19000],
"Customer_Rating": [3.5, 3.8, 4.0, 4.1, 4.4, 4.6]
})
print(df.corr(numeric_only=True))
Pearson correlation ranges from -1 to +1:
- +1: Perfect positive linear relationship.
- 0: No linear relationship.
- -1: Perfect negative linear relationship.
A correlation near zero does not rule out nonlinear relationships. Correlation can also be influenced by outliers and does not establish causation.
Comparing Numerical Values Across Categories
Grouping numerical data by a category helps identify differences between groups.
product_sales = pd.DataFrame({
"Product": ["Laptop", "Phone", "Tablet", "Laptop", "Phone", "Tablet"],
"Units_Sold": [12, 25, 18, 15, 30, 20]
})
summary = product_sales.groupby("Product")["Units_Sold"].agg(
["mean", "min", "max", "count"]
)
print(summary)
This provides the average, minimum, maximum, and number of observations for each product. Such comparisons can help identify consistently strong categories or groups that require further investigation.
Python for Data Science
Vishalini A








