'Sorting a multindex data frame
I am trying to work on a requirement where I want to sort values in a multiindex dataframe, here is my below code-
import pandas as pd
import numpy as np
s={'Fruits':['Apple','Orange', 'Banana', 'Mango'],'month':[2014,2015,2016,2017],'Weight':[2,4,1,7],'Quant':[5,2,3,9]}
p=pd.DataFrame(data=s)
df=pd.pivot_table(p,index='Fruits',columns='month',values=['Weight','Quant'],aggfunc=np.sum)
df=df.fillna(0)
The dataframe looks like this-
I am trying to sort values based on Quant, so every column ie 2014-2017,present in Quant, its values needs to be arranged in an ascending order.
I am trying the below code, but getting an error, how would I rectify this-
df.sort_values(by='Quant')
ValueError: The column label 'Quant' is not unique.
For a multi-index, the label must be a tuple with elements corresponding to each level.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

