Python Pandas - 返回基礎資料的形狀元組


要返回基礎資料的形狀元組,請在 Pandas 中使用 **index.shape** 屬性。

首先,匯入必需的庫 −

import pandas as pd

建立索引 −

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

顯示索引 −

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

返回基礎資料的形狀元組 −

print("\nA tuple of the shape of underlying data...\n",index.shape)

示例

以下程式碼 −

import pandas as pd

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

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

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

# Return a tuple of the shape of the underlying data
print("\nA tuple of the shape of underlying data...\n",index.shape)

輸出

這將生成以下程式碼 −

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

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

A tuple of the shape of underlying data...
(5,)

更新日期: 13-10-2021

487 次瀏覽

啟動您的 職業生涯

完成課程進行認證

開始
廣告
© . All rights reserved.