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

simple mapping of pandas series to 0 and 1s given threshold

I am sorry for asking such a simple question (yes I googled). Do I really require 2 steps to map a simple pandas series of float between 0 and 1s to 0 and 1s given a threshold. This is the reproducible example:

series = pd.Series([0.0, 0.3, 0.6, 1.0])
threshold = 0.5
print(series)

series[series > threshold] = 1.0
series[series <= threshold] = 0.0

print(series)

It works producing:

0    0.0
1    0.0
2    1.0
3    1.0

from:

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

0    0.0
1    0.3
2    0.6
3    1.0

>Solution :

You could also apply a function on all elements using map() like

series = series.map(lambda x: 1.0 if x > threshold else 0.0)
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