【发布时间】:2018-11-04 05:28:24
【问题描述】:
我是一名尝试进入 Python 的 R 程序员。在 R 中,当我想有条件地改变一列时,我使用:
col = dplyr::mutate(col, ifelse(condition, if_true(x), if_false(x))
在 Python 中,如何有条件地改变列值?这是我的最小可重现示例:
def act(cntnt):
def do_thing(cntnt):
return(cntnt + "has it")
def do_other_thing(cntnt):
return(cntnt + "nope")
has_abc = cntnt.str.contains.contains("abc")
if has_abc == T:
cntnt[has_abc].apply(do_thing)
else:
cntnt[has_abc].apply(do_other_thing)
【问题讨论】:
-
请您添加一个关于您的问题和预期结果的小例子吗?另外,我假设
if has_abc == T:实际上是if has_abc == True:
标签: python pandas if-statement conditional