หากต้องการตรวจสอบว่าดัชนีว่างเปล่าโดยมีองค์ประกอบ 0 หรือไม่ ให้ใช้ index.empty ทรัพย์สินในหมีแพนด้า
ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
import pandas as pd
การสร้างดัชนี -
index = pd.Index([])
แสดงดัชนี -
print("Pandas Index...\n",index) ตรวจสอบดัชนีว่าง -
print("\nIs the index empty?\n",index.empty)
ตัวอย่าง
ต่อไปนี้เป็นรหัส -
import pandas as pd
# Creating the index
index = pd.Index([])
# Display the index
print("Pandas Index...\n",index)
# Return the number of elements in the Index
print("\nNumber of elements in the index...\n",index.size)
# check for empty index
print("\nIs the index empty?\n",index.empty) ผลลัพธ์
สิ่งนี้จะสร้างรหัสต่อไปนี้ -
Pandas Index... Index([], dtype='object') Number of elements in the index... 0 Is the index empty? True