ในการส่งคืนดัชนีใหม่ของค่าที่ตั้งไว้ด้วยมาสก์ ให้ใช้ index.putmask() วิธีการในแพนด้า ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
import pandas as pd
การสร้างดัชนีนุ่น -
index = pd.Index([5, 65, 10, 17, 75, 40])
แสดงดัชนีหมีแพนด้า −
print("Pandas Index...\n",index) มาสก์และวางค่าดัชนีที่น้อยกว่า 3 ด้วยค่า 111 -
print("\nMask...\n",index.putmask(index < 30, 111))
ตัวอย่าง
ต่อไปนี้เป็นรหัส -
import pandas as pd
# Creating Pandas index
index = pd.Index([5, 65, 10, 17, 75, 40])
# 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)
# mask and place index values less than 3 with a value 111
print("\nMask...\n",index.putmask(index < 30, 111)) ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
Pandas Index... Int64Index([5, 65, 10, 17, 75, 40], dtype='int64') Number of elements in the index... 6 The dtype object... int64 Mask... Int64Index([111, 65, 111, 111, 75, 40], dtype='int64')