文章目录
3.5排序
- list.sort() 操作于原值
- ndarray.sort() 操作于原值
- np.sort(ndarray) 原值不变 ,返回新值
pandas 排序
-
obj.sort_index(ascending = True) 默认升序
-
obj.sort_values(by=‘label’,axis=0,ascending=True,inplace=False)
-
by:str :表示根据 axis方向的索引名进行排序
-
axis:0/1 :表示轴
-
ascending:bool :默认True升序,Flase 是降序
-
inplace:false ;默认不在原值上排序,返回新值
-
准备一个Series
s1=Series(np.random.choice(range(1,7),6,replace=False)*10,index=list('adefcb'))
s1
a 30
d 50
e 10
f 20
c 40
b 60
dtype: int32
- 排 索引
s1.sort_index()
a 30
b 60
c 40
d 50
e 10
f 20
dtype: int32
s1.sort_values()
e 10
f 20
a 30
c 40
d 50
b 60
dtype: int32
s1.sort_values(ascending=False)
b 60
d 50
c 40
a 30
f 20
e 10
dtype: int32
- 对 DataFrame 排序
df3 = DataFrame(
np.random.randint(1