Python Forum

Full Version: multi np.where
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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
I found solution

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

Thanks