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

Multiply only positive values within a column by 100

Any values greater than 0 should be multiplied by 100.

Example dataset:

df = pd.DataFrame({'col1': ['A','B','C'], 'col2':[-0.42,0.091,0.0023], 'col3': [30, 10,20]})

   col1     col2   col3
0     A    -0.42     30
1     B    0.091     10
2     C   0.0023     20

Expected outcome for col2 would look like this:

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

   col1     col2   col3
0     A    -0.42     30
1     B      9.1     10
2     C     0.23     20

Not sure how to tackle this one if anyone can provide coding or guidance using python/pandas

>Solution :

Use loc indexing:

df.loc[df['col2'] > 0, 'col2'] *= 100
print(df)

# Output:
  col1  col2  col3
0    A -0.42    30
1    B  9.10    10
2    C  0.23    20
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