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

48 Views Asked by At

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.

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

There are 2 best solutions below

0
e-motta On BEST ANSWER

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
0
douglas On

You were on the right track with using np.where(), but you need to adjust the condition and what happens when the condition is not met.

You can see the np.where() as an if else. If the first condition is met (i.e. df['Weight'] > 30) you update the value with the second argument (in your case multiply by 2.2). But what if the condition isn't met ? That's why you need to provide a 3rd argument to the method which tells what do do if the 1st condition isn't met.

The corrected code is:

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