We chose Pandas for its ability to read and write data between in-memory data structures and different formats. Pandas makes manipulating and analyzing data a breeze.
This is a continuation of Brief intro into Spotify Web API with Spotipy Python library where we used Spotify’s API to download data to a CSV file. Follow the steps in that article to create your own CSV or use our demo file.
1. Setup project files and dependencies
i. Create directory and change to that directory
cd /Users/Charlie/
mkdir spotify-part-1
cd /spotify-part-1
ii. Download the demo CSV file and upload to your new directory
iii. Install Pandas Data Analysis Library (documentation)
iv. Create directory and change to that directory
v. Install pandas dependency with pip package installer
pip install pandas
2. Start programming
#!/usr/bin/env python3
import pandas as pd
music = pd.read_csv("saved-tracks.csv")
def tracks_by_name(music=zip(music.artists, music.name), artist='Matt Costa'):
""" Track(s) using artist name """
tl = []
for t in music:
if t[0] == artist:
tl.append(f'{t[1]} by {t[0]}')
return tl
def tracks_by_energy(music=zip(music.artists, music.name, music.energy)):
""" Tracks greater than 0.8 energy """
tl = []
for t in music:
if t[2] > 0.8:
tl.append(f'{t[1]} by {t[0]} | Energy: {t[2]}')
return tl
3. Install and launch iPython. I recommend keeping iPython as a part of your global libraries and not in your virtual environment. Don’t forget to ‘run main.py’ every time you make edits, in this case main.py.
pip install ipython
ipython
run main.py
4. Get tracks by artist name
tracks = tracks_by_name()
print(tracks)
5. Get high energy tracks
tracks = energy_tracks()
print(tracks)