I have a pandas dataframe df
with 4 columns. For example here is a toy example:
foo1 foo2 foo3 foo4
egg cheese 2 1
apple pear 1 3
french spanish 10 1
The columns are foo1, foo2, foo3 and foo4
I would like to swap columns foo1 and foo2 and also swap columns foo3 and foo4 when foo3 < foo4. So the result would be:
foo1 foo2 foo3 foo4
cheese egg 1 2
apple pear 1 3
spanish french 1 10
I can find the rows that need swapping with df[df['foo3'] < df['foo4']]
but how can I do the swapping efficiently. My dataframe is large.