মাল্টি ইনডেক্স থেকে প্রতিটি স্তরের দৈর্ঘ্য সহ একটি টিপল পেতে, 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)