【问题标题】:Python 3 replacing nan valuesPython 3 替换 nan 值
【发布时间】:2018-12-06 10:15:15
【问题描述】:

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

nancolumn = column1.value + column2.value

我的第一次尝试没有成功,即还有 nan 值

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() 和 isnan()

df[np.isnan(df)]=1

我在尝试 isnan() 时收到此错误:

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

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

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

有人可以帮忙吗?

【问题讨论】:

标签: python-3.x nan


【解决方案1】:

我发现最好先引用列,然后再引用索引,如下所示

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

虽然,我仍然不明白其中的区别。如果有人有解释,那将很有帮助。

【讨论】:

    猜你喜欢
    • 2019-04-22
    • 2019-11-08
    • 1970-01-01
    • 2019-09-29
    • 2018-09-24
    • 1970-01-01
    • 1970-01-01
    • 2019-05-05
    • 2023-03-04
    相关资源
    最近更新 更多