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

助您开启 职业生涯

完成课程,获得认证

立即开始
广告