1. 程式人生 > >pandas中Series和Dataframe的排序操作

pandas中Series和Dataframe的排序操作

對pandas中的Series和Dataframe進行排序,主要使用sort_values()和sort_index()。
DataFrame.sort_values(by, axis=0, ascending=True, inplace=False, kind=‘quicksort’, na_position=‘last’)
by:列名,按照某列排序
axis:按照index排序還是按照column排序
ascending:是否升序排列
kind:選擇 排序演算法{‘quicksort’, ‘mergesort’, ‘heapsort’}, 預設是‘quicksort’,也就是快排
na_position:nan排列的位置,是前還是後{‘first’, ‘last’}, 預設是‘last’
sort_index() 的引數和上面差不多。
例項:

import numpy as np
import pandas as pd
from pandas import Series, DataFrame

np.random.seed(666)

s1 = Series(np.random.randn(10))
print(s1)
'''
0    0.824188
1    0.479966
2    1.173468
3    0.909048
4   -0.571721
5   -0.109497
6    0.019028
7   -0.943761
8    0.640573
9   -0.786443
dtype: float64
'''

#  為series排序的兩種方式,1 用 value 來排序, 2 用 index 來排序
s2 = s1.sort_values() # 按照 value 來排序 print(s2) ''' 7 -0.943761 9 -0.786443 4 -0.571721 5 -0.109497 6 0.019028 1 0.479966 8 0.640573 0 0.824188 3 0.909048 2 1.173468 dtype: float64 ''' # axis 設定軸 的方向, ascending 設定升降序 s2 = s1.sort_values(axis = 0, ascending=False) print(s2) ''' 2 1.173468 3 0.909048 0 0.824188 8 0.640573 1 0.479966 6 0.019028 5 -0.109497 4 -0.571721 9 -0.786443 7 -0.943761 dtype: float64 '''
# 通過 對 index 進行排序 s2.sort_index() print(s2) ''' 2 1.173468 3 0.909048 0 0.824188 8 0.640573 1 0.479966 6 0.019028 5 -0.109497 4 -0.571721 9 -0.786443 7 -0.943761 dtype: float64 ''' # 對於 dataframe 的排序 df1 = DataFrame(np.random.randn(40).reshape(8, 5), columns=['a', 'b', 'c', 'd', 'e']) print(df1) ''' a b c d e 0 0.608870 -0.931012 0.978222 -0.736918 -0.298733 1 -0.460587 -1.088793 -0.575771 -1.682901 0.229185 2 -1.756625 0.844633 0.277220 0.852902 0.194600 3 1.310638 1.543844 -0.529048 -0.656472 -0.201506 4 -0.700616 0.687138 -0.026076 -0.829758 0.296554 5 -0.312680 -0.611301 -0.821752 0.897123 0.136079 6 -0.258655 1.110766 -0.188424 -0.041489 -0.984792 7 -1.352282 0.194324 0.267239 -0.426474 1.447735 ''' # 按照 columns 進行排序, 這種做法 和 對 series的 操作 差不多 print(df1['a'].sort_values()) ''' 2 -1.756625 7 -1.352282 4 -0.700616 1 -0.460587 5 -0.312680 6 -0.258655 0 0.608870 3 1.310638 Name: a, dtype: float64 ''' # 將 dataframe 按照 其中 某個列進行排序, 引數ascending來控制 升降序 print(df1.sort_values('a')) ''' a b c d e 2 -1.756625 0.844633 0.277220 0.852902 0.194600 7 -1.352282 0.194324 0.267239 -0.426474 1.447735 4 -0.700616 0.687138 -0.026076 -0.829758 0.296554 1 -0.460587 -1.088793 -0.575771 -1.682901 0.229185 5 -0.312680 -0.611301 -0.821752 0.897123 0.136079 6 -0.258655 1.110766 -0.188424 -0.041489 -0.984792 0 0.608870 -0.931012 0.978222 -0.736918 -0.298733 3 1.310638 1.543844 -0.529048 -0.656472 -0.201506 ''' df2 = df1.sort_values('a') print(df2) ''' a b c d e 2 -1.756625 0.844633 0.277220 0.852902 0.194600 7 -1.352282 0.194324 0.267239 -0.426474 1.447735 4 -0.700616 0.687138 -0.026076 -0.829758 0.296554 1 -0.460587 -1.088793 -0.575771 -1.682901 0.229185 5 -0.312680 -0.611301 -0.821752 0.897123 0.136079 6 -0.258655 1.110766 -0.188424 -0.041489 -0.984792 0 0.608870 -0.931012 0.978222 -0.736918 -0.298733 3 1.310638 1.543844 -0.529048 -0.656472 -0.201506 ''' # 對 df2 的 索引 進行排序, 又回到之前的原本的 df2 print(df2.sort_index()) ''' a b c d e 0 0.608870 -0.931012 0.978222 -0.736918 -0.298733 1 -0.460587 -1.088793 -0.575771 -1.682901 0.229185 2 -1.756625 0.844633 0.277220 0.852902 0.194600 3 1.310638 1.543844 -0.529048 -0.656472 -0.201506 4 -0.700616 0.687138 -0.026076 -0.829758 0.296554 5 -0.312680 -0.611301 -0.821752 0.897123 0.136079 6 -0.258655 1.110766 -0.188424 -0.041489 -0.984792 7 -1.352282 0.194324 0.267239 -0.426474 1.447735 '''