The Startup
Published in

The Startup

Find your favorite artists in Spotify playlists with Python

Cover image
(source)

Create your Spotify Application

Spotify Developer Application information
Spotify Developer Application information

Install Python libraries

pip install spotipy
pip install pandas
pip install plotly

Script to get data from Spotify

get_spotify_playlist_data.py
  • authenticate: authenticate into the Spotify API with the client keys
  • get_pl_length: helper function to get the number of songs in the playlist (this includes local files)
  • get_tracks_artist_info: retrieve information about the artists of each track of the playlist
  • get_artist_counts: count the frequency of each artist, using the previously retrieved data
  • save_artists_csv: save the artist frequencies in a CSV file
How to get a playlist Spotify URI
How to get a playlist Spotify URI
Spotify API response breakdown
Spotify API response breakdown
JSON response for a single track (with a lot of information cut out)
Portion about a track’s artists

Script to plot the data

analyze_spotify_data.py
  • pre_process_data: load the data from the CSV and sort it by descending order of artist frequencies
  • plot_column_chart: plot a column chart of the artist frequencies with Plotly Express, including the necessary formatting changes

Conclusions

Final column chart
Final column chart

--

--

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
José Fernando Costa

I write about data science to help other people who might come across the same problems