AttributeError:"float"对象没有属性"split" [英] AttributeError: 'float' object has no attribute 'split'

查看:1577
本文介绍了AttributeError:"float"对象没有属性"split"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在拨打此行:

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

这似乎在row["language_modifiers"]是一个单词(atlas methodcentral)的情况下起作用,但是当它出现为nan时却无效.

This seems to work where row["language_modifiers"] is a word (atlas method, central), but not when it comes up as nan.

我认为我的if not isinstance(row["language_modifiers"], float)可以赶上nan出现的时间,但事实并非如此.

I thought my if not isinstance(row["language_modifiers"], float) could catch the time when things come up as nan but not the case.

背景:row["language_modifiers"]是tsv文件中的一个单元格,当要解析的tsv中该单元格为空时,该单元格显示为nan.

Background: row["language_modifiers"] is a cell in a tsv file, and comes up as nan when that cell was empty in the tsv being parsed.

推荐答案

您是正确的,此类错误主要是由NaN表示空单元格引起的. 通常在应用进一步的操作之前,在数据框df上使用以下习惯用法过滤掉此类数据:

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()]

或者,使用fillna()方法将(c9)值插值(替换)为默认值可能更方便. 例如.所有nullNaN都可以替换为其列的平均值

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("")

这篇关于AttributeError:"float"对象没有属性"split"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆