Computer >> Máy Tính >  >> Lập trình >> Python

Python Pandas - Trả về Transpose của chỉ mục

Để trả về Transpose của chỉ mục, hãy sử dụng index.T tài sản.

Đầu tiên, hãy nhập các thư viện được yêu cầu -

import pandas as pd

Tạo chỉ mục -

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

Hiển thị chỉ mục -

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

Hiển thị chuyển vị của chỉ mục -

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

Ví dụ

Sau đây là mã -

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 which is by definition self...\n",index.T)

Đầu ra

Điều này sẽ tạo ra mã sau -

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')