How Pandemic Influence Our Mood? Let’s Ask Spotify

Krisna Renaldi
5 min readAug 26, 2021

--

We can utilize Spotify’s API to track mood of a nation that can be levelized to detect happiness.

Photo by Andrea Piacquadio from Pexels

Music has become part of our daily life. We can see people are listening to music in commuter, cars, jogging, restaurants, gym, mall, cafe, works. It can bring cheerful, funny, melancholy, anger, ironic, joyous and even depressing.

According to this research, people display a balance of mood incongruency and congruency in their listening. Congruence is when we listen to songs similar to how we’re feeling, while incongruence is the phenomenon of trying to lift ourselves out of a bad mood with upbeat music.

With this scene in mind, I want to track mood Spotify listener in Indonesia, — since I am Indonesian, before pandemic and after pandemic. Are they happy, depress, ‘santuy’ (chill), worry or else. And luckily Spotify give us daily top charts data (Top 200).

Spotifychart Top 200

Data Collection

Spotifycharts gives Top 200 based on Country and daily/weekly. I have scrapped data all day long for year 2019 and 2020. The process is pretty straight forward, you can use Scrapy or make code by yourself. I made my own code. The scrapping consist of two section, getting date charts and scrap the charts.

I made function that take argument number of year.

Using BeautifulSoup

Next, the date is required to make URL to get the csv. https://spotifycharts.com/regional/{REGIONAL} /daily/{DATE}.
Here is the snippet:

I have scrapped the list from 830URLs resulting in a dataset with 145,814 rows with song names, artist names, streams, song position. There are over 932 unique songs, over 400 solo and group artists.
Here is the snapshot of the sample data:

Top 200 chart daily

Just to play with data a little bit, we could find top 20 artist that has been featured in the top list 200 across days combined 2019 and 2020.

Pamungkas is the top feature artist.

Also, we can get information about average number of streams per month for 2019 and 2020.

The average streams increase significantly in 2019, and as we entered pandemic at March 2020, the average number decrease and slowly to going up and stable in 61.000 average streams in December.

Spotify API

We need to get other relevant music data for the scrapped songs. Fortunately, Spotify provides API to access all of the music data. After necessary process to create client id and client secret, I am able to get music data. I used Spotipy to extract audio feature from each songs.

When we extract a song from Spotify API, it will give us audio features data, and this is the brief description:

Values of all the features lies in the range 0–1 except loudness and tempo. And for this analysis, I focused mainly on two attributes: energy and valence.

Data Analysis: Mood measurement

From the csv files earlier, using Spotipy we can extract each songs to get energy and valence score. Combine energy and valence gives us better picture of how happy (or depressing) a song might be. A song with high-valence but low-energy might be considered as ‘peaceful’ or ‘calm’ than ‘happy’ or ‘joy’. Likewise, a song that is low-valence and high-energy might be considered more ‘angry’ or ‘turbulent’ than ‘sad’. And a song with low-valence and low-energy is considerably the most depressing song. I plot valence against energy for the whole songs, the ‘sad’ songs will be the ones closest to lowest-valence and lowest-energy (0,0):

The red dots are the songs with energy score < 0.5 or valence score < 0.5 and green dots are the opposite.

To quantify the musical happiness (or sadness), we calculate the song’s distance (in term of valence and energy) from the point (0,0) — the lower this distance is, the sadder the song. Here is the table of musical sadness and happiness.

“Cloud 9” is the happiest song
“Hilmira” is the saddest song

Now we explore the valence score. Let us weigh the valence scores by stream counts. This is the way to make a song that has thousands streams contributes less than a song with hundreds thousand streams. I made raw streams to fractional of total streams. This way valence means are independent of total stream counts.

weigh valence score = value of stream of a track / total sum of stream on that day

Lastly, I multiply valence and stream share and take the mean. It gives the average mood for that day. I plot all average values across 2019 to 2020.

Mean positivity

I noticed several things from this line chart:

  • July 2019 was the peak of happiness and May 2020 was the second, it was Idul Fitri celebration.
  • Started mid June 2020, the listener’s mood tend to sad or depress, probably it was the early quarantine adaptation.
  • At the end of 2019 and 2020, listeners mood were decrease to sad/depress but 2020 was the saddest.

The valence metric may be seem accurate as one would hope. But music and mood do not always connect. People listen to happy music when they are sad, sad music when they’re happy, and may not listen to music at all in either case.

Conclusion

Spotify’s data is very fun to play with. And I thought we could take insight to upper level. I do not include lyrics analysis. We can get lyrics for each songs using Genius API, or scrap by home made code. Then, expect the sentiment analysis with the help of NLP. We also, can get another insight how is the musical taste of Spotify users in Indonesia. Plot the genres, audio feature distribution analysis, and many more.

Thank you for your time to read my first blog. This was an awesome process. You can follow me to keep up to date with upcoming articles.

Let data inspire you.

--

--

Krisna Renaldi

Web Developer, love back-end, math, physics and data scientiest