Home > Mobile >  How to populate a dataframe column based on condition met in another column
How to populate a dataframe column based on condition met in another column

Time:12-31

I was wondering how for the following dataframe can make a new column (for example B), and for each row define if its value is A1: x>2, A2: between(2,0), A3: between(0,-2), or A4: x<-2.

imprt pandas as pd
imort numpy as np
df = pd.DataFrame({'A':[-4,-3.5,-2.5,-1,1,1.5,2,2.5,3.5]})

I tried the following code but it didnt work.

df['B'] = np.where((df['A']>2), 'A1',
               np.where(df['A'].between(2,0),'A2',
                         np.where(df['A'].between(0,-2),'A3', 
                                   np.where(df['A']<-2), 'A4'))

CodePudding user response:

Your solution is possible if change ():

df['B'] = np.where(df['A']>2,'A1',
          np.where(df['A'].between(0,2),'A2',
          np.where(df['A'].between(-2,0),'A3', 
          np.where(df['A']<-2, 'A4',''))))

Alternative with cut:

df['B1'] = pd.cut(df['A'], bins=(-np.inf,-2,0,2,np.inf), labels=('A4','A3','A2','A1'))
print (df)
     A   B  B1
0 -4.0  A4  A4
1 -3.5  A4  A4
2 -2.5  A4  A4
3 -1.0  A3  A3
4  1.0  A2  A2
5  1.5  A2  A2
6  2.0  A2  A2
7  2.5  A1  A1
8  3.5  A1  A1
  • Related