কম্পিউটার

Python Pandas - অনুরোধ করা লেবেল/স্তরের জন্য অবস্থান এবং কাটা সূচী পান তবে স্তরটি বাদ দেবেন না


একটি মাল্টি ইনডেক্সে অনুরোধকৃত লেবেল/স্তরের জন্য অবস্থান এবং স্লাইস করা সূচক পেতে, get_loc_level() ব্যবহার করুন পান্ডাসে পদ্ধতি। ড্রপ_লেভেল ব্যবহার করুন প্যারামিটার এবং সেট করুন মিথ্যা লেভেল ড্রপ এড়াতে।

প্রথমে, প্রয়োজনীয় লাইব্রেরিগুলি আমদানি করুন -

import pandas as pd

মাল্টি-ইন্ডেক্স হল পান্ডাস অবজেক্টের জন্য একটি মাল্টি-লেভেল, বা হায়ারার্কিক্যাল, ইনডেক্স অবজেক্ট -

multiIndex = pd.MultiIndex.from_arrays([list('pqrrss'), list('strvwx')],names=['One', 'Two'])

মাল্টি ইনডেক্স −

প্রদর্শন করুন
print("The MultiIndex...\n",multiIndex)

অবস্থান এবং কাটা সূচক পান. একটি লেভেল ড্রপ এড়াতে, আমরা "False" −

মান সহ "ড্রপ_লেভেল" প্যারামিটার ব্যবহার করেছি।
print("\nGet the location and sliced index (avoid dropping the level)...\n",multiIndex.get_loc_level('r', drop_level=False))

উদাহরণ

নিম্নলিখিত কোড -

import pandas as pd

# MultiIndex is a multi-level, or hierarchical, index object for pandas objects
multiIndex = pd.MultiIndex.from_arrays([list('pqrrss'), list('strvwx')],names=['One', 'Two'])

# display the MultiIndex
print("The MultiIndex...\n",multiIndex)

# get the levels in MultiIndex
print("\nThe levels in MultiIndex...\n",multiIndex.levels)

# Get the location and sliced index
# To avoid dropping a level, we have used the "drop_level" parameter with value "False"
print("\nGet the location and sliced index (avoid dropping the level)...\n",multiIndex.get_loc_level('r', drop_level=False))

আউটপুট

এটি নিম্নলিখিত আউটপুট −

তৈরি করবে
The MultiIndex...
MultiIndex([('p', 's'),
            ('q', 't'),
            ('r', 'r'),
            ('r', 'v'),
            ('s', 'w'),
            ('s', 'x')],
            names=['One', 'Two'])

The levels in MultiIndex...
   [['p', 'q', 'r', 's'], ['r', 's', 't', 'v', 'w', 'x']]

Get the location and sliced index (avoid dropping the level)...
(slice(2, 4, None), MultiIndex([('r', 'r'),('r', 'v')],names=['One', 'Two']))

  1. Python Pandas - সূচী থেকে লেবেল ফেরত দিন বা উপস্থিত না থাকলে, আগেরটি

  2. Python Pandas - সূচকের উপাদানগুলির সাথে একটি নতুন সূচক ফেরত দিন এবং পার্থক্য পান৷

  3. পাইথন - স্তরের নাম ব্যবহার করে একাধিক স্তর সরান এবং সূচকটি ফেরত দিন

  4. পাইথন - স্তরের নাম ব্যবহার করে একটি স্তর সরান এবং সূচকটি ফেরত দিন