【发布时间】:2016-12-28 08:26:46
【问题描述】:
如何在 pandas 中进行条件替换?
df = pd.DataFrame([[1, 2, 3], [4, None, None], [None, None, 9]])
在 R 中 - 认为这段代码很容易理解:
library(dplyr)
df = df %>%
mutate( # mutate means create new column for non-r people
my_new_column = ifelse( is.na(the_2nd_column)==TRUE & is.na(the_3rd_column)==TRUE, ' abc', 'cuz')
如何在 pandas 中执行此操作 - 可能是语法上的愚蠢问题,但我听说 np.where 相当于 R 中的 if else ...
df['new_column'] = np.where(np.nan(....help here with a conditional....))
【问题讨论】:
标签: python pandas numpy if-statement null