学无先后,达者为师

网站首页 编程语言 正文

pandas按某列降序的实现_python

作者:桂花很香,旭很美   更新时间: 2023-03-13 编程语言

升序

import pandas as pd
import numpy as np

data = 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 pd
import numpy as np

data = 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降序

原文链接:https://blog.csdn.net/weixin_40959890/article/details/128634507

栏目分类
最近更新