ในการรับตำแหน่งสำหรับลำดับของป้ายกำกับใน MultiIndex ให้ใช้ MutiIndex.get_locs() วิธีการในนุ่น
ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
import pandas as pd
MultiIndex เป็นวัตถุดัชนีหลายระดับหรือแบบลำดับชั้นสำหรับวัตถุแพนด้า -
multiIndex = pd.MultiIndex.from_arrays([list('pqrrst'), list('kytssp')])
แสดง MultiIndex -
print("The MultiIndex...\n",multiIndex) รับตำแหน่งสำหรับลำดับฉลาก -
print("\nGet the locations in MultiIndex...\n",multiIndex.get_locs('s'))
ตัวอย่าง
ต่อไปนี้เป็นรหัส -
import pandas as pd
# MultiIndex is a multi-level, or hierarchical, index object for pandas objects
multiIndex = pd.MultiIndex.from_arrays([list('pqrrst'), list('kytssp')])
# display the MultiIndex
print("The MultiIndex...\n",multiIndex)
# get the levels in MultiIndex
print("\nThe levels in MultiIndex...\n",multiIndex.levels)
# Get the location for a sequence of labels
print("\nGet the locations in MultiIndex...\n",multiIndex.get_locs('s')) ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
The MultiIndex...
MultiIndex([('p', 'k'),
('q', 'y'),
('r', 't'),
('r', 's'),
('s', 's'),
('t', 'p')],
)
The levels in MultiIndex...
[['p', 'q', 'r', 's', 't'], ['k', 'p', 's', 't', 'y']]
Get the locations in MultiIndex...
[4]