sorting in pandas, while alternating between ascending and descending on the same sorting column

I would like to sort a dataframe by a two columns, but one is always ascending, and the other switches between ascending and descending based on the value of the first column. In other words, when the first column increases, the sorting flips from ascending to descending or vise versa.

My motivation for this is that I am trying to sort a set of data that is indexed spatially in a gird into chronological order. The data is measured by snaking upwards, and then back and forth in a grid. I would like to sort ascending by y value, and then go back and forth in the x value whenever the y value increments. I don’t know how to so this with df.sort_values() or df.groupby() as those alter the whole dataframe.

I am trying to sort this

X position | Y position | Data
     0            0        '1st'
     0            1        '4th'
     1            0        '2nd'
     1            1        '3rd'

Into this

X position | Y position | Data
     0            0        '1st'
     1            0        '2nd'
     1            1        '3rd'
     0            1        '4th'

Answer

Not sure if you have found a solution in the meantime…

You could do the following:

df = pd.concat(
    [
        sdf.sort_values("X", ascending=i%2)
        for i, (_, sdf) in enumerate(df.sort_values("Y").groupby("Y"), start=1)
    ]
)

or

def sort(sdf):
    global i
    i += 1
    return sdf.sort_values("X", ascending=i%2)

i = 0
df = df.sort_values("Y").groupby("Y", group_keys=False).apply(sort)

Result for

df =
   X  Y
0  0  0
1  0  1
2  0  2
3  1  0
4  1  1
5  1  2

is

   X  Y
0  0  0
3  1  0
4  1  1
1  0  1
2  0  2
5  1  2