Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I swap values in two columns by applying a function?

I have a data set that has various columns. I want to swap values where the minimum temperature(tmin) is bigger than the maximum temperature(tmax) by applying a function.

The function I want to apply:

def swap(a,b):
    if a >= b:
        return b,a
    else:
        return a,b

Applying it:

cam.apply(lambda row: swap(row['tmin'], row['tmax']), axis=1)

when i check if the code worked, I find that it didn't change anything cam.query('tmin>tmax')

station       date  year  month  day  rain  tmin  tmax

126  garoua  1954-05-07  1954      5  127  NaN   35.6  33.8

2012 garoua  1959-07-06  1959      7  187  NaN   33.0  31.6
like image 734
jb12n Avatar asked Feb 14 '26 22:02

jb12n


1 Answers

Here's one way indexing the dataframe on the rows where tmin is greater than tmax and using DataFrame.reindex to swap the values in both columns:

# columns to be used for indexing
cols = ["tmin","tmax"]
#indices where tmin is greater than tmax
ixs = df.tmin.gt(df.tmax)
# Where ixs is True, values are swapped
df.loc[ixs,cols] = df.loc[ixs, cols].reindex(columns=cols[::-1]).values

      station    date     year  month  day  rain  tmin  tmax
126   garoua  1954-05-07  1954      5  127   NaN  33.8  35.6
2012  garoua  1959-07-06  1959      7  187   NaN  31.6  33.0

Or using DataFrame.where:

df[cols] = df[cols].where(df.tmin.lt(df.tmax), df[cols[::-1]].values)

      station    date     year  month  day  rain  tmin  tmax
126   garoua  1954-05-07  1954      5  127   NaN  33.8  35.6
2012  garoua  1959-07-06  1959      7  187   NaN  31.6  33.0
like image 155
yatu Avatar answered Feb 21 '26 16:02

yatu