I want to get the audio features of songs in spotify.
features = {}
all_track_ids = list(df['song_id'].unique())
start = 0
num_tracks = 100
while start < len(all_track_ids):
tracks_batch = all_track_ids[start:start+num_tracks]
features_batch = sp.audio_features(tracks_batch)
features.update({ track_id : track_features
for track_id, track_features in zip(tracks_batch, features_batch) })
start += num_tracks
features['1mqlc0vEP9mU1kZgTi6LIQ']
The results(this is an example of a song):
{'danceability': 0.693,
'energy': 0.911,
'key': 9,
'mode': 1,
'speechiness': 0.0305,
'acousticness': 0.183,
'instrumentalness': 0.000107,
'liveness': 0.3,
'valence': 0.982,
'tempo': 125.895,
'type': 'audio_features',
'id': '1mqlc0vEP9mU1kZgTi6LIQ',
'uri': 'spotify:track:1mqlc0vEP9mU1kZgTi6LIQ',
'track_href': 'https://api.spotify.com/v1/tracks/1mqlc0vEP9mU1kZgTi6LIQ',
'analysis_url': 'https://api.spotify.com/v1/audio-analysis/1mqlc0vEP9mU1kZgTi6LIQ',
'duration_ms': 216520,
'time_signature': 4}
Now, I tried to create a dataframe with these songs and their features, this is a sample:
tracks = pd.DataFrame.from_dict(zip(tracks_batch, features_batch))
tracks
|song_id|features |
|:------|:--------------------------------------:|
|link |{'valence:0.23', 'danceability:0.21',...|
How can I make this look like:
|song_id|valence|danceability|
|link |0.23 |0.21 |
That is how to convert the dictionary into a Dataframe, whose column will be a feature of a song? (I have a csv with a great deal of songs' ids.)