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+ 瀏覽次數

開啟你的 職業

透過完成課程獲取認證

開始
廣告
© . All rights reserved.