pandas dataframe排序

import pandas as pd
# 读取文件
filepath = '../data/2016_happiness.csv'
data = pd.read_csv(filepath, 
                   usecols=['Country', 'Region', 'Happiness Rank', 'Happiness Score'], 
                   index_col='Happiness Rank')
data.head()

# 默认 axis=0,表示按索引,行排序
data.sort_index(ascending=False).head(10)
# 按列名排序
data.sort_index(axis=1).head()

按值排序
# 单列排序
data.sort_values(by='Country').head(10)
# 按多列排序
data.sort_values(by=['Region', 'Country']).head(10)
# 按多列排序
data.sort_values(by=['Region', 'Country'], ascending=[True, False]).head(10)


你可能感兴趣的:(小象学院,df,排序,pandas)