Python - 檢查 Pandas 索引是否為浮點型別


要檢查 Pandas 索引是否為浮點型別,請在 Pandas 中使用 index.is_floating() 方法。首先,匯入所需的程式碼庫 -

import pandas as pd

建立 Pandas 索引 −

index = pd.Index([5.7, 6.8, 10.5, 20.4, 25.6, 30.8, 40.5, 50.2])

顯示 Pandas 索引 −

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

檢查索引值是否僅包含浮點數 −

print("\nIndex values only consists of floats?\n",index.is_floating())

示例

以下是程式碼 −

import pandas as pd

# Creating Pandas index
index = pd.Index([5.7, 6.8, 10.5, 20.4, 25.6, 30.8, 40.5, 50.2])

# 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)

# check whether index values has only floats
print("\nIndex values only consists of floats?\n",index.is_floating())

輸出

這將產生以下輸出 −

Pandas Index...
Float64Index([5.7, 6.8, 10.5, 20.4, 25.6, 30.8, 40.5, 50.2], dtype='float64')

Number of elements in the index...
8

The dtype object...
float64

Index values only consists of floats?
True

更新於:2021 年 10 月 13 日

657 次瀏覽

助您開啟 職業生涯

完成課程,獲得認證

立即開始
廣告