[英]Replacing column values in a pandas dataframe based if it contains a specific substring
I am new to python data science and started solving questions. 我是python数据科学的新手,并开始解决问题。 I got stuck in one problem where I am not able to replace some column values. 我陷入了一个无法替换某些列值的问题。
I am doing problem to predict old car price based on number of factors such as Power, seats, model, make, manufacturer and others. 我在根据功率,座椅,型号,制造商,制造商等因素的数量来预测旧车价格时遇到了问题。 For a power
column, fields are having values like as shown in snapshot 对于power
列,字段的值类似于快照中所示
Some fields are having values null bhp
. 一些字段的值为null bhp
。 I am trying to replace these null values to nan
so that I will be able to fill mean in those values in next step but I am unable to convert null to nan
我正在尝试将这些空值替换为nan
以便在下一步中可以填充这些值中的均值,但无法将null to nan
转换null to nan
Below is the code I am using 下面是我正在使用的代码
data["Power"]= data["Power"].str.split("bhp",expand = True)
#This is to change bhp
and then I am doing like this 然后我就这样
for i in data.Power:
if i=="null":
data.Power = np.nan
It is not doing anything. 它什么也没做。
Instead of splitting and iterating, just search for "null" and replace with loc
in one step. 无需拆分和迭代,只需搜索“ null”并用loc
替换即可。
data.loc[data['Power'].str.contains('null', na=False), 'Power'] = np.nan
You can use numpy.where
to do the same thing, possibly faster, 您可以使用numpy.where
来做同样的事情,可能更快,
data['Power'] = np.where(data['Power'].str.contains('null'), np.nan, data['Power'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.