import pandas as pd import matplotlib.pyplot as plt import seaborn as sns OUT = "eda_outputs" import os os.makedirs(OUT, exist_ok=True) df = pd.read_csv("PaavoDataset.csv", encoding="latin1", sep=";") df.columns = df.columns.str.strip() # remove stray leading/trailing spaces df = df[~df["Area"].str.strip().str.startswith("MK")].copy() # Drop regional summary rows (e.g. "MK01 Uusimaa") #Summary stats print("--- Info ---") print(df.info()) print("\n--- Descriptive statistics ---") desc = df.describe().T print(desc) desc.to_csv(f"{OUT}/summary_statistics.csv") # Distributions of key variables key_vars = [ "Inhabitants", "Average age of inhabitants", "Average income of inhabitants", "Median income of inhabitants", "Average size of households", ] fig, axes = plt.subplots(len(key_vars), 1, figsize=(8, 4 * len(key_vars))) for ax, col in zip(axes, key_vars): sns.histplot(df[col], kde=True, ax=ax, color="#6fa8dc") ax.set_title(f"Distribution of {col}") plt.tight_layout() fig.savefig(f"{OUT}/01_distributions.png", dpi=150) plt.close(fig) # Correlation heatmap (numeric columns only) numeric_df = df.select_dtypes(include="number") corr = numeric_df.corr() fig, ax = plt.subplots(figsize=(14, 12)) sns.heatmap(corr, cmap="coolwarm", center=0, annot=False, ax=ax) ax.set_title("Correlation heatmap") fig.savefig(f"{OUT}/02_correlation_heatmap.png", dpi=150, bbox_inches="tight") plt.close(fig) # Top areas by income and population top_income = df.nlargest(15, "Average income of inhabitants")[["Area", "Average income of inhabitants"]] top_population = df.nlargest(15, "Inhabitants")[["Area", "Inhabitants"]] print("\n--- Top 15 areas by average income ---") print(top_income) print("\n--- Top 15 areas by population ---") print(top_population) # Income category composition (stacked bar, top 20 by inhabitants) income_cats = [ "Inhabitants belonging to the lowest income category", "Inhabitants belonging to the middle income category", "Inhabitants belonging to the highest income category", ] top20_income = df.nlargest(20, "Inhabitants").set_index("Area")[income_cats].iloc[::-1] top20_income.columns = ["Lowest income cat.", "Middle income cat.", "Highest income cat."] fig, ax = plt.subplots(figsize=(10, 8)) top20_income.plot(kind="barh", stacked=True, color=["#6fa8dc", "#cc6659", "#e69138"], ax=ax) ax.set_title("Income category composition - top 20 areas by population") ax.set_xlabel("Number of inhabitants") fig.savefig(f"{OUT}/03_income_categories_top20.png", dpi=150, bbox_inches="tight") plt.close(fig) # Household income category composition (same style, top 20 by households) household_cats = [ "Households belonging to the lowest income category", "Households belonging to the middle income category", "Households belonging to the highest income category", ] top20_households = (df.nlargest(20, "Households").set_index("Area")[household_cats].iloc[::-1]) top20_households.columns = ["Lowest income cat.", "Middle income cat.", "Highest income cat."] fig, ax = plt.subplots(figsize=(10, 8)) top20_households.plot(kind="barh", stacked=True, color=["#6fa8dc", "#cc6659", "#e69138"], ax=ax) ax.set_title("Household income category composition - top 20 areas by number of households") ax.set_xlabel("Number of households") fig.savefig(f"{OUT}/04_household_income_categories_top20.png", dpi=150, bbox_inches="tight") plt.close(fig) # Inhabitants per residential building (housing density proxy) df["Inhabitants per residential building"] = ( df["Inhabitants"] / df["Residential buildings"].replace(0, pd.NA) ) density_top20 = df.nlargest(20, "Inhabitants per residential building")[ ["Area", "Inhabitants per residential building"] ].set_index("Area") fig, ax = plt.subplots(figsize=(9, 8)) density_top20.sort_values("Inhabitants per residential building").plot( kind="barh", legend=False, color="#6fa8dc", ax=ax ) ax.set_title("Top 20 areas by inhabitants per residential building)") ax.set_xlabel("Inhabitants per residential building") fig.savefig(f"{OUT}/05_inhabitants_per_building_top20.png", dpi=150, bbox_inches="tight") plt.close(fig) # Accumulated purchasing power of inhabitants purchasing_top20 = df.nlargest(20, "Accumulated purchasing power of inhabitants")[ ["Area", "Accumulated purchasing power of inhabitants"] ].set_index("Area") fig, ax = plt.subplots(figsize=(9, 8)) purchasing_top20.sort_values("Accumulated purchasing power of inhabitants").plot( kind="barh", legend=False, color="#e69138", ax=ax ) ax.set_title("Top 20 areas by accumulated purchasing power of inhabitants") ax.set_xlabel("Accumulated purchasing power (EUR)") fig.savefig(f"{OUT}/06_purchasing_power_top20.png", dpi=150, bbox_inches="tight") plt.close(fig) print(f"\nAll plots and summary CSV saved to ./{OUT}/")