MultiIndex से प्रत्येक स्तर की लंबाई के साथ एक टपल प्राप्त करने के लिए, MultiIndex.levshape का उपयोग करें पंडों में संपत्ति।
सबसे पहले, आवश्यक पुस्तकालयों को आयात करें -
import pandas as pd
मल्टीइंडेक्स पांडा वस्तुओं के लिए एक बहु-स्तरीय, या पदानुक्रमित, अनुक्रमणिका वस्तु है। सरणियाँ बनाएँ -
arrays = [[1, 2, 3, 4, 5], ['John', 'Tim', 'Jacob', 'Chris', 'Keiron']]
"नाम" पैरामीटर प्रत्येक सूचकांक स्तर के लिए नाम निर्धारित करता है। From_arrays() uis एक मल्टीइंडेक्स बनाने के लिए उपयोग किया जाता है -
multiIndex = pd.MultiIndex.from_arrays(arrays, names=('ranks', 'student')) प्रत्येक स्तर की लंबाई के साथ एक टपल प्राप्त करें -
print("\nThe tuple with the length of each level in a Multi-index...\n",multiIndex.levshape) उदाहरण
निम्नलिखित कोड है -
import pandas as pd
# MultiIndex is a multi-level, or hierarchical, index object for pandas objects
# Create arrays
arrays = [[1, 2, 3, 4, 5], ['John', 'Tim', 'Jacob', 'Chris', 'Keiron']]
# The "names" parameter sets the names for each of the index levels
# The from_arrays() uis used to create a Multiindex
multiIndex = pd.MultiIndex.from_arrays(arrays, names=('ranks', 'student'))
# display the Multiindex
print("The Multi-index...\n",multiIndex)
# get the integer number of levels in Multiindex
print("\nThe number of levels in Multi-index...\n",multiIndex.nlevels)
# get the levels in Multiindex
print("\nThe levels in Multi-index...\n",multiIndex.levels)
# get a tuple with the length of each level
print("\nThe tuple with the length of each level in a Multi-index...\n",multiIndex.levshape) आउटपुट
यह निम्नलिखित आउटपुट उत्पन्न करेगा -
The Multi-index...
MultiIndex([(1, 'John'),
(2, 'Tim'),
(3, 'Jacob'),
(4, 'Chris'),
(5, 'Keiron')],
names=['ranks', 'student'])
The number of levels in Multi-index...
2
The levels in Multi-index...
[[1, 2, 3, 4, 5], ['Chris', 'Jacob', 'John', 'Keiron', 'Tim']]
The tuple with the length of each level in a Multi-index...
(5, 5)