Python - 检查包含某些 NaN 的 Pandas 索引是否为浮点类型


要检查包含某些 NaN 的 Pandas 索引是否为浮点类型,请使用 **index.is_floating()** 方法。首先,导入所需的库 −

import pandas as pd
import numpy as np

创建包含某些 NaN 的 Pandas 索引 −

index = pd.Index([5.7, 6.8, 10.5, np.nan,17.8, 25.6, np.nan ,np.nan, 50.2])

显示 Pandas 索引 −

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

检查包含某些 NaN 的索引值是否为浮点类型 −

print("\nIndex values with some NaNs is a floating type?\n",index.is_floating())

示例

以下为代码 −

import pandas as pd
import numpy as np

# Creating Pandas index with some NaNs
index = pd.Index([5.7, 6.8, 10.5, np.nan,17.8, 25.6, np.nan ,np.nan, 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 with some NaNs are floating type
print("\nIndex values with some NaNs is a floating type?\n",index.is_floating())

输出

这将生成以下输出 −

Pandas Index...
Float64Index([5.7, 6.8, 10.5, nan, 17.8, 25.6, nan, nan, 50.2], dtype='float64')

Number of elements in the index...
9

The dtype object...
float64

Index values with some NaNs is a floating type?
True

更新日期:2021 年 10 月 13 日

103 次浏览

开启您的职业生涯

完成课程即可获得认证

开始
广告