Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
multi np.where
#1
Hello guys. I need help. Can you help me with code. I have not found it anywhere. I use np.where but I would like to use np.where as if in excel. I need create clusters with amount. I have one column 'PP' with values. and I would like to created column with clusters.


I used this code but it does not work.

Quote:MODEL1b['clusters']=np.where(MODEL1b['PP_PRED']<10,'0-10',
np.where(MODEL1b['PP']<20 & MODEL1b['PP']>=10, '10-20',
np.where(MODEL1b['PP']<30 & MODEL1b['PP']>=20, '20-30',
np.where(MODEL1b['PP']<50 & MODEL1b['PP']>=30, '30-50',
np.where(MODEL1b['PP']<70 & MODEL1b['PP']>=50, '50-70',
np.where(MODEL1b['PP']<100 & MODEL1b['PP']>=70, '70-100',
np.where(MODEL1b['PP']<150 & MODEL1b['PP']>=10, '100-150',
np.where(MODEL1b['PP']>=150, '150-', 'empty'))))))))


for example i have in column PP values: 10,15,20,50,80,90,200,30,5
so next column will be like '10-20','10-20','20-30','50-70','70-100','70-100','150-','30-50','0-10'


Thank you so much
Reply
#2
Hi dawid294,

Could you post your full Python Code, and upload the File you are using ?

So I can get you, the answer you are looking for ?

Best Regards

Eddie Winch
Reply
#3
I found solution

MODEL1b.loc[(MODEL1b['PP']<20) & (MODEL1b['PP']>=10), 'cl'] = '10--20'

Thanks
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020