You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The error you're encountering, 'float' object is not subscriptable, usually occurs when you're trying to access an index or slice of a non-string object. In your case, it seems like some of the 'Release Date' values in your data DataFrame might be NaN or not in a string format.
You can modify your code to handle this by checking if the 'Release Date' is a string before attempting to slice it:
from collections import defaultdict
import pandas as pd
import matplotlib.pyplot as plt
years = defaultdict(int)
for i, song in data.iterrows():
release_date = song['Release Date']
if isinstance(release_date, str):
years[release_date[:4]] += 1
years = pd.DataFrame(years.items(), columns=['Year', 'Num Songs']).sort_values('Year')
TypeError Traceback (most recent call last)
Cell In[16], line 3
1 years = defaultdict(int)
2 for i,song in data.iterrows():
----> 3 years[song['Release Date'][:4]] += 1
5 years = pandas.DataFrame(years.items(), columns=['Year', 'Num Songs']
6 ).sort_values('Year')
8 pyplot.figure(figsize=(10, 6))
TypeError: 'float' object is not subscriptable
The text was updated successfully, but these errors were encountered: