Python Pandas-返回索引值列表

Python Pandas-返回索引值列表

要返回索引值列表,请在Pandas中使用index.to_list()方法。首先,导入所需的库 –

import pandas as pd

创建Pandas索引 −

index = pd.Index([50.4, 10.2, 70.5, 110.5, 90.8, 50.6])

显示Pandas索引 −

print("Pandas索引...\n",index)

返回列表 −

print("\n索引值列表...\n",index.to_list())

示例

以下是代码 –

import pandas as pd

# 创建Pandas索引
index = pd.Index([50.4, 10.2, 70.5, 110.5, 90.8, 50.6])

# 显示Pandas索引
print("Pandas索引...\n",index)

# 返回索引中元素的数量
print("\n索引中元素的数量...\n",index.size)

# 返回数据的dtype
print("\n数据dtype对象...\n",index.dtype)

# 返回列表
print("\n索引值列表...\n",index.to_list())

输出

这将产生以下输出 –

Pandas Index...
Float64Index([50.4, 10.2, 70.5, 110.5, 90.8, 50.6], dtype='float64')

索引中元素的数量...
6

数据dtype对象...
float64

索引值列表...
[50.4, 10.2, 70.5, 110.5, 90.8, 50.6]

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程