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

Python - ส่งคืนอาร์เรย์ที่แสดงข้อมูลใน Pandas Index


ในการส่งคืนอาร์เรย์ที่แสดงข้อมูลในดัชนี Pandas ให้ใช้ index.values ทรัพย์สินในหมีแพนด้า

ขั้นแรก นำเข้าไลบรารีที่จำเป็น -

import pandas as pd

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

index = pd.Index(['Car','Bike','Truck','Ship','Airplane'])

แสดงดัชนี -

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

ส่งกลับอาร์เรย์ที่แสดงถึงข้อมูลในดัชนี -

print("\nArray...\n",index.values)

ตัวอย่าง

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

import pandas as pd

# Creating the index
index = pd.Index(['Car','Bike','Truck','Ship','Airplane'])

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

# Return an array representing the data in the Index
print("\nArray...\n",index.values)

# Display the transpose of the index
print("\nTranspose of the Pandas Index...\n",index.T)

ผลลัพธ์

สิ่งนี้จะสร้างรหัสต่อไปนี้ -

Pandas Index...
Index(['Car', 'Bike', 'Truck', 'Ship', 'Airplane'], dtype='object')

Array...
['Car' 'Bike' 'Truck' 'Ship' 'Airplane']

Transpose of the Pandas Index...
Index(['Car', 'Bike', 'Truck', 'Ship', 'Airplane'], dtype='object')