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

Python Pandas - ผนวกชุดตัวเลือกดัชนีเข้าด้วยกัน


ในการผนวกคอลเลกชันของตัวเลือกดัชนีเข้าด้วยกัน ให้ใช้ append() วิธีการในแพนด้า ขั้นแรก นำเข้าไลบรารีที่จำเป็น -

import pandas as pd

การสร้างดัชนีนุ่น -

index1 = pd.Index([10, 20, 30, 40, 50])

แสดงดัชนีหมีแพนด้า −

print("Pandas Index...\n",index1)

สร้างดัชนีใหม่ที่จะผนวก -

index2 = pd.Index([60, 70, 80])

ต่อท้ายดัชนีใหม่ -

print("\nAfter appending...\n",index1.append(index2))

ตัวอย่าง

ต่อไปนี้เป็นรหัส -

import pandas as pd

# Creating Pandas index
index1 = pd.Index([10, 20, 30, 40, 50])

# Display the Pandas index
print("Pandas Index...\n",index1)

# Return the number of elements in the Index
print("\nNumber of elements in the index...\n",index1.size)

# create a new index to be appended
index2 = pd.Index([60, 70, 80])

# Append the new index
print("\nAfter appending...\n",index1.append(index2))

ผลลัพธ์

สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -

Pandas Index...
Int64Index([10, 20, 30, 40, 50], dtype='int64')

Number of elements in the index...
5

After appending...
Int64Index([10, 20, 30, 40, 50, 60, 70, 80], dtype='int64')