Reputation: 17
I'm trying to execute this code
import pandas as pd
df_schema = pd.read_csv('survey_results_schema.csv')
df_results = pd.read_csv('survey_results_public.csv', index_col='Respondent')
print(df_results['Country'].apply(len))
And I should get this:
Respondent
1 14
2 22
3 8
...
But I keep getting this error and I don't know how to fix it:
Exception has occurred: TypeError
object of type 'float' has no len()
If we execute this lineprint(df_results['Country'])
, we'll get this
Respondent
1 United Kingdom
2 Bosnia and Herzegovina
3 Thailand
4 United States
5 Ukraine
...
88377 Canada
88601 NaN
88802 NaN
88816 NaN
88863 Spain
What's wrong with my code?
Upvotes: 1
Views: 1619
Reputation: 508
Your problem is probably that you have NaN in your data. NaN type is recognized as a float so this is the reason for your error.
The following code will set 0 for every NaN value else will set the len of the value:
print(df_results['Country'].apply(lambda x: 0 if pd.isna(x) else len(x)))
A better approach will be to clear your null values before working with the Dataframe. You can use the dropna function for that: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.dropna.html#pandas.DataFrame.dropna
Upvotes: 1