
- Python Pandas 教程
- Python Pandas - 首頁
- Python Pandas - 簡介
- Python Pandas - 環境設定
- Python Pandas - 基礎
- Python Pandas - 資料結構介紹
- Python Pandas - 索引物件
- Python Pandas - 面板
- Python Pandas - 基本功能
- Python Pandas - 索引和資料選擇
- Python Pandas - Series
- Python Pandas - Series
- Python Pandas - 切片 Series 物件
- Python Pandas - Series 物件的屬性
- Python Pandas - Series 物件的算術運算
- Python Pandas - 將 Series 轉換為其他物件
- Python Pandas - DataFrame
- Python Pandas - DataFrame
- Python Pandas - 訪問 DataFrame
- Python Pandas - 切片 DataFrame 物件
- Python Pandas - 修改 DataFrame
- Python Pandas - 從 DataFrame 中刪除行
- Python Pandas - DataFrame 的算術運算
- Python Pandas - I/O 工具
- Python Pandas - I/O 工具
- Python Pandas - 使用 CSV 格式
- Python Pandas - 讀取和寫入 JSON 檔案
- Python Pandas - 從 Excel 檔案讀取資料
- Python Pandas - 將資料寫入 Excel 檔案
- Python Pandas - 處理 HTML 資料
- Python Pandas - 剪貼簿
- Python Pandas - 使用 HDF5 格式
- Python Pandas - 與 SQL 的比較
- Python Pandas - 資料處理
- Python Pandas - 排序
- Python Pandas - 重新索引
- Python Pandas - 迭代
- Python Pandas - 連線
- Python Pandas - 統計函式
- Python Pandas - 描述性統計
- Python Pandas - 處理文字資料
- Python Pandas - 函式應用
- Python Pandas - 選項和自定義
- Python Pandas - 視窗函式
- Python Pandas - 聚合
- Python Pandas - 合併/連線
- Python Pandas - 多級索引
- Python Pandas - 多級索引基礎
- Python Pandas - 使用多級索引進行索引
- Python Pandas - 使用多級索引進行高階重新索引
- Python Pandas - 重新命名多級索引標籤
- Python Pandas - 對多級索引進行排序
- Python Pandas - 二元運算
- Python Pandas - 二元比較運算
- Python Pandas - 布林索引
- Python Pandas - 布林掩碼
- Python Pandas - 資料重塑和透視
- Python Pandas - 透視表
- Python Pandas - 堆疊和取消堆疊
- Python Pandas - 熔化
- Python Pandas - 計算虛擬變數
- Python Pandas - 類別資料
- Python Pandas - 類別資料
- Python Pandas - 類別資料的排序和分類
- Python Pandas - 類別資料的比較
- Python Pandas - 處理缺失資料
- Python Pandas - 缺失資料
- Python Pandas - 填充缺失資料
- Python Pandas - 缺失值的插值
- Python Pandas - 刪除缺失資料
- Python Pandas - 使用缺失資料進行計算
- Python Pandas - 處理重複資料
- Python Pandas - 重複資料
- Python Pandas - 計數和檢索唯一元素
- Python Pandas - 重複標籤
- Python Pandas - 分組和聚合
- Python Pandas - GroupBy
- Python Pandas - 時間序列資料
- Python Pandas - 日期功能
- Python Pandas - Timedelta
- Python Pandas - 稀疏資料結構
- Python Pandas - 稀疏資料
- Python Pandas - 視覺化
- Python Pandas - 視覺化
- Python Pandas - 其他概念
- Python Pandas - 注意事項和陷阱
- Python Pandas 有用資源
- Python Pandas - 快速指南
- Python Pandas - 有用資源
- Python Pandas - 討論
Python Pandas - 排序
排序是在 Pandas 中處理資料時一項基本的操作,無論您是在組織行、列還是特定值。排序可以幫助您以有意義的方式排列資料,以便更好地理解和輕鬆分析。
Pandas 提供了強大的工具來有效地對資料進行排序,這可以透過標籤或實際值來完成。在本教程中,我們將探討 Pandas 中各種資料排序方法,從基本的按索引或列標籤排序到更高階的技術,如按多列排序和選擇特定的排序演算法。
Pandas 中的排序型別
Pandas 中有兩種排序方式。它們是 -
按標籤排序 - 這涉及根據索引標籤對資料進行排序。
按值排序 - 這涉及根據 DataFrame 或 Series 中的實際值對資料進行排序。
按標籤排序
要按索引標籤排序,您可以使用sort_index()方法,透過傳遞軸引數和排序順序,可以對資料結構物件進行排序。預設情況下,此方法會根據行標籤按升序對 DataFrame 進行排序。
示例
讓我們以一個基本的示例來說明如何使用sort_index()方法對 DataFrame 進行排序。
import pandas as pd import numpy as np unsorted_df = pd.DataFrame(np.random.randn(10,2),index=[1,4,6,2,3,5,9,8,0,7],columns = ['col2','col1']) print("Original DataFrame:\n", unsorted_df) # Sort the DataFrame by labels sorted_df=unsorted_df.sort_index() print("\nOutput Sorted DataFrame:\n", sorted_df)
其輸出如下 -
Original DataFrame: col2 col1 1 1.116188 1.631727 4 0.287900 -1.097359 6 0.058885 -0.642273 2 -2.070172 0.148255 3 -1.458229 1.298907 5 -0.723663 2.220048 9 -1.271494 2.001025 8 -0.412954 -0.808688 0 0.922697 -0.429393 7 -0.476054 -0.351621 Output Sorted DataFrame: col2 col1 0 0.922697 -0.429393 1 1.116188 1.631727 2 -2.070172 0.148255 3 -1.458229 1.298907 4 0.287900 -1.097359 5 -0.723663 2.220048 6 0.058885 -0.642273 7 -0.476054 -0.351621 8 -0.412954 -0.808688 9 -1.271494 2.001025
示例 - 控制排序順序
透過向 ascending 引數傳遞布林值,可以控制排序順序。讓我們考慮以下示例來理解這一點。
import pandas as pd import numpy as np unsorted_df = pd.DataFrame(np.random.randn(10,2),index=[1,4,6,2,3,5,9,8,0,7],columns = ['col2','col1']) print("Original DataFrame:\n", unsorted_df) # Sort the DataFrame by ascending order sorted_df = unsorted_df.sort_index(ascending=False) print("\nOutput Sorted DataFrame:\n", sorted_df)
其輸出如下 -
Original DataFrame: col2 col1 1 -0.668366 0.576422 4 0.605218 -0.066065 6 1.140478 0.236687 2 0.137617 0.312423 3 -0.055631 0.774057 5 0.108002 1.038820 9 -0.929134 -0.982358 8 -0.207542 -1.283386 0 -0.210571 -0.656371 7 -0.106388 0.672418 Output Sorted DataFrame: col2 col1 9 -0.929134 -0.982358 8 -0.207542 -1.283386 7 -0.106388 0.672418 6 1.140478 0.236687 5 0.108002 1.038820 4 0.605218 -0.066065 3 -0.055631 0.774057 2 0.137617 0.312423 1 -0.668366 0.576422 0 -0.210571 -0.656371
示例 - 對列進行排序
透過向 axis 引數傳遞值 0 或 1,可以在列標籤上進行排序。預設情況下,axis=0,按行排序。讓我們考慮以下示例來理解這一點。
import pandas as pd import numpy as np unsorted_df = pd.DataFrame(np.random.randn(6,4),index=[1,4,2,3,5,0],columns = ['col2','col1', 'col4', 'col3']) print("Original DataFrame:\n", unsorted_df) # Sort the DataFrame columns sorted_df=unsorted_df.sort_index(axis=1) print("\nOutput Sorted DataFrame:\n", sorted_df)
其輸出如下 -
Original DataFrame: col2 col1 col4 col3 1 -0.828951 -0.798286 -1.794752 -0.082656 4 0.440243 -0.693218 -0.218277 -0.790168 2 1.017670 1.443679 -1.939119 -1.887223 3 -0.992471 -1.425046 0.651336 -0.278247 5 -0.103537 -0.879433 0.471838 0.860885 0 -0.222297 1.094805 0.501531 -0.580382 Output Sorted DataFrame: col1 col2 col3 col4 1 -0.798286 -0.828951 -0.082656 -1.794752 4 -0.693218 0.440243 -0.790168 -0.218277 2 1.443679 1.017670 -1.887223 -1.939119 3 -1.425046 -0.992471 -0.278247 0.651336 5 -0.879433 -0.103537 0.860885 0.471838 0 1.094805 -0.222297 -0.580382 0.501531
按實際值排序
與索引排序類似,可以使用sort_values()方法按實際值進行排序。此方法允許按一個或多個列進行排序。它接受一個“by”引數,該引數將使用 DataFrame 的列名來對值進行排序。
示例 - 對 Series 值進行排序
以下示例演示瞭如何使用sort_values()方法對 pandas Series 物件進行排序。
import pandas as pd panda_series = pd.Series([18, 95, 66, 12, 55, 0]) print("Unsorted Pandas Series: \n", panda_series) panda_series_sorted = panda_series.sort_values(ascending=True) print("\nSorted Pandas Series: \n", panda_series_sorted)
執行以上程式碼後,您將獲得以下輸出 -
Unsorted Pandas Series: 0 18 1 95 2 66 3 12 4 55 5 0 dtype: int64 Sorted Pandas Series: 5 0 3 12 0 18 4 55 2 66 1 95 dtype: int64
示例 - 對 DataFrame 值進行排序
以下示例演示了sort_values()方法在 DataFrame 物件上的工作原理。
import pandas as pd import numpy as np unsorted_df = pd.DataFrame({'col1':[2,9,5,0],'col2':[1,3,2,4]}) print("Original DataFrame:\n", unsorted_df) # Sort the DataFrame by values sorted_df = unsorted_df.sort_values(by='col1') print("\nOutput Sorted DataFrame:\n", sorted_df)
其輸出如下 -
Original DataFrame: col1 col2 0 2 1 1 9 3 2 5 2 3 0 4 Output Sorted DataFrame: col1 col2 3 0 4 0 2 1 2 5 2 1 9 3
觀察,col1 值已排序,相應的 col2 值和行索引將與 col1 一起更改。因此,它們看起來未排序。
示例 - 對多個列的值進行排序
您還可以透過向'by'引數傳遞列名列表來按多列排序。
import pandas as pd import numpy as np unsorted_df = pd.DataFrame({'col1':[2,1,0,1],'col2':[1,3,4,2]}) print("Original DataFrame:\n", unsorted_df) # Sort the DataFrame multiple columns by values sorted_df = unsorted_df.sort_values(by=['col1','col2']) print("\nOutput Sorted DataFrame:\n", sorted_df)
其輸出如下 -
Original DataFrame: col1 col2 0 2 1 1 1 3 2 0 4 3 1 2 Output Sorted DataFrame: col1 col2 2 0 4 3 1 2 1 1 3 0 2 1
選擇排序演算法
Pandas 允許您使用sort_values()方法中的 kind 引數指定排序演算法。您可以在“mergesort”、“heapsort”和“quicksort”之間進行選擇。“mergesort”是唯一穩定的演算法。
示例
以下示例使用sort_values()方法和特定演算法對 DataFrame 進行排序。
import pandas as pd import numpy as np unsorted_df = pd.DataFrame({'col1':[2,5,0,1],'col2':[1,3,0,4]}) print("Original DataFrame:\n", unsorted_df) # Sort the DataFrame sorted_df = unsorted_df.sort_values(by='col1' ,kind='mergesort') print("\nOutput Sorted DataFrame:\n", sorted_df)
其輸出如下 -
Original DataFrame: col1 col2 0 2 1 1 5 3 2 0 0 3 1 4 Output Sorted DataFrame: col1 col2 2 0 0 3 1 4 0 2 1 1 5 3