I’m working with the Spotify API to extract audio characteristics from songs. I have a bunch of track IDs and I want to get their audio features like energy, tempo, etc. Here’s my code:
audio_data = {}
song_ids = list(music_df['track_id'].unique())
index = 0
batch_size = 100
while index < len(song_ids):
current_batch = song_ids[index:index+batch_size]
batch_features = spotify_client.audio_features(current_batch)
audio_data.update({song_id: song_data
for song_id, song_data in zip(current_batch, batch_features)})
index += batch_size
# Check one entry
audio_data['1mqlc0vEP9mU1kZgTi6LIQ']
# Try to convert to DataFrame
result_df = pd.DataFrame.from_dict(audio_data, orient='index')
When I try to create the DataFrame, I get an AttributeError saying ‘NoneType’ object has no attribute ‘items’. The problem seems to be that some values in my dictionary are None instead of the expected feature dictionaries.
I want to transform my nested dictionary structure into a flat DataFrame where each audio feature becomes a separate column. Instead of having one column with all features bundled together, I need individual columns for each metric. How can I handle the None values and properly flatten this data structure?
yea, same thing happened to me. spotify’s api returns None when a track doesnt have audio features. just filter out the None values before creating your dataframe: audio_data = {k: v for k, v in audio_data.items() if v is not None} then use pd.DataFrame.from_dict like normal.
Had this exact problem last month with a music recommendation system. Those None values happen when you’ve got invalid track IDs or tracks Spotify hasn’t analyzed yet. Here’s what fixed it for me: add a validation step right after you get the batch features but before updating your dictionary. Make sure each item in batch_features isn’t None AND has the keys you need like ‘energy’ and ‘tempo’. Sometimes the API sends back empty dictionaries instead of None, which’ll also break your DataFrame. Once you’ve cleaned the data, use pd.DataFrame.from_dict with orient=‘index’ - that’ll give you the flat structure with separate columns for each audio feature.
This happens because Spotify’s API returns None for tracks without audio features - pretty common issue. Instead of filtering after, handle it during batch processing. Modify your dictionary comprehension to skip None values right away: audio_data.update({song_id: song_data for song_id, song_data in zip(current_batch, batch_features) if song_data is not None}). This keeps None values out of your dictionary from the start. With clean data, DataFrame creation works smoothly and you’ll get separate columns for each audio feature.