एक मल्टीइंडेक्स में लेबल के अनुक्रम के लिए स्थान प्राप्त करने के लिए, MutiIndex.get_locs() का उपयोग करें। पंडों में विधि।
सबसे पहले, आवश्यक पुस्तकालयों को आयात करें -
import pandas as pd
मल्टीइंडेक्स पांडा वस्तुओं के लिए एक बहु-स्तरीय, या पदानुक्रमित, अनुक्रमणिका वस्तु है -
multiIndex = pd.MultiIndex.from_arrays([list('pqrrst'), list('kytssp')])
मल्टीइंडेक्स प्रदर्शित करें -
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]