Search code examples
pythonpandasdata-wrangling

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.

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