প্যান্ডাস Index-এর নিচের ডেটা বাফার শেয়ার করে একটি নতুন ভিউ (view) তৈরি করতে index.view(dtype) মেথড ব্যবহার করুন। এটি কপি তৈরি করে না, মেমরি শেয়ার করে।
প্রয়োজনীয় লাইব্রেরি ইমপোর্ট করুন
import pandas as pd
প্যান্ডাস ইনডেক্স তৈরি করুন
index = pd.Index([50, 10, 70, 110, 90, 50, 110, 90, 30])
ইনডেক্স প্রদর্শন করুন
print("Pandas Index...\n", index)
নতুন ভিউ তৈরি করুন (uint8 টাইপে)
res = index.view('uint8')
নতুন ভিউ প্রদর্শন করুন
print("\nThe new view...\n", res)
ভিউ শেয়ার করা মেমরি যাচাই করুন
print("\nView for 0th index...\n", res[0])
print("\nView for 1st index...\n", res[1])
সম্পূর্ণ উদাহরণ
import pandas as pd
# Creating Pandas index
index = pd.Index([50, 10, 70, 110, 90, 50, 110, 90, 30])
# 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)
# Create a new view
res = index.view('uint8')
# displaying the new view
print("\nThe new view...\n", res)
# shares the same underlying values
print("\nView for 0th index...\n", res[0])
print("\nView for 1st index...\n", res[1])
আউটপুট
Pandas Index... Int64Index([50, 10, 70, 110, 90, 50, 110, 90, 30], dtype='int64')
Number of elements in the index... 9
The dtype object... int64
The new view... [ 50 0 0 0 0 0 0 0 10 0 0 0 0 0 0 0 70 0 0 0 0 0 0 0 110 0 0 0 0 0 0 0 90 0 0 0 0 0 0 0 50 0 0 0 0 0 0 0 110 0 0 0 0 0 0 0 90 0 0 0 0 0 0 0 30 0 0 0 0 0 0 0]
View for 0th index... 50
View for 1st index... 0
ব্যাখ্যা: int64 (৮ বাইট) কে uint8 (১ বাইট) ভিউতে কনভার্ট করলে প্রতিটি সংখ্যা ৮ বাইটে বিস্তারিত হয়। ৫০ = 0x32, তাই প্রথম বাইট ৫০, বাকি ৭টি বাইট ০। ভিউ মূল ডেটা শেয়ার করে, তাই একটা পরিবর্তন অন্যটিতে প্রতিফলিত হবে।