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 Index...\n",index)

返回列表 −

print("\nList of the index values...\n",index.to_list())

示例

以下是代码 −

import pandas as pd

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

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

# Return the number of elements in the Index
print("\nNumber of elements in the index...\n",index.size)

# Return the dtype of the data
print("\nThe dtype object...\n",index.dtype)

# return a list
print("\nList of the index values...\n",index.to_list())

输出

这将产生以下输出 −

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

Number of elements in the index...
6

The dtype object...
float64

List of the index values...
[50.4, 10.2, 70.5, 110.5, 90.8, 50.6]

更新日期: 13-Oct-2021

2K+ 浏览次数

开启你的 职业

通过完成课程获取认证

开始
广告