日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Pandas数据排序——【按索引排序sort_index()方法、按值排序sort_value()方法】

發布時間:2023/12/13 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Pandas数据排序——【按索引排序sort_index()方法、按值排序sort_value()方法】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 按索引排序——sort_index()
    • 對Series排序
    • 對DataFrame排序
  • 按值排序——sort_value()
    • 對Series進行排序
    • 對DataFrame進行排序


按索引排序——sort_index()

sort_index(axis=0, level=None, ascending=True, inplace=False, kind=‘quicksort’, na_position=‘last’,sort_remaining=True)

上述方法中常用參數:

axis:軸索引(排序的方向),0表示按index,1表示按columns

level:若不為None,則對指定索引級別的值進行排序

ascending:是否升序排列,默認為True,表示升序

inplace:默認為False,表示對數據表進行排序,不創建新的實例

kind:選擇排序算法

這里只舉例kind參數的作用,其他參數在下文的應用中會被用到 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))

輸出結果:

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

對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)) # 降序排列

輸出結果:

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

對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進行降序排序

輸出結果:

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’)

上述方法中常用參數:

by: 表示排序的列

na_position:為first則將NaN值放在開頭;為last則將NaN值放在最后

對Series進行排序

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'))

輸出結果:

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

對DataFrame進行排序

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)) # 對列索引為2的數據進行降序排序

輸出結果:

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

總結

以上是生活随笔為你收集整理的Pandas数据排序——【按索引排序sort_index()方法、按值排序sort_value()方法】的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。