কম্পিউটার

Python Pandas - একটি নির্দিষ্ট অবস্থানে একটি নতুন সূচক মান সন্নিবেশ করান


একটি নির্দিষ্ট অবস্থানে একটি নতুন সূচক মান সন্নিবেশ করতে, index.insert() ব্যবহার করুন পান্ডাসে পদ্ধতি। প্রথমে, প্রয়োজনীয় লাইব্রেরি আমদানি করুন -

import pandas as pd

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

index = pd.Index(['Car','Bike','Airplane','Ship','Truck'])

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

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

insert() পদ্ধতি ব্যবহার করে একটি নির্দিষ্ট অবস্থানে একটি নতুন মান সন্নিবেশ করান। insert() এর প্রথম প্যারামিটার হল সেই অবস্থান যেখানে নতুন সূচক মান স্থাপন করা হয়েছে। এখানে 2 এর অর্থ হল নতুন সূচক মানটি সূচক 2 এ ঢোকানো হয়েছে অর্থাৎ অবস্থান 3। দ্বিতীয় প্যারামিটারটি হল নতুন সূচক মান সন্নিবেশ করা হবে।

print("\nAfter inserting a new index value...\n", index.insert(2, 'Suburban'))

উদাহরণ

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

import pandas as pd

# Creating the Pandas index
index = pd.Index(['Car','Bike','Airplane','Ship','Truck'])

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

# Return the dtype of the data
print("\nThe dtype object...\n",index.dtype)

# Insert a new value at a specific position using the insert() method
# The first parameter in the insert() is the location where the new index value is placed.
# The 2 here means the new index value gets inserted at index 2 i.e. position 3
# The second parameter is the new index value to be inserted.
print("\nAfter inserting a new index value...\n", index.insert(2, 'Suburban'))

আউটপুট

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

তৈরি করবে
Pandas Index...
Index(['Car', 'Bike', 'Airplane', 'Ship', 'Truck'], dtype='object')

The dtype object...
object

After inserting a new index value...
Index(['Car', 'Bike', 'Suburban', 'Airplane', 'Ship', 'Truck'], dtype='object')

  1. Python Pandas - শেষ থেকে প্রথম সূচকে একটি নতুন সূচক মান সন্নিবেশ করান

  2. পাইথন - পান্ডাস ডেটাফ্রেমে ধ্রুবক মান সহ একটি নতুন কলাম যোগ করুন

  3. পাইথন - পান্ডাদের সাথে একটি নির্দিষ্ট মানের জন্য ডেটাফ্রেম অনুসন্ধান করুন

  4. পাইথন - তালিকায় সূচক মান পুনরাবৃত্তি