KBar
KBar

Reputation: 33

Create new column based on multiple conditions in the same data frame

I'm very new to python and still learning. I really appreciate any help you can provide. I have five columns in my data frame. I'd like to compare S1 to S2, then, based on several conditions, add a column with string values that represent how each sample ID has changed.

Id S1   Cat_1  S2   Cat2
25  3   Acq     4   Acq
15  9   Chp     9   Chp
22  4   Acq     5   Frn
34  4   Acq     1   Acq
45  3   Acq     9   Chp

I did this so far, but can't figure out how to add more criteria.

df['new column'] = np.where(df['S2']>df['S1'],'More','no')

What I would like is:
if ['S2']>['S1'] = 'More' 
   OR
if ['S2']<['S1'] = 'Less' 
   OR
if ['S2']=['S1'] = 'Same' 
  OR
if none of the above conditions are true, or if S1 is nan, new column value = 'New'

The result should look like this:

Id  S1  Cat_1  S2   Cat2  New_column
25  3   Acq     4   Acq    More
15  9   Chp     9   Chp    Same  
22  4   Acq     5   Frn    More  
34  4   Acq     1   Acq    Less
45  3   Acq     9   Chp    More
01              9   Chp    New 

Thanks

Upvotes: 1

Views: 57

Answers (1)

Celius Stingher
Celius Stingher

Reputation: 18377

You can either concatenate conditions within the np.where() in the false argument, or create a custom function with your criteria and pass it using apply with lambda I will showcase both option:

Using multipe np.where():

df['new column'] = np.where(df['S1'].isna(),'New',
   np.where(df['S2']>df['S1'],'More',
      np.where(df['S2'] < ['S1'],'Less','Same')))

Creating a custom function:

def my_function(x):
   if x['S1'].isna():
      return 'New'
   elif x['S2'] > x['S1']:
      return "More"
   elif x['S2'] < x['S1']:
      return "Less"
   else:
      return "Same"

df['New column'] = df.apply(lambda x: my_function(x),axis=1)

Upvotes: 1

Related Questions