对多索引数据框进行排序
我正在尝试满足一个要求,我想对多索引数据框中的值进行排序,这是我的下面的代码-
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)
数据框看起来像这样-
我正在尝试根据 Quant 对值进行排序,所以每列即2014-2017,存在于Quant中,其值需要按升序排列。
我正在尝试下面的代码,但出现错误,我该如何纠正这个错误-
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论