Python——返回表示 Pandas 索引中資料的陣列


若要返回表示 Pandas 索引中資料的陣列,請在 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')

更新時間:2021 年 10 月 13 日

352 次瀏覽

開啟你的 職業

透過完成課程獲得認證

立即開始
廣告
© . All rights reserved.