মূল সূচক থেকে একটি ডেটাফ্রেম তৈরি করতে কিন্তু একটি নতুন সূচী প্রয়োগ করতে, 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