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

python to check the value of every row in a dataframe

I have a column of values in a dataframe as below. I want to create a new column that checks every row of the current column and and a value of 0 if it is larger than -5 or return -5 if the value is smaller than -5. Is there a quick way to do this? Thanks

input :

    value 
0   -2.26
1   -5.70
2   -2.14
3   -2.30
4   -2.22
5   -4.86
6   -5.07
7   -3.86
8   -3.26

output :

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

    value   new_value
0   -2.26           0
1   -5.70          -5
2   -2.14           0
3   -2.30           0
4   -2.22           0
5   -4.86           0
6   -5.07          -5
7   -3.86           0
8   -3.26           0

>Solution :

You can use np.where

df['new_values'] = np.where(df['values'] < -5, -5, 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