Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in Cell 16 (Release Dates) #41

Open
x1-foundation opened this issue Mar 17, 2024 · 1 comment
Open

Error in Cell 16 (Release Dates) #41

x1-foundation opened this issue Mar 17, 2024 · 1 comment

Comments

@x1-foundation
Copy link


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

@x1-foundation
Copy link
Author

x1-foundation commented Mar 17, 2024

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')

plt.figure(figsize=(10, 6))
plt.bar(years['Year'], years['Num Songs'])
plt.xticks(rotation=80)
plt.xlabel(years.columns[0])
plt.ylabel(years.columns[1])
plt.title('Songs per Year')
plt.show()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant