Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

Python Pandas - ตรวจสอบว่าสอง Index objects ที่เรียงลำดับตรงข้ามเท่ากันหรือไม่

เพื่อตรวจสอบว่าสอง Index objects ที่มีลำดับตรงข้ามกันเท่ากันหรือไม่ ให้ใช้เมธอด equals()

เริ่มต้นด้วยการนำเข้าไลบรารีที่จำเป็น:

import pandas as pd

สร้าง Pandas index1 และ index2:

index1 = pd.Index([15, 25, 35, 45, 55, 65, 75, 85, 95])
index2 = pd.Index([95, 85, 75, 65, 55, 45, 35, 25, 15])

แสดง index1 และ index2:

print("Pandas Index1...\n", index1)
print("Pandas Index2...\n", index2)

ตรวจสอบว่าสอง Index objects ที่มีลำดับตรงข้ามกันเท่ากันหรือไม่:

print("\nAre two Index objects with opposite order equal?\n", index1.equals(index2))

ตัวอย่างโค้ดเต็ม

import pandas as pd

# Creating Pandas index1 and index2
index1 = pd.Index([15, 25, 35, 45, 55, 65, 75, 85, 95])
index2 = pd.Index([95, 85, 75, 65, 55, 45, 35, 25, 15])

# Display the index1 and index2
print("Pandas Index1...\n", index1)
print("Pandas Index2...\n", index2)

print("\nAre two Index objects with opposite order equal?\n", index1.equals(index2))

เอาต์พุต

จะได้ผลลัพธ์ดังนี้:

Pandas Index1... Int64Index([15, 25, 35, 45, 55, 65, 75, 85, 95], dtype='int64')
Pandas Index2... Int64Index([95, 85, 75, 65, 55, 45, 35, 25, 15], dtype='int64')
Are two Index objects with opposite order equal? False