หากต้องการสร้างดัชนีใหม่โดยลบรายการป้ายกำกับที่ผ่านไปแล้ว ให้ใช้ index.drop() กระบวนการ. ผ่านรายการป้ายกำกับในนั้น
ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
import pandas as pd
การสร้างดัชนี -
index = pd.Index(['Car','Bike','Truck','Ship','Airplane'])
แสดงดัชนี -
print("Pandas Index...\n",index) ผ่านรายการที่มีป้ายกำกับที่จะละทิ้ง -
print("\nUpdated index after deleting labels...\n",index.drop(['Bike', 'Ship']))
ตัวอย่าง
ต่อไปนี้เป็นรหัส -
import pandas as pd
# Creating the index
index = pd.Index(['Car','Bike','Truck','Ship','Airplane'])
# Display the index
print("Pandas Index...\n",index)
# Return the dtype of the data
print("\nThe dtype object...\n",index.dtype)
# Return a tuple of the shape of the underlying data
print("\nA tuple of the shape of underlying data...\n",index.shape)
# a list containing labels to be dropped are passed
print("\nUpdated index after deleting labels...\n",index.drop(['Bike', 'Ship'])) ผลลัพธ์
สิ่งนี้จะสร้างรหัสต่อไปนี้ -
Pandas Index... Index(['Car', 'Bike', 'Truck', 'Ship', 'Airplane'], dtype='object') The dtype object... object A tuple of the shape of underlying data... (5,) Updated index after deleting labels... Index(['Car', 'Truck', 'Airplane'], dtype='object')