如何對 Pandas 資料序列進行排序?
在這個問題中,我們必須對 Pandas 資料序列進行排序。我們定義一個未排序的 pandas 資料序列,並使用 Pandas 庫中的 sort_values()函式對它進行排序。
演算法
Step 1: Define Pandas series. Step 2: Sort the series using sort_values() function. Step 3: Print the sorted series.
示例程式碼
import pandas as pd
panda_series = pd.Series([18,15,66,92,55,989])
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 15 2 66 3 92 4 55 5 989 dtype: int64 Sorted Pandas Series: 1 15 0 18 4 55 2 66 3 92 5 989 dtype: int64
解釋
sort_values()函式中的 ascending 引數採用布林值。如果值為 True,它將按升序對序列進行排序。如果值為 False,它將按降序對值進行排序。
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP