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

How do I recalculate the values of a column based on the current value in python?

I need to multiply the values of a column by 2.2 but only if the values are less than 30. Any help would be much appreciated

Here is what I tried. I know it is super wrong but I have no idea where to go with this. I am new to python, so forgive the really dumb question.

Values less than 30 are in kg and need to be recalculated into pounds.

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

df['Weight']=np.where(df['Weight'] > 30, df['Weight']*2.2)

>Solution :

Given this dataframe for example:

df = pd.DataFrame({"weight": [10, 20, 30, 40, 50]})
   weight
0      10
1      20
2      30
3      40
4      50

You can use .loc like this:

df.loc[df["weight"] < 30, "weight"] = df["weight"] * 2.2
   weight
0      22
1      44
2      30
3      40
4      50

You can read more about selecting data here.


You can also use DataFrame.where (not numpy.where):

df["weight"] = df.where(df["weight"] >= 30, df["weight"] * 2.2, axis=0)
   weight
0      22
1      44
2      30
3      40
4      50

Or DataFrame.mask:

df["weight"] = df.mask(df["weight"] < 30, df["weight"] * 2.2, axis=0)
   weight
0      22
1      44
2      30
3      40
4      50
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