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 次浏览

启动您的 职业生涯

完成课程进行认证

开始
广告