jeudi 16 avril 2020

Python "not in" across multiple conditions [duplicate]

I am importing both pandas and numpy in Jupyter notebooks

I have 2 columns x and y in dataframe (df) which are fed in from a larger CSV but these are the 2 columns that are relevant to the calculation, x is an int, y is a float.

In SAS I would type:

case
when t1.x <> 1 or t1.y not in (1,2)
then z
else t1.y
end

I can't get that to work in Python whereby the column y is overwritten when calculating the formula I've been using conditions and choices for the simpler queries but the following doesn't work:

conditions = [
    (df['x'] != 1)] | (df['y'] not in (1,2))]

choices = [z]

df['y'] = np.select(conditions, choices, default=(df['y']))

Any suggestions?

Aucun commentaire:

Enregistrer un commentaire