หากต้องการคืนค่าดัชนีแบบโมโนโทนิก (เท่ากับหรือเพิ่มขึ้นเท่านั้น) ให้ใช้ index.is_monotonic_increasing ทรัพย์สิน
ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
import pandas as pd
การสร้างดัชนี -
index = pd.Index([10, 20, 20, 30, 40])
แสดงดัชนี -
print("Pandas Index...\n",index) ตรวจสอบว่าดัชนี monotonic เพิ่มขึ้นหรือไม่ -
print("\nIs the Pandas index monotonic increasing?\n",index.is_monotonic_increasing)
ตัวอย่าง
ต่อไปนี้เป็นรหัส -
import pandas as pd
# Creating the index
index = pd.Index([10, 20, 20, 30, 40])
# Display the index
print("Pandas Index...\n",index)
# Return an array representing the data in the Index
print("\nArray...\n",index.values)
# Check if the index monotonic increasing
print("\nIs the Pandas index monotonic increasing?\n",index.is_monotonic_increasing) ผลลัพธ์
สิ่งนี้จะสร้างรหัสต่อไปนี้ -
Pandas Index... Int64Index([10, 20, 20, 30, 40], dtype='int64') Array... [10 20 20 30 40] Is the Pandas index monotonic increasing? True