I'm trying to create a flag variable (i.e. a new column with binary values, like 1 for True, 0 for False) - I've tried both np.where
(as per this post) and df.where
to no avail.
With df.where using:
df.where(((df['MOSL_Rating'] == 'Highly Effective') & (df['MOTP_Rating'] == 'Developing')) | ((df['MOSL_Rating'] == 'Highly Effective') & (df['MOTP_Rating'] == 'Ineffective')) | ((df['MOSL_Rating'] == 'Effective') & (df['MOTP_Rating'] == 'Ineffective')) | ((df['MOSL_Rating'] == 'Ineffective') & (df['MOTP_Rating'] == 'Highly Effective')) | ((df['MOSL_Rating'] == 'Ineffective') & (df['MOTP_Rating'] == 'Effective')) | ((df['MOSL_Rating'] == 'Developing') & (df['MOTP_Rating'] == 'Highly Effective')), df['disp_rating'], 1, axis=1)
but this returns ValueError: For argument "inplace" expected type bool, received type int.
If I change my code from df['disp_rating'], 1, axis=1
to df['disp_rating'], True, axis=1
it returns TypeError: Cannot do inplace boolean setting on mixed-types with a non np.nan value
I've also tried np.where
but that returns ValueError: either both or neither of x and y should be given
I've also read this question, which looks similar. However when I use the solution presented there it returns:
KeyError: 'disp_rating'
If I create the variable in advance (to avoid the Key Error
) I just get another error about something else.
I assumed it would be pretty simple to create a new variable based on some basic conditions, but I've been stuck on this for a while and am not really making any progress despite reading the documentation and lots of SO posts.
edit: Just to be extra clear, I'm trying to create a new column (named 'disp_rating') based on whether or not the values in 2 other columns ('MOSL_Rating and 'MOTP_Rating') within the same df meet certain conditions. I only have 1 dataframe, so I'm not trying to compare 2 dataframes. In SQL I would use a CASE WHEN statement, in SAS I would use an IF/THEN/ELSE statement.
My df generally looks like this:
ID Loc MOSL_rating MOTP_Rating
12 54X D E
45 86I D I
98 65R H H