Pandas数据排序——【按索引排序sort_index()方法、按值排序sort_value()方法】
生活随笔
收集整理的這篇文章主要介紹了
Pandas数据排序——【按索引排序sort_index()方法、按值排序sort_value()方法】
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 按索引排序——sort_index()
- 對(duì)Series排序
- 對(duì)DataFrame排序
- 按值排序——sort_value()
- 對(duì)Series進(jìn)行排序
- 對(duì)DataFrame進(jìn)行排序
按索引排序——sort_index()
sort_index(axis=0, level=None, ascending=True, inplace=False, kind=‘quicksort’, na_position=‘last’,sort_remaining=True)
上述方法中常用參數(shù):
axis:軸索引(排序的方向),0表示按index,1表示按columns
level:若不為None,則對(duì)指定索引級(jí)別的值進(jìn)行排序
ascending:是否升序排列,默認(rèn)為True,表示升序
inplace:默認(rèn)為False,表示對(duì)數(shù)據(jù)表進(jìn)行排序,不創(chuàng)建新的實(shí)例
kind:選擇排序算法
這里只舉例kind參數(shù)的作用,其他參數(shù)在下文的應(yīng)用中會(huì)被用到 arrays = [np.array(['qux', 'qux', 'foo', 'foo','baz', 'baz', 'bar', 'bar']),np.array(['two', 'one', 'two', 'one','two', 'one', 'two', 'one'])] s = pd.Series([1, 2, 3, 4, 5, 6, 7, 8], index=arrays) print("s:\n", s) print("s.sort_index(level=0):\n", s.sort_index(level=0)) print("s.sort_index(level=1:\n", s.sort_index(level=1))輸出結(jié)果:
s: qux two 1one 2 foo two 3one 4 baz two 5one 6 bar two 7one 8 dtype: int64s.sort_index(level=0): bar one 8two 7 baz one 6two 5 foo one 4two 3 qux one 2two 1 dtype: int64s.sort_index(level=1: bar one 8 baz one 6 foo one 4 qux one 2 bar two 7 baz two 5 foo two 3 qux two 1 dtype: int64對(duì)Series排序
import pandas as pd import numpy as npser_obj = pd.Series(range(10, 15), index=[5, 1, 3, 1, 2]) print("ser_obj:\n", ser_obj) print("sort:\n", ser_obj.sort_index()) # 升序排列 print("Descending order:\n", ser_obj.sort_index(ascending=False)) # 降序排列輸出結(jié)果:
ser_obj:5 10 1 11 3 12 1 13 2 14 dtype: int64 sort:1 11 1 13 2 14 3 12 5 10 dtype: int64 Descending order:5 10 3 12 2 14 1 11 1 13 dtype: int64對(duì)DataFrame排序
df_obj = pd.DataFrame(np.arange(12).reshape(3, 4), index=[2, 1, 3]) print("df_obj:\n", df_obj) print("sort:\n", df_obj.sort_index()) print("Descending order:\n", df_obj.sort_index(axis=1, ascending=False)) # 按columns進(jìn)行降序排序輸出結(jié)果:
df_obj:0 1 2 3 2 0 1 2 3 1 4 5 6 7 3 8 9 10 11 sort:0 1 2 3 1 4 5 6 7 2 0 1 2 3 3 8 9 10 11 Descending order:3 2 1 0 2 3 2 1 0 1 7 6 5 4 3 11 10 9 8按值排序——sort_value()
sort_value(by, axis=0, ascending=True, inplace=False, kind=‘quicksort’, na_position=‘last’)
上述方法中常用參數(shù):
by: 表示排序的列
na_position:為first則將NaN值放在開頭;為last則將NaN值放在最后
對(duì)Series進(jìn)行排序
ser_obj1 = pd.Series([4, np.nan, 6, np.nan, -3, 2]) print("ser_obj1:\n", ser_obj1) print("ser_obj1.sort_values():\n", ser_obj1.sort_values()) # 按值升序排列 print("sort_values(na_position='first'):\n", ser_obj1.sort_values(na_position='first'))輸出結(jié)果:
ser_obj1:0 4.0 1 NaN 2 6.0 3 NaN 4 -3.0 5 2.0 dtype: float64 ser_obj1.sort_values():4 -3.0 5 2.0 0 4.0 2 6.0 1 NaN 3 NaN dtype: float64 sort_values(na_position='first'):1 NaN 3 NaN 4 -3.0 5 2.0 0 4.0 2 6.0 dtype: float64對(duì)DataFrame進(jìn)行排序
df_obj1 = pd.DataFrame([[0.4, -0.1, -0.3, 0.0],[0.2, 0.6, -0.1, -0.7],[0.8, 0.6, -0.5, 0.1]]) print("df_obj1:\n", df_obj1) print("df1.value:\n", df_obj1.sort_values(by=2, ascending=False)) # 對(duì)列索引為2的數(shù)據(jù)進(jìn)行降序排序輸出結(jié)果:
df_obj1:0 1 2 3 0 0.4 -0.1 -0.3 0.0 1 0.2 0.6 -0.1 -0.7 2 0.8 0.6 -0.5 0.1 df1.value:0 1 2 3 1 0.2 0.6 -0.1 -0.7 0 0.4 -0.1 -0.3 0.0 2 0.8 0.6 -0.5 0.1總結(jié)
以上是生活随笔為你收集整理的Pandas数据排序——【按索引排序sort_index()方法、按值排序sort_value()方法】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 境外保险如何买
- 下一篇: [AR]ImageTarget(图像识别