Python 按从最大到最小的顺序分组

Python 按从最大到最小的顺序分组,python,pandas,csv,data-analysis,Python,Pandas,Csv,Data Analysis,尝试对一家公司的销售额从最大到最小进行排序,同时根据列表中的销售额指数重新排列公司名称 我想不出该怎么办,因为我被卡住了 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)

尝试对一家公司的销售额从最大到最小进行排序,同时根据列表中的销售额指数重新排列公司名称

我想不出该怎么办,因为我被卡住了

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

这就解决了你的问题

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)

这就解决了你的问题

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)

可以使用sort_values()方法


可以使用sort_values()方法


sort_values()获得了一个意外的关键字参数“by”,请在此处查看该方法的文档。也许你使用的是早期版本的熊猫?sort_values()获得了一个意外的关键字参数“by”,请在此处查看该方法的文档。也许你使用的是早期版本的熊猫?