Given approximate Dataframe
df = pd.DataFrame({
'Name': ['Max', 'Stefan', 'John', 'Kate', 'Walter', 'Karin', 'Julia', 'Ben', 'Spencer'],
'Balance': [0.12, 0.03, 0.12, 0.12, 0.12, 0.03, 0.03, 0.06, 0.03]})
Name Balance
0 Max 0.12
1 Stefan 0.03
2 John 0.12
3 Kate 0.12
4 Walter 0.12
5 Karin 0.03
6 Julia 0.03
7 Ben 0.06
8 Spencer 0.03
The condition of the sorting is to create mini-groups by 3 rows. These rows would be: first(max value of the column), second(the value which is less than max val and bigger than min val), third(min value of the column). But the nuance is that this value(second) can be equal to max value or equal to min value or just max > average > min. Also, it's important to note that all values can be equal. So there are four different outcomes are possible.
I need to sort this Dataframe in such a way:
Name Balance
0 Max 0.12
1 Ben 0.06
2 Julia 0.03
3 Kate 0.12
4 Walter 0.12
5 Karin 0.03
6 John 0.12
7 Stefan 0.03
8 Spencer 0.03