Home > Software engineering >  Updating Value in Column Based on Values in Another Column in Pandas
Updating Value in Column Based on Values in Another Column in Pandas

Time:01-06

I have a dataframe that has two columns: Rate and English Pay Rate.

English Pay Rate in the original df has no values in it. Rate has values of float64 datatype

example:

enter image description here

I want to update the column df['English Rate'] pull the value from df['Rate'] IF the value is =>10.95. If the value in df['Rate'] is < 10.95, then I want df['English Rate'] to be 10.95

Desired Output:

enter image description here

I feel there is a really simple way of doing this, and I am over thinking it.

I tried to do a for loop and if statement but it didn't work. I am not sure how to use the .loc function to achieve this either.

for loop I tried, but it didn't work:

for i in df['Rate']:
    if i >= 10.95:
        df['English Rate'] = df['Rate']
    else:
        df['English Rate'] = 10.95

CodePudding user response:

Try numpy.where

import pandas as pd
import numpy as np

df = pd.DataFrame([15.15, 12.65, 10.84, 9.95], columns=['Rate']) # sample df
df['English Rate'] = np.where(df['Rate']>=10.95, df['Rate'], 10.95)

    Rate  English Rate
0  15.15         15.15
1  12.65         12.65
2  10.84         10.95
3   9.95         10.95
  •  Tags:  
  • Related