Python Pandas - 检查 Pandas 索引是否包含 Interval 对象
若要检查 Pandas 索引是否包含 Interval 对象,请在 Pandas 中使用 index.is_interval() 方法。
首先,导入所需的库 -
import pandas as pd
创建 Interval 对象 -
interval1 = pd.Interval(10, 30) interval2 = pd.Interval(30, 50)
显示间隔 -
print("Interval1...\n",interval1) print("Interval2...\n",interval2)
使用 Interval 对象 1 和 2 创建 Pandas 索引 -
index = pd.Index([interval1,interval2])
检查索引值是否仅包含 Interval 对象 -
print("\nDoes Index consists of Interval objects?\n", index.is_interval())
示例
以下是代码 -
import pandas as pd # create Interval objects interval1 = pd.Interval(10, 30) interval2 = pd.Interval(30, 50) # display the intervals print("Interval1...\n",interval1) print("Interval2...\n",interval2) # Creating Pandas index with Interval object1 and 2 index = pd.Index([interval1,interval2]) # Display the Pandas index print("\nPandas Index...\n",index) # Return the dtype of the data print("\nThe dtype object...\n",index.dtype) # check whether index values has only ints print("\nDoes Index consists of Interval objects?\n", index.is_interval())
输出
将生成以下输出 -
Interval1... (10, 30] Interval2... (30, 50] Pandas Index... IntervalIndex([(10, 30], (30, 50]], dtype='interval[int64, right]') The dtype object... interval[int64, right] Does Index consists of Interval objects? True
广告