Python 3 replacing nan values



我正试图使用其他列的计算来替换一些列中的nan值。即

nancolumn = column1.value + column2.value

我的第一次尝试没有成功

indecies = list(list(map(tuple, np.where(np.isnan(df['nancolumn']))))[0])
newValue = df.iloc[indecies]['column1'] + df.iloc[indecies ]['column2']
df.iloc[indecies]['nancolumn'] = newValue

然后我找到了一个我想替换的特定索引,1805,并尝试用1.0替换这个数据点值。结果仍然是一个nan

df.iloc[1805]['nancolumn'] = 1.0

我试过使用fillna((,它是((

df[np.isnan(df)]=1

我在isnan((尝试中得到了这个错误:

TypeError:输入类型不支持ufunc"isnan",并且根据强制转换规则"安全",无法将输入安全地强制为任何支持的类型

df.iloc[1805]['nancolumn'].dtype
dtype('float64')

我知道我错过了一些简单的东西,但我想不通。

有人能帮忙吗?

我发现最好先引用列,然后引用索引,就像下面的一样

df['nancolumn'].iloc[1805] = 1.0

尽管如此,我还是不太明白其中的区别。如果有人咒骂,那会很有帮助。

最新更新