确定包含数组的Pandas列是否包含特定值

时间:2019-06-07 00:14:07

标签: python pandas numpy

我有一个包含三列的数据框:两列定义时间段的开始和结束(一个窗口),另一列定义单个时间点的数组。我想确定窗口的起点和终点(其他两列)中是否有任何单独的点。理想的输出是每一行都是True / False。

我可以遍历数据帧的每一行,提取时间点以及start_window和end_window时间并一次确定这一行,但是我一直在寻找更快的(无循环)选项。

数据框示例

row    start_window     end_window        times (numpy array)

0      307.110309       307.710309     [307.48857, 307.6031]
1      309.140340       311.900309     [315.23134]

...

基于上述数据框的输出为:

错误

3 个答案:

答案 0 :(得分:1)

一种方法是使用getTacni() { this.storageService.getQuestions().then(items => { const odgovori: { id: number; answer: number }[] = []; for (let i of items) { odgovori.push({ id: i.id, answer: i.tacan }); } }); }

pd.DataFrame.apply

输出:

df.apply(lambda x: any(x['start_window']< i< x['end_window'] for i in x['times']), 1)

答案 1 :(得分:0)

让我们来验证

s=pd.DataFrame(df.time.tolist(),index=df.index)
((df.start_window-s<0)&(df.end_window-s>0)).any(1)
Out[277]: 
0     True
1    False
dtype: bool

答案 2 :(得分:0)

这是另一个有效的解决方案。

t_max = df["times"].apply(max)
t_min = df["times"].apply(min)
out = (t_max > df["start_window"]) & (t_min < df["end_window"])
相关问题