Skip to content
Spotify Music Data
  • AI Chat
  • Code
  • Report
  • Spotify Music Data

    This dataset consists of ~600 songs that were in the top songs of the year from 2010 to 2019 (as measured by Billboard). You can explore interesting song data pulled from Spotify such as the beats per minute, amount of spoken words, loudness, and energy of every song.

    Not sure where to begin? Scroll to the bottom to find challenges!

    import pandas as pd
    
    pd.read_csv("spotify_top_music.csv", index_col=0)

    Data dictionary

    VariableExplanation
    0titleThe title of the song
    1artistThe artist of the song
    2top genreThe genre of the song
    3yearThe year the song was in the Billboard
    4bpmBeats per minute: the tempo of the song
    5nrgyThe energy of the song: higher values mean more energetic (fast, loud)
    6dnceThe danceability of the song: higher values mean it's easier to dance to
    7dBDecibel: the loudness of the song
    8liveLiveness: likeliness the song was recorded with a live audience
    9valValence: higher values mean a more positive sound (happy, cheerful)
    10durThe duration of the song
    11acousThe acousticness of the song: likeliness the song is acoustic
    12spchSpeechines: higher values mean more spoken words
    13popPopularity: higher values mean more popular

    Source of dataset.

    import pandas as pd from sqlalchemy import create_engine

    Read the CSV file into a pandas dataframe

    df = pd.read_csv("spotify_top_music.csv")

    Create a connection to the database

    engine = create_engine('postgresql://username:password@localhost:5432/database_name')

    Write the dataframe to the database table

    df.to_sql('table_name', engine, if_exists='replace')

    import pandas as pd
    from sqlalchemy import create_engine
    import psycopg2
    
    # Read the CSV file into a pandas dataframe
    df = pd.read_csv("spotify_top_music.csv")
    
    # Create a connection to the database
    try:
        engine = create_engine('postgresql://gusflores:abril1320@localhost:5432/spotify')
        conn = engine.connect()
        print("Connection established successfully!")
    except psycopg2.OperationalError as e:
        print(f"Error: {e}")
    
    # Write the dataframe to the database table
    try:
        df.to_sql('table_name', engine, if_exists='replace')
        print("Data written to PostgreSQL successfully!")
    except Exception as e:
        print(f"Error: {e}")
    finally:
        conn.close()