AttributeError: 'float' object has no attribute 'split'

后端 未结 2 440
醉话见心
醉话见心 2021-02-05 16:26

I am calling this line:

lang_modifiers = [keyw.strip() for keyw in row[\"language_modifiers\"].split(\"|\") if not isinstance(row[\"language_modifiers\"], float)         


        
2条回答
  •  猫巷女王i
    2021-02-05 17:25

    You are right, such errors mostly caused by NaN representing empty cells. It is common to filter out such data, before applying your further operations, using this idiom on your dataframe df:

    df_new = df[df['ColumnName'].notnull()]
    

    Alternatively, it may be more handy to use fillna() method to impute (to replace) null values with something default. E.g. all null or NaN's can be replaced with the average value for its column

    housing['LotArea'] = housing['LotArea'].fillna(housing.mean()['LotArea'])
    

    or can be replaced with a value like empty string "" or another default value

    housing['GarageCond']=housing['GarageCond'].fillna("")
    

提交回复
热议问题