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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With