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

Range of int as keys for dict

I have a column which rejected amounts (these are just 5 exmaples)

enter image description here

I want to add a column, given points based on the price.

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

For example, if price is betwen 0 and 100 -> 0.

what i did was :

dict_reject_amount = {0:0,\
                      range(1,101):1,\
                      range(101,201):2,\
                      range(201,301):3,\
                      range(301,401):4,\
                      range(401,501):5,\
                      range(501,601):6,\
                      range(601,701):7,\
                      range(701,801):8,\
                      range(801,901):9,\
                      range(901,100000):9}

convert to int, floats do not matter

new['rejected_int'] = new['rejected'].astype(int)
new['reject_amount_points']= new['rejected_int'].map(dict_reject_amount)

enter image description here

Unfortunately it didn’t work.

>Solution :

You can use replace to apply the dict values

new['reject_amount_points'] = df['rejected_int'].replace(dict_reject_amount)
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