Để trả về bản sao của Index đã sắp xếp (mặc định theo thứ tự tăng dần), sử dụng phương thức index.sort_values() trong Pandas.
Các bước thực hiện
- Import thư viện:
import pandas as pd - Tạo Index:
index = pd.Index([50, 10, 70, 95, 110, 90, 30]) - Gọi
index.sort_values().
Ví dụ hoàn chỉnh
import pandas as pd
index = pd.Index([50, 10, 70, 95, 110, 90, 30])
print("Pandas Index...\n", index)
print("\nNumber of elements in the index...\n", index.size)
print("\nThe dtype object...\n", index.dtype)
# Sắp xếp tăng dần (mặc định)
print("\nSort the index values...\n", index.sort_values())
Kết quả
Pandas Index...
Int64Index([50, 10, 70, 95, 110, 90, 30], dtype='int64')
Number of elements in the index...
7
The dtype object... int64
Sort the index values...
Int64Index([10, 30, 50, 70, 90, 95, 110], dtype='int64')