Python Pandas - 检查 Pandas 索引是否包含 Interval 对象
要检查Pandas索引是否包含Interval对象,请使用Pandas中的方法。index.is_interval()
首先,导入所需的库——
import pandas as pd
创建间隔对象-
interval1 = pd.Interval(10, 30) interval2 = pd.Interval(30, 50)
显示间隔-
print("Interval1...\n",interval1) print("Interval2...\n",interval2)
使用Intervalobject1和2创建Pandas索引-
index = pd.Index([interval1,interval2])
检查索引值是否只有区间对象-
print("\nDoes Index consists of Interval objects?\n", index.is_interval())
示例
以下是代码-
import pandas as pd #创建区间对象 interval1 = pd.Interval(10, 30) interval2 = pd.Interval(30, 50) #显示间隔 print("Interval1...\n",interval1) print("Interval2...\n",interval2) #使用Intervalobject1和2创建Pandas索引 index = pd.Index([interval1,interval2]) #显示Pandas索引 print("\nPandas Index...\n",index) #返回数据的dtype print("\nThe dtype object...\n",index.dtype) #检查索引值是否只有整数 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