使用pandas group by按从最大到最小的顺序排序

问题描述 投票:1回答:2

尝试从公司的销售额的最大到最小排序,该公司还根据列表中的销售指数重新排列公司的名称。

因为我被卡住,我无法弄清楚要尝试什么。

df=df.groupby(df['Distributor'])['Tickets Sold'].sum() 
df1=df[df.div(df.sum()).lt(0.01)] df2=df.drop(df1.index) 
yourdf=pd.concat([df2,pd.Series(df1.sum(),index=['Others'])])
print(yourdf)

而不是这个。

20th Century Fox 141367982   
Focus Features 18799261 
Lionsgate 75834308 
Paramount Pictures 86302817 
STX Entertainment 22606674 
Sony Pictures 102746480 
Universal 159556790 
Walt Disney 315655340 
Warner Bros. 216426845 
Others 74618013 

我需要这个。

Walt Disney 315655340 
Warner Bros. 216426845 
Universal 159556790 
20th Century Fox 141367982 
Sony Pictures 102746480 
Paramount Pictures 86302817 
Lionsgate 75834308
Others 74618013 
STX Entertainment 22606674 
Focus Features 18799261
python pandas csv data-analysis
2个回答
0
投票

您可以使用sort_values()方法

   df.sort_values(by=['ColName'], ascending=False)

0
投票

这解决了你的问题!

df=df.groupby(df['Distributor'])['Tickets Sold'].sum()
df1=df[df.div(df.sum()).lt(0.01)]
df2=df.drop(df1.index)
yourdf=pd.concat([df2,pd.Series(df1.sum(),index=['Others'])])

yourdf = yourdf.sort_values(ascending=False)
print(yourdf)
© www.soinside.com 2019 - 2024. All rights reserved.