python 将数组中取某一值的元素全部替换为其他元素的方法

这里的问题是在做House Price Prediction的时候遇到的,尝试对GarageArea做log转化,但是由于有些房子没有车库,所以GarageArea = 0,再通过log转化变成-inf了,所以我想把所有-inf的数据全部再转化为0,如题目所示。

除了循环还可以怎么做呢?看下面的代码

train_test['GarageArea'] = np.log(train_test['GarageArea'])

train_test['GarageArea'] = train_test['GarageArea'].replace(float("-inf"), 0)

本来以为replace()函数是str专用的函数,结果尝试发现在数组上也可以用,这里比较特殊的是-inf的写法,不能直接写成 -inf 或者 '-inf',而要写成 float("-inf")

原文地址:https://www.cnblogs.com/RB26DETT/p/11610345.html