Python Forum
multi np.where - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: multi np.where (/thread-32728.html)



multi np.where - dawid294 - Mar-01-2021

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


RE: multi np.where - eddywinch82 - Mar-03-2021

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


RE: multi np.where - dawid294 - Mar-04-2021

I found solution

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

Thanks