Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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'))

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading