Giving Error on Trying out the same Code

I tried doing exactly what you tell in the video. While iterating over the titles, and trying to obtain their length, it gave me an error saying “object of type ‘float’ has no len()”

df = pd.read_csv(“movie_metadata.csv”)
titles = df.get(‘director_name’)
print(titles)

freq_title = {}

for t in titles:
length = len(t)

try:
    freq_title[length] += 1
except:
    freq_title[length] = 1

print(freq_title)

Hello Kartikeya, no doubt your code is alright. But if you see sir was using movie_title as a field and every row has that field but you are using director_name and at some places it is possible that director is not there and those values are mentioned as nan. So your code is fine you are getting error because whenever nan comes in the loop it reads its type as float and so float type does not have any function like len(). That’s why you are getting the error.
So pls add the condition whenever you get float data type you won’t take it.You can consider only those situations when you get string data type, like this if (type(t)==type("")): and after this write the remaining try except.
I hope it is clear to you, pls ask if anything is not clear to you or seems confusing, I will try to help you out.
In case it is clear to you pls mark it as resolve and pls provide the rating as well as feedback so that we can improve ourselves.
Thanks :slight_smile:
Happy Coding !!

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.