本 节 介 绍 对 S e r i e s 和 D a t a F r a m e 的 排 序 \color{red}本节介绍对Series和DataFrame的排序 本节介绍对Series和DataFrame的排序

一 、 使 用 . s o r t _ i n d e x ( ) 函 数 对 索 引 进 行 排 序 一、使用.sort\_index()函数对索引进行排序 一、使用.sort_index()函数对索引进行排序

对 于 S e r i e s 来 说 对于Series来说 对于Series来说

import numpy as np
import pandas as pd
a=pd.Series(np.arange(5),index=["a","c","h","t","g"])
a=a.sort_index(ascending=True)
print(a)

其 中 a s c e n d i n g 属 性 设 置 T r u e 表 示 升 序 , F a l s e 是 降 序 其中ascending属性设置True表示升序,False是降序 其中ascending属性设置True表示升序,False是降序

对 D a t a F r a m e 来 说 对DataFrame来说 对DataFrame来说

import numpy as np
import pandas as pd
a=pd.DataFrame(np.arange(10).reshape(2,5),index=["a","b"],columns=["你","是","伸","么","啦"])
print("排序前")
print(a)
a=a.sort_index(axis=1,ascending=False)
print("排序后")
print(a)


pandas的学习笔记(五、数据的排序)_属性设置

其 中 s o r t i n d e x 默 认 对 行 索 引 排 序 , 设 置 a x i s = 1 表 示 对 列 索 引 排 序 其中sort_index默认对行索引排序,设置axis=1表示对列索引排序 其中sortindex默认对行索引排序,设置axis=1表示对列索引排序

与之对应的还有sort_value方法 \colorbox{orange}{与之对应的还有sort\_value方法} 与之对应的还有sort_value方法

这 个 函 数 是 对 数 值 进 行 排 序 这个函数是对\color{red}{数值进行排序} 这个函数是对数值进行排序

. s o r t _ v a l u e ( b y , a x i s = 0 , a s c e n d i n g = T r u e ) .sort\_value(by,axis=0,ascending=True) .sort_value(by,axis=0,ascending=True)

其 中 a x i s 为 0 对 列 行 索 引 的 数 值 进 行 排 序 , 为 1 对 列 其中axis为0对列行索引的数值进行排序,为1对列 其中axis为0对列行索引的数值进行排序,为1对列

a s c e n d i n g = T r u e 仍 然 是 升 序 ascending=True仍然是升序 ascending=True仍然是升序

同 时 , 因 为 D a t a F r a m e 是 二 维 的 , 所 以 还 有 设 置 b y 属 性 , 设 置 3 代 表 对 第 4 行 ( 列 ) 排 序 同时,因为DataFrame是二维的,所以还有设置by属性,设置3代表对第4行(列)排序 同时,因为DataFrame是二维的,所以还有设置by属性,设置3代表对第4行(列)排序