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