কম্পিউটার

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


অনুরোধ করা লেবেলের জন্য পূর্ণসংখ্যার অবস্থান পেতে এবং সঠিক মিল না থাকলে পূর্ববর্তী সূচক মান খুঁজে পেতে, index.get_loc() ব্যবহার করুন . প্যারামিটার পদ্ধতি সেট করুন মান ফিল .

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

import pandas as pd

পান্ডাস সূচক তৈরি করা হচ্ছে -

index = pd.Index([10, 20, 30, 40, 50, 60, 70])

পান্ডাস সূচক প্রদর্শন করুন -

print("Pandas Index...\n",index)

কোনো সঠিক মিল না থাকলে পূর্ববর্তী সূচকের অবস্থান পান। get_loc() -

-এর "পদ্ধতি" প্যারামিটার ব্যবহার করে মান "ফিল" সেট করা হয়েছে
print("\nGet the location of the previous index if no exact match...\n", index.get_loc(45, method="ffill"))

উদাহরণ

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

import pandas as pd

# Creating Pandas index
index = pd.Index([10, 20, 30, 40, 50, 60, 70])

# Display the Pandas index
print("Pandas Index...\n",index)

# Return the number of elements in the Index
print("\nNumber of elements in the index...\n",index.size)

# get integer location from the given index
print("\nDisplay integer location from given index...\n",index.get_loc(20))
print("\nDisplay integer location from given index...\n",index.get_loc(50))

# Get the location of the previous index if no exact match
# The value is set "ffill" using the "method" parameter of the get_loc()
print("\nGet the location of the previous index if no exact match...\n", index.get_loc(45, method="ffill"))

আউটপুট

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

তৈরি করবে
Pandas Index...
Int64Index([10, 20, 30, 40, 50, 60, 70], dtype='int64')

Number of elements in the index...
7

Display integer location from given index...
1

Display integer location from given index...
4

Get the location of the previous index if no exact match...
3

  1. Python Pandas - ব্যবধানের জন্য সঠিক আবদ্ধ পান

  2. পাইথন - সূচক মানের পণ্য খুঁজুন এবং সমষ্টি খুঁজুন

  3. Python Pandas - একটি কলামের সর্বোচ্চ মান খুঁজুন এবং এর সংশ্লিষ্ট সারি মান ফেরত দিন

  4. একটি প্রদত্ত সিরিজে NaN মানের জন্য সূচক খুঁজে পেতে পাইথনে একটি প্রোগ্রাম লিখুন