如何在IntervalIndex中获得一些值?

时间:2017-07-06 00:58:36

标签: python pandas

变量'lvl'是IntervalIndex类型,如下所示。

lvl = IntervalIndex([(1.418, 1.69], (1.69, 6.696], (6.696, 7.217], (7.217, 7.845], (7.845, 10.11] ... (17.648, 18.199], (18.199, 19.315], (19.315, 20.16], (20.16, 22.471], (22.471, 25.009]]
          closed='right',
          dtype='interval[float64]') 

现在我尝试添加一些像这样的索引值:

for each in val_list:  
    for k in range(len(lvl)):
        lower_val = float(lvl[k][1:-1].split(',')[0])
        upper_val = float(lvl[k][1:-1].split(',')[-1])
        if each >=lower_val and each <= upper_val:
            some_var_index.append(k)

但是,错误引起了我的注意......:

{TypeError}'pandas._libs.interval.Interval' object is not subscriptable 

我是怎么做到的? 谢谢

1 个答案:

答案 0 :(得分:5)

对象不可订阅意味着它的行为不像普通列表,但你在这里做了两次:

  lineChartData: Array<any>;

  constructor(private _httpService: HttpService, private _http: Http) {
     this._httpService.getParams("myURL").subscribe(S => {
       this.stat1 = S;
       this.lineChartData = [
          { data: [this.stat1[0].data, this.stat2[0].data], label: 'Data 1' },
          { data: [this.stat1[1].data, this.stat2[1].data], label: 'Data 2' },
          { data: [this.stat1[2].data, this.stat2[2].data], label: 'Data 3' }
       ];
    });

第二个方括号lower_val = float(lvl[k][1:-1].split(',')[0]) upper_val = float(lvl[k][1:-1].split(',')[-1]) 。相反,这两行使用这一行:

[1:-1]

你应该好。