Để tính slice indexer cho nhãn đầu vào, sử dụng phương thức index.slice_indexer().
Trước tiên, import thư viện:
import pandas as pd
Tạo đối tượng Index Pandas:
index = pd.Index(list('pqrstuvwxyz'))
Hiển thị Index:
print("Pandas Index...\n", index)
Lấy slice indexer. start là nhãn bắt đầu, end là nhãn kết thúc:
print("\nSlice indexer với start và stop...\n", index.slice_indexer(start='s', end='w'))
Ví dụ hoàn chỉnh
import pandas as pd
index = pd.Index(list('pqrstuvwxyz'))
print("Pandas Index...\n", index)
print("\nSố phần tử trong index...\n", index.size)
print("\nSlice indexer với start và stop...\n", index.slice_indexer(start='s', end='w'))
Kết quả đầu ra
Pandas Index... Index(['p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'], dtype='object') Số phần tử trong index... 11 Slice indexer với start và stop... slice(3, 8, None)