Pandas में MultiIndex के लिए किसी लेबल या लेबल्स के टपल की लोकेशन प्राप्त करने के लिए MultiIndex.get_loc() मेथड का उपयोग करें।
आवश्यक लाइब्रेरी आयात करें
import pandas as pd
MultiIndex बनाएं
MultiIndex पैंडास ऑब्जेक्ट्स के लिए एक मल्टी-लेवल या हायरार्किकल इंडेक्स ऑब्जेक्ट है।
multiIndex = pd.MultiIndex.from_arrays([list('pqrrss'), list('stuvwx')])
MultiIndex प्रदर्शित करें
print("The MultiIndex...\n", multiIndex)
लोकेशन प्राप्त करें
print("\nGet the locations in MultiIndex...\n", multiIndex.get_loc('s'))
पूरा उदाहरण
import pandas as pd
# MultiIndex एक मल्टी-लेवल, या हायरार्किकल, इंडेक्स ऑब्जेक्ट है पैंडास ऑब्जेक्ट्स के लिए
multiIndex = pd.MultiIndex.from_arrays([list('pqrrss'), list('stuvwx')])
# MultiIndex प्रदर्शित करें
print("The MultiIndex...\n", multiIndex)
# MultiIndex में स्तर प्राप्त करें
print("\nThe levels in MultiIndex...\n", multiIndex.levels)
# लोकेशन प्राप्त करें
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)