Creating a new column based on if-elif-else condition Creating a new column based on if-elif-else condition python python

Creating a new column based on if-elif-else condition


To formalize some of the approaches laid out above:

Create a function that operates on the rows of your dataframe like so:

def f(row):    if row['A'] == row['B']:        val = 0    elif row['A'] > row['B']:        val = 1    else:        val = -1    return val

Then apply it to your dataframe passing in the axis=1 option:

In [1]: df['C'] = df.apply(f, axis=1)In [2]: dfOut[2]:   A  B  Ca  2  2  0b  3  1  1c  1  3 -1

Of course, this is not vectorized so performance may not be as good when scaled to a large number of records. Still, I think it is much more readable. Especially coming from a SAS background.

Edit

Here is the vectorized version

df['C'] = np.where(    df['A'] == df['B'], 0, np.where(    df['A'] >  df['B'], 1, -1)) 


df.loc[df['A'] == df['B'], 'C'] = 0df.loc[df['A'] > df['B'], 'C'] = 1df.loc[df['A'] < df['B'], 'C'] = -1

Easy to solve using indexing. The first line of code reads like so, if column A is equal to column B then create and set column C equal to 0.


For this particular relationship, you could use np.sign:

>>> df["C"] = np.sign(df.A - df.B)>>> df   A  B  Ca  2  2  0b  3  1  1c  1  3 -1