কম্পিউটার

পাইথন পান্ডাস - মূল সূচক থেকে একটি ডেটাফ্রেম তৈরি করুন কিন্তু একটি নতুন সূচী প্রয়োগ করুন


মূল সূচক থেকে একটি ডেটাফ্রেম তৈরি করতে কিন্তু একটি নতুন সূচী প্রয়োগ করতে, index.to_frame() ব্যবহার করুন। প্যারামিটার সূচী সেট করুন মিথ্যাতে .

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

import pandas as pd

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

index = pd.Index(['Electronics','Accessories','Decor', 'Books', 'Toys'],name ='Products')

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

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

নতুন সূচী প্রয়োগ করুন এবং সূচককে ডেটাফ্রেমে রূপান্তর করুন। এখানে, প্রকৃত সূচকটি অন্য একটি সূচক -

দ্বারা প্রতিস্থাপিত হয়
print("\nIndex to DataFrame...\n",index.to_frame(index=False))

উদাহরণ

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

import pandas as pd

# Creating Pandas index
index = pd.Index(['Electronics','Accessories','Decor', 'Books', 'Toys'],name ='Products')

# 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)

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

# Enforce new index and convert index to DataFrame
# Here, the actual index gets replaced by another index
print("\nIndex to DataFrame...\n",index.to_frame(index=False))

আউটপুট

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

তৈরি করবে
Pandas Index...
Index(['Electronics', 'Accessories', 'Decor', 'Books', 'Toys'], dtype='object', name='Products')

Number of elements in the index...
5

The dtype object...
object

Index to DataFrame...
      Products
0  Electronics
1  Accessories
2        Decor
3        Books
4         Toys

  1. পাইথন - পান্ডাসে একটি পাইপলাইন তৈরি করুন

  2. পাইথন - পান্ডাস ডেটাফ্রেমে একটি মাল্টি-লেভেল কলাম সূচক থেকে একাধিক স্তর বাদ দিন

  3. পাইথন - পান্ডাস ডেটাফ্রেমে বহু-স্তরের কলাম সূচক থেকে একটি স্তর ড্রপ করুন

  4. পাইথনে সমান দৈর্ঘ্যের তালিকা থেকে একটি পান্ডাস ডেটাফ্রেম তৈরি করুন