这篇文章主要介绍“pandas怎么按某列降序”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“pandas怎么按某列降序”文章能帮助大家解决问题。
升序
import pandas as pdimport numpy as npdata = np.random.randint(low=2,high=10,size=(5,3))data2 = np.random.randint(low=2,high=10,size=(5,3))df1 = pd.DataFrame(data,columns=["a","b","c"],index=range(5)) df2 = pd.DataFrame(data2,columns=["a","b","c"],index=range(5))df1 = df1.sort_values(by=['a','b'])print(df1)
运行结果:
降序
import pandas as pdimport numpy as npdata = np.random.randint(low=2,high=10,size=(5,3))data2 = np.random.randint(low=2,high=10,size=(5,3))df1 = pd.DataFrame(data,columns=["a","b","c"],index=range(5)) df2 = pd.DataFrame(data2,columns=["a","b","c"],index=range(5))df1 = df1.sort_values(by=['a','b'], ascending=[False, False])print(df1)
运行结果:
PS: 先按a降序,再按b降序
关于“pandas怎么按某列降序”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注编程网行业资讯频道,小编每天都会为大家更新不同的知识点。