I have a bunch of dataframes with the structure like below
df = pd.DataFrame(
[[1, 'A', 10], [2, 'A', 20], [3, 'A', 30],
[1, 'B', 20], [2, 'B', 20], [3, 'B', 10],
[1, 'M', 20], [2, 'M', 30], [3, 'M', 30]],
columns=['foo', 'bar', 'buzz']
)
the dataframe is initially sorted by columns bar and foo as one can get from
df.sort_values(['bar', 'foo'])
I need to get the df sorted by foo an bar instead. The obvious solution would be
df.sort_values(['foo', 'bar'])
which gives me
foo bar buzz
0 1 A 10
3 1 B 20
6 1 M 20
1 2 A 20
4 2 B 20
7 2 M 30
2 3 A 30
5 3 B 10
8 3 M 30
but the real-world dataframe contains about 500,000 rows and I have about 3,000 individual dataframes to be processed.
I was wondering if there is a better, more efficient solution which would take into account the fact that the dataframe is already pre-sorted?