Để trả về một mảng đại diện cho dữ liệu trong Chỉ mục Pandas, hãy sử dụng index.values tài sản ở Pandas.
Đầ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) Trả về một mảng đại diện cho dữ liệu trong Chỉ mục -
print("\nArray...\n",index.values)
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...\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... Index(['Car', 'Bike', 'Truck', 'Ship', 'Airplane'], dtype='object')