Sorting pandas DataFrame with MultiIndex according to column value
I have a DataFrame with MultiIndex that looks like this after being printed to the console:
value indA indB scenarioId group 2015-04-13 1 A -54.0 1.0 1.0 B -160.0 1.0 1.0 C -15.0 0.0 1.0 2 A -83.0 1.0 1.0 3 A -80.0 2.0 2.0 4 A -270.0 2.0 2.0 2015-04-14 1 A -56.0 1.0 1.0 B -1.0 1.0 1.0 C -60.0 0.0 1.0 2 A -32.0 1.0 1.0 3 A -91.0 2.0 2.0 4 A -17.0 2.0 2.0
I got it after using the functions groupby
and sum
in my original dataset.
I would like to keep the same format but order it according to the column value
. I tried to do it using sorting functions, but I think the problem of having the first index (for dates) MultiIndex without a name is the problem.
Essentially, the output should look like this:
value indA indB scenarioId group 2015-04-13 1 B -160.0 1.0 1.0 A -54.0 1.0 1.0 C -15.0 0.0 1.0 2 A -83.0 1.0 1.0 3 A -80.0 2.0 2.0 4 A -270.0 2.0 2.0 2015-04-14 1 C -60.0 1.0 1.0 A -56.0 1.0 1.0 B -1.0 0.0 1.0 2 A -32.0 1.0 1.0 3 A -91.0 2.0 2.0 4 A -17.0 2.0 2.0
Can someone enlighten me on this?
Thanks in advance.
source to share
You can use sort_values
+ sort_index
:
print (df.sort_values('value').sort_index(level=[0,1], sort_remaining=False))
value indA indB
scenarioId group
2015-04-13 1 B -160.0 1.0 1.0
A -54.0 1.0 1.0
C -15.0 0.0 1.0
2 A -83.0 1.0 1.0
3 A -80.0 2.0 2.0
4 A -270.0 2.0 2.0
2015-04-14 1 C -60.0 0.0 1.0
A -56.0 1.0 1.0
B -1.0 1.0 1.0
2 A -32.0 1.0 1.0
3 A -91.0 2.0 2.0
4 A -17.0 2.0 2.0
Another solution is sort_values
with reset_index
and set_index
:
df = df.reset_index()
.sort_values(['level_0','scenarioId','value'])
.set_index(['level_0','scenarioId','group'])
print (df)
value indA indB
level_0 scenarioId group
2015-04-13 1 B -160.0 1.0 1.0
A -54.0 1.0 1.0
C -15.0 0.0 1.0
2 A -83.0 1.0 1.0
3 A -80.0 2.0 2.0
4 A -270.0 2.0 2.0
2015-04-14 1 C -60.0 0.0 1.0
A -56.0 1.0 1.0
B -1.0 1.0 1.0
2 A -32.0 1.0 1.0
3 A -91.0 2.0 2.0
4 A -17.0 2.0 2.0
source to share