Home > OS >  How to apply function to multiple columns based on value of another column?
How to apply function to multiple columns based on value of another column?

Time:03-17

Let's say I have a dataframe as follow:

unit altitude_low altitude_high
meter 456 25435
meter 254 35223
feet 34 3256
feet 46 234
meter 456 45776

How do I convert the value of both altitude columns to meter if the unit column is feet? I tried:

def convert_to_m(row):  
    if row['unit'] == "feet":
        row['altitude_low ']= row['altitude_low ']/3.281
        row['altitude_high']= row['altitude_high']/3.281 
    else:
        pass 

df= df.apply(lambda row: convert_to_m(row), axis= "columns")

but this failed.

CodePudding user response:

You can use boolean indexing:

df.loc[df['unit'].eq('feet'), ['altitude_low','altitude_high']] /= 3.281

Output:

    unit  altitude_low  altitude_high
0  meter    456.000000   25435.000000
1  meter    254.000000   35223.000000
2   feet     10.362694     992.380372
3   feet     14.020116      71.319720
4  meter    456.000000   45776.000000

You can take the opportunity to change the unit as well:

mask = df['unit'].eq('feet')
df.loc[mask, ['altitude_low','altitude_high']] /= 3.281
df.loc[mask, 'unit'] = 'meter'

Output:

    unit  altitude_low  altitude_high
0  meter    456.000000   25435.000000
1  meter    254.000000   35223.000000
2  meter     10.362694     992.380372
3  meter     14.020116      71.319720
4  meter    456.000000   45776.000000

alternative approach for multiple units:

Use a dictionary with the conversion factors

units = {'feet': 1/3.281, 'meter': 1}

cols = ['altitude_low','altitude_high']
df[cols] = df[cols].mul(df['unit'].map(units), axis=0)
  • Related