Python Central HubSign in

Data Analytics with Python / Phase 10: Data Analytics Projects

Spotify Song Popularity Analysis

Analyze audio features, popularity distribution, and relationships between features and popularity.

Goal

Given Spotify song features, answer:

  • What does popularity distribution look like?
  • Which audio features correlate with popularity?
  • Do genres differ in average popularity?

Analysis pipeline

diagram Spotify popularity analysis pipeline mermaid
From raw audio features to the traits that correlate with a hit.

Step 1: Load

Python
import pandas as pd
 
df = pd.read_csv("data/spotify.csv")
print(df.head())

Step 2: Popularity distribution

Python
import seaborn as sns
import matplotlib.pyplot as plt
 
plt.figure(figsize=(7, 4))
sns.histplot(df["popularity"], bins=30, kde=True)
plt.title("Popularity distribution")
plt.tight_layout()
plt.show()

Step 3: Correlations

Python
import seaborn as sns
import matplotlib.pyplot as plt
 
num = df.select_dtypes(include="number")
 
plt.figure(figsize=(8, 6))
sns.heatmap(num.corr(), cmap="coolwarm", center=0)
plt.title("Correlation heatmap")
plt.tight_layout()
plt.show()

Visualize it

sketch Popularity distribution p5.js
Most songs cluster in the middle; only a few reach very high popularity.

Deliverable

  • Top correlated features
  • Whether correlations are meaningful or driven by outliers

Check your understanding

quizCheck yourself
  1. Why check a scatter plot as well as the corr() number?

    Show answer

    A — A strong correlation can be driven by a couple of outlier songs — The project asks whether correlations are meaningful or driven by outliers.

  2. What does num = df.select_dtypes(include="number") do before the heatmap?

    Show answer

    A — Keeps only numeric columns so corr() works — Correlation needs numeric columns, so text columns such as genre are excluded.

  3. Why does the heatmap use center=0 with cmap="coolwarm"?

    Show answer

    A — Zero correlation gets the neutral color and positive and negative are shown in opposite colors — A diverging colormap centered at zero makes the sign of each correlation visible.

  4. What does sns.histplot(df["popularity"], bins=30, kde=True) show?

    Show answer

    A — The shape of the popularity distribution with a smooth density curve — It answers the first question, what the popularity distribution looks like.

🧪 Try It Yourself

Exercise 1 – Summary statistics

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Correlation matrix

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Select only numeric columns

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did