I am ne w to Python so forgive me if this is a basic question. I am in the process of converting my R code into Python. I try to create new variable using data from other two variables. In R I would do the following:
df$new_col <- ifelse(is.na(df$col1) & is.na(df$col2), 99,
ifelse(is.na(df$col1) & !is.na(df$col2), df$col2,
ifelse(is.na(df$col2) & !is.na(df$col1), df$col1,
ifelse(df$col2 > df$col1, df$col1,
ifelse(df$col2 < df$col1, df$col2,
ifelse(df$col1==df$col2, df$col2, "Error in coding"))))))
I have seen below bit of code in answers to the similar questions which works when I have only one condition but I can't get it work when it is more complex.
df['new_col'] = np.where(df['col1'].isnull() and df['col2'].isnull(), x, y)
Any help would be greatly appreciated.
Aucun commentaire:
Enregistrer un commentaire