एक मल्टीइंडेक्स में किसी लेबल या लेबल के टपल के लिए स्थान प्राप्त करने के लिए, MultiIndex.get_loc() का उपयोग करें। पंडों में विधि।
सबसे पहले, आवश्यक पुस्तकालयों को आयात करें -
import pandas as pd
मल्टीइंडेक्स पांडा वस्तुओं के लिए एक बहु-स्तरीय, या पदानुक्रमित, अनुक्रमणिका वस्तु है -
multiIndex = pd.MultiIndex.from_arrays([list('pqrrss'), list('stuvwx')])
मल्टीइंडेक्स प्रदर्शित करें -
print("The MultiIndex...\n",multiIndex) स्थान प्राप्त करें -
print("\nGet the locations in MultiIndex...\n",multiIndex.get_loc('s'))
उदाहरण
निम्नलिखित कोड है -
import pandas as pd
# MultiIndex is a multi-level, or hierarchical, index object for pandas objects
multiIndex = pd.MultiIndex.from_arrays([list('pqrrss'), list('stuvwx')])
# display the MultiIndex
print("The MultiIndex...\n",multiIndex)
# get the levels in MultiIndex
print("\nThe levels in MultiIndex...\n",multiIndex.levels)
# Get the location
print("\nGet the locations in MultiIndex...\n",multiIndex.get_loc('s')) आउटपुट
यह निम्नलिखित आउटपुट देगा -
The MultiIndex...
MultiIndex([('p', 's'),
('q', 't'),
('r', 'u'),
('r', 'v'),
('s', 'w'),
('s', 'x')],
)
The levels in MultiIndex...
[['p', 'q', 'r', 's'], ['s', 't', 'u', 'v', 'w', 'x']]
Get the locations in MultiIndex...
slice(4, 6, None)