根据列的阈值选择pandas数据帧的行

时间:2017-11-03 17:22:39

标签: python pandas

我有一个pandas数据框,其中包含一列"值"和一列"时间戳"。现在我想根据时间戳的阈值过滤行。我做了以下事情:

idx = df.index[df['timestamp'] >= start and df['timestamp'] <= end]
df = df.loc[idx]

df是数据框,startend是两个整数。

不知何故,这不起作用。我收到了错误:

  

ValueError:DataFrame的真值是不明确的。使用a.empty,   a.bool(),a.item(),a.any()或a.all()。

编辑:还有一个问题。 start是一个只有一个值(一行,一列)的数据帧。 End是一个包含多行且只有一列的数据帧(但我只对最后一行感兴趣)。当我做以下

    print(end.iloc[-1])
    print(start.iloc[0])

我得到以下输出

1508504026077
start_timestamp_milli    1508502348946
Name: 0, dtype: int64

当我尝试print(df[column] >= start.iloc[0])时,我收到错误:

  

ValueError:只能比较带有相同标签的Series对象

因此,mask=(df['timestamp'] >= start & df['timestamp'] <= end)也会失败。

1 个答案:

答案 0 :(得分:1)

IIUC

LIKE