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])
显示熊猫指数-
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
#使用一些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)
#返回索引中的元素数
print("\nNumber of elements in the index...\n",index.size)
#返回数据的dtype
print("\nThe dtype object...\n",index.dtype)
#检查带有一些NaN的索引值是否为浮点型
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