मल्टीइंडेक्स में अनुरोधित लेबल/स्तर के लिए स्थान और कटा हुआ सूचकांक प्राप्त करने के लिए, get_loc_level() का उपयोग करें पंडों में विधि। ड्रॉप_लेवल . का उपयोग करें पैरामीटर और इसे सेट करें गलत स्तर गिरने से बचने के लिए।
सबसे पहले, आवश्यक पुस्तकालयों को आयात करें -
import pandas as pd
मल्टीइंडेक्स पांडा वस्तुओं के लिए एक बहु-स्तरीय, या पदानुक्रमित, अनुक्रमणिका वस्तु है -
multiIndex = pd.MultiIndex.from_arrays([list('pqrrss'), list('strvwx')],names=['One', 'Two']) मल्टीइंडेक्स प्रदर्शित करें -
print("The MultiIndex...\n",multiIndex)
स्थान और कटा हुआ सूचकांक प्राप्त करें। एक स्तर को गिराने से बचने के लिए, हमने "ड्रॉप_लेवल" पैरामीटर का उपयोग "गलत" मान के साथ किया है -
print("\nGet the location and sliced index (avoid dropping the level)...\n",multiIndex.get_loc_level('r', drop_level=False)) उदाहरण
निम्नलिखित कोड है -
import pandas as pd
# MultiIndex is a multi-level, or hierarchical, index object for pandas objects
multiIndex = pd.MultiIndex.from_arrays([list('pqrrss'), list('strvwx')],names=['One', 'Two'])
# display the MultiIndex
print("The MultiIndex...\n",multiIndex)
# get the levels in MultiIndex
print("\nThe levels in MultiIndex...\n",multiIndex.levels)
# Get the location and sliced index
# To avoid dropping a level, we have used the "drop_level" parameter with value "False"
print("\nGet the location and sliced index (avoid dropping the level)...\n",multiIndex.get_loc_level('r', drop_level=False)) आउटपुट
यह निम्नलिखित आउटपुट उत्पन्न करेगा -
The MultiIndex...
MultiIndex([('p', 's'),
('q', 't'),
('r', 'r'),
('r', 'v'),
('s', 'w'),
('s', 'x')],
names=['One', 'Two'])
The levels in MultiIndex...
[['p', 'q', 'r', 's'], ['r', 's', 't', 'v', 'w', 'x']]
Get the location and sliced index (avoid dropping the level)...
(slice(2, 4, None), MultiIndex([('r', 'r'),('r', 'v')],names=['One', 'Two']))