查找并用数字替换"nan" [英] finding and replacing 'nan' with a number

查看:66
本文介绍了查找并用数字替换"nan"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想替换数字3而不是数组中的所有"nan".这是我的代码:

I want to replace number 3 instead of all 'nan' in array. this is my code:

train= train.replace("nan",int(3))

但是我的数组没有任何变化.你能指导我吗?

But nothing changes in my array. Could u please guide me?

推荐答案

>>> import math
>>> train = [10, float('NaN'), 20, float('NaN'), 30]
>>> train = [3 if math.isnan(x) else x for x in train]
>>> train
[10, 3, 20, 3, 30]

这篇关于查找并用数字替换"nan"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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