Để tính vị trí cắt cho nhãn đầu vào, sử dụng phương thức index.slice_locs().
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 vị trí cắt. start là nhãn bắt đầu, end là nhãn kết thúc:
print("\nVị trí cắt với start và stop...\n", index.slice_locs(start='q', end='v'))
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("\nVị trí cắt với start và stop...\n", index.slice_locs(start='q', end='v'))
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 Vị trí cắt với start và stop... (1, 7)