Python Pandas IntervalIndex - 检查缺失值的区间是否为空
要检查缺失值的区间是否为空,请使用IntervalIndex.is_empty属性。首先,导入所需的库-
import pandas as pd import numpy as np
使用NaN值创建IntervalIndex-
interval = pd.IntervalIndex.from_arrays([np.nan, np.nan], [np.nan, np.nan])
显示间隔-
print("IntervalIndex...\n",interval)
检查包含缺失值的区间是否为空-
print("\nIs the interval empty?\n",interval.is_empty)
示例
以下是代码-
import pandas as pd import numpy as np #使用NaN值创建IntervalIndex interval = pd.IntervalIndex.from_arrays([np.nan, np.nan], [np.nan, np.nan]) #显示间隔 print("IntervalIndex...\n",interval) #显示间隔 length print("\nIntervalIndex length...\n",interval.length) #检查包含缺失值的区间是否为空 print("\nIs the interval empty?\n",interval.is_empty)输出结果
这将产生以下输出-
IntervalIndex... IntervalIndex([nan, nan], dtype='interval[float64, right]') IntervalIndex length... Float64Index([nan, nan], dtype='float64') Is the interval empty? [False False]