使用if语句在python中替换字符串 [英] String replace in python using if statement
本文介绍了使用if语句在python中替换字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试根据列值替换字符串,但出现错误 ValueError:系列的真值不明确.使用a.empty,a.bool(),a.item(),a.any()或a.all().
Trying to replace a string based on column value, getting the error ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
if df['Target'] == 'U':
df['Target'] = df['Action']
获取错误 ValueError:系列的真值不明确.使用a.empty,a.bool(),a.item(),a.any()或a.all().
Getting the Error ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
我需要检查字符串,如果匹配则用另一个列值替换
Just I need to check the string and replace it with another column value if matches
推荐答案
使用np.where
例如:
import numpy as np
df['Target'] = np.where(df['Target'] == "U", df['Action'], df['Target'])
这篇关于使用if语句在python中替换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文