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

Python Pandas - ดูทรานส์โพสของดัชนี (index.T)

ในการดูทรานส์โพส (transpose) ของดัชนี (Index) ให้ใช้คุณสมบัติ index.T ใน Pandas (สำหรับดัชนี 1 มิติ ทรานส์โพสจะคืนค่าตัวมันเอง)

ขั้นตอนการทำงาน

1. นำเข้าไลบรารีที่จำเป็น

import pandas as pd

2. สร้างดัชนี

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

3. แสดงดัชนี

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

4. ดูทรานส์โพส

print("\nTranspose of the Pandas Index which is by definition self...\n", index.T)

ตัวอย่างโค้ดสมบูรณ์

import pandas as pd

# สร้างดัชนี
index = pd.Index(['Car', 'Bike', 'Truck', 'Ship', 'Airplane'])

# แสดงดัชนี
print("Pandas Index...\n", index)

# ดึงอาร์เรย์ข้อมูล
print("\nArray...\n", index.values)

# ดูทรานส์โพส
print("\nTranspose of the Pandas Index which is by definition self...\n", index.T)

ผลลัพธ์

Pandas Index... Index(['Car', 'Bike', 'Truck', 'Ship', 'Airplane'], dtype='object')
Array... ['Car' 'Bike' 'Truck' 'Ship' 'Airplane']
Transpose of the Pandas Index which is by definition self... Index(['Car', 'Bike', 'Truck', 'Ship', 'Airplane'], dtype='object')

คำอธิบาย

index.T สำหรับดัชนี 1 มิติ จะคืนค่าดัชนีตัวมันเอง (self) เนื่องจากทรานส์โพสของเวกเตอร์ 1 มิติคือตัวมันเอง