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

标签 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)

而不是这个。

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

最佳答案

您可以使用 sort_values() 方法

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

关于python - 使用 pandas group by 从最大到最小的顺序排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55941941/

相关文章:

sql - 如何使用 Postgres 中的 CSV 文件中的值更新选定的行?

mysql - 使用 MySQL 在 CSV 中搜索

python - 需要 pexpect 模块的帮助

python - 使用 dataframe.loc 将信息从一个数据帧调用到另一个数据帧的更有效方法

python - 如何从索引开始查找第一次出现的 bool 值

python - 迭代 pandas 数据框中的行并匹配列表字典中的值以创建新列

python - 有没有办法找到Python中存在*.csv?

python - 我的笔记的 Sphinx 源位置 - 警告 : document isn't included in any toctree

python - 我可以使用复合主键代替 ID 主键吗?

python - to_latex : Incorrect Encoding for Strings (with Backslashes)