Afrobeats Data Exploration with Spotify Python API

Afrobeats genre of music has been on the rise as one of the most popular genres in the last few years. This is also evident in Burnaboy’s win of the 2021 Grammy’s best world music album category. The Nigerian powerhouse grabbed one of the most significant awards of the night. You can get more information about the Afrobeats genre here.

Afrobeats is an umbrella term used to describe popular music that emanates from West Africa. They are a fusion of African music with the more mainstream genres of music such as Pop, RnB, and the likes. Hence, the artists describe their music as Afrofusion (as Burnaboy describes his type of music) or like many other artists describe theirs as Afropop. Afrobeats was curled from the original Afrobeat “without the s” which is most popular with world-renowned Fela Anikulapo Kuti and his band.

In this blog, we will be exploring data of some of the recent popular Afrobeats songs from the top artists. We will utilize Spotify API for data extraction. We will also utilize various python libraries for analysis and visualization of the data. Tableau will be utilized for further visualization as well.

Here are the steps required to extract Spotify data:

  1. First, log on to Spotify with your credentials, the same credentials can be used to login into the developer account. If you do not have a Spotify account, you can create one.
Spotify API

2. Check the details about Spotify API. Spotify has great documentation for their API here. While logged into the developer account, you will need to obtain your client ID and secret client ID. You will initiate this by clicking on the green button “create an app” and then fill out the forms that follow with the appropriate information.

3. Retrieve your client ID and secret ID within your developer account. You can get to this by clicking on the app that you have just created. The image is shown below. Click on the green button “show client secret ID to get the secret ID).

Spotify for Developers

4. Go to your Spotify account and create a playlist. The playlist data is what will be extracted. The playlist contains tracks in albums released by 7 of the top Nigerian Afrobeats Artist in 2020. We used an existing public playlist; you can find that here.

5. Next, check on how to use Spotipy library to extract data from the playlist created. Spotipy is a lightweight Python library for Spotify web API. It offers easy functionality as regards getting full access to all music data available on the Spotify platform. You can check Spotipy’s documentation here.

Writing the code: Find source code here on Github.

The next step is following the documentation to write the code. You can write the code using an online IDE such as repl. In our own case, we wrote the code using Jupyter notebook via the anaconda distribution. Follow the steps below to write your code:

  1. Import all required libraries for data manipulation, extraction, and visualization. Ensure that the libraries are installed in your virtual environment if th:

2. We need to authenticate and connect to Spotify’s API. To do so, we need our “Client ID” and “Client Secret ID”. The client ID servers as a username and secret ID is the password unique to your user to authenticate with the Spotify API. The details are below:

3. Next, create a function for the playlist. Within that function, there is a for loop to get all the tracks.

In the last line of code above, where it says ids = TrackIDs(‘username’, ‘4MRgvByeNtyl2LcUjDpbE1’), the first variable is the username of the person who created the playlist. Although this is a public playlist, I’d imported the playlist into my library. The other variable is the playlist URL which can be found by hitting the setting button, the share link has the playlist URL.

4. Now, we can check the number of tracks we have if it matches the number of songs in the playlist created. We have 90 tracks on the playlist

The print(ids) will list all the individual ids of each track within the playlist.

5. The next step is to create a function used to grab all track ids. This will include the metadata such as track name; album; artist and the likes as well as other specific features such as danceability; acousticness; liveness; loudness and many others.

6. Next, we loop over each track and get all the metadata and features for each track.

7. Next, the data is wrapped into a data frame, convert to CSV, and save in a directory of choice.

Now that the data is extracted, exploratory data analysis can be performed with the use of the dataset. Here is what the dataset looks like:

EXPLORATION AND VISUALIZATION

For exploratory data analysis, the first step would be to read the file from its location.

We explored our data using the Pandas library to check for null values and drop columns that are irrelevant to our analysis.

We checked for outliers within the data.

To take it an inch further, we decided to visualize our data in Tableau. For our analysis and visualization, we only utilized 5 out of the 16 features/columns extracted from Spotify. The features are:

Danceability: this shows how danceable or rhythmic a track is. It is measured based on the mixture of musical features such as rhythm stability, beat strength, tempo, and overall regularity. According to our data, a value of 1.0 is the most danceable and 0.0 is the least danceable.

Popularity: this describes how much a track is liked, admired, or supported by the listeners. In our data, the track with a value below 40 is less popular than the track with a value greater than 60.

Energy: Energy is a measure of activity and intensity. Usually, energetic tracks or artists are somewhat fast, noisy and upbeat.

Boringness: In our analysis, as inspired by Juan De Dios Santos, boringness is a combination of four features; energy, danceability, tempo, and loudness. Typically, a good party often involves loud music, a nice tempo that brings out the energy in the party guests to get them in a dancing mood.

What are the Top 10 danceable songs in the playlist?

Artist Popularity

This is a Tableau Visualization. We guess that settles the argument of who is the most popular Afrobeats artist in Nigeria

Most Energetic artist

Although a sample dataset was utilized for this visualization, it is safe to assume that this bubble chart is a reflection of the bigger Afrobeats picture.

Top Boring Songs.

As described above, boringness is a combination of four features; energy, danceability, tempo and loudness. Boringness = [Loudness] + [Tempo] + ([Energy]*100) + ([Danceability]*100)

FULL DASHBOARD IMAGE

Click here for the full interactive dashboard for detailed visualization. The Tableau dashboard includes an embedded URL action that, upon selection, takes you to a web player which plays the track selected. Please feel free to download it for your review.

DISCLAIMER: Please note that this analysis is based on a sample data and does not represent the entirety of Afrobeats genre in West Africa and Nigeria in particular.

References:

https://theculturetrip.com/africa/nigeria/articles/an-introduction-to-afrobeats-nigerias-beloved-music-genre/

https://en.wikipedia.org/wiki/Afrobeat#:~:text=Afrobeat%20is%20a%20music%20genre,complex%20intersecting%20rhythms%2C%20and%20percussion

https://docs.python.org/3/

Co-collaborators on this project:

Oyindamola Odulaja

Olu Ojo

Olu is a budding Data Scientist who is very passionate about using his skills to make change within his community. Self-starter, Passionate about learning

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store