Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从数据帧中提取范围开始和范围结束记录_Python_Pandas - Fatal编程技术网

Python 从数据帧中提取范围开始和范围结束记录

Python 从数据帧中提取范围开始和范围结束记录,python,pandas,Python,Pandas,我想计算Value在范围(41-46)内的时间段,并在下面的df中保持相同的值值仅应在发生更改时更新,否则保持不变 Id Timestamp Value 34213951 34214809 2012-05-01 08:33:47.127 41.5 34214252 34215110 2012-05-01 08:39:06.270 41.5 34214423 34215281

我想计算
Value
在范围(41-46)内的时间段,并在下面的
df
中保持相同的值<代码>值仅应在发生更改时更新,否则保持不变

            Id          Timestamp               Value
34213951    34214809    2012-05-01 08:33:47.127 41.5    
34214252    34215110    2012-05-01 08:39:06.270 41.5    
34214423    34215281    2012-05-01 08:41:56.240 40.5
34214602    34215460    2012-05-01 08:44:55.777 39.5
34214873    34215731    2012-05-01 08:49:25.600 38.5
34215071    34215929    2012-05-01 08:53:04.593 37.5
34215342    34216200    2012-05-01 08:56:47.257 36.5
34216007    34216865    2012-05-01 09:07:24.370 34.5
34216443    34217301    2012-05-01 09:14:46.120 33.5
34216884    34217742    2012-05-01 09:22:51.907 32.5
34217190    34218048    2012-05-01 09:29:00.023 31.5
34217803    34218661    2012-05-01 09:40:08.483 30.5
34218381    34219239    2012-05-01 09:50:20.440 30.5
34218382    34219240    2012-05-01 09:50:22.317 32.5
34218388    34219246    2012-05-01 09:50:26.067 37.5
34218389    34219247    2012-05-01 09:50:27.940 39.0
34218392    34219250    2012-05-01 09:50:29.817 39.5
34218393    34219251    2012-05-01 09:50:31.690 40.5
34218396    34219254    2012-05-01 09:50:35.440 41.0
34218789    34219647    2012-05-01 09:56:55.327 41.0
34218990    34219848    2012-05-01 10:00:07.847 40.0
与:

预期产出:

    StartTime               EndTime                     StartValue  EndValue

0   2012-05-01 08:33:47.127 2012-05-01 08:41:56.240     41.5        40.5
1   2012-05-01 09:50:35.440 2012-05-01 10:00:07.847     41.0        40.0


我本以为
EndTime
总是在
StartTime
之后,但事实并非如此。我遗漏了什么吗?

这里有一种矢量化的方法。主要使用
shift
比较相邻行

df["in_range"] = (df.Value >= 41) & (df.Value <= 46)
df["end_of_range"] = df.in_range.shift() & ~df.in_range
df["start_of_range"] = ~df.in_range.shift(1).fillna(False) & df.in_range
我现在创建两个数据帧-一个用于所有“范围开始”记录,另一个用于所有“范围结束”记录:

starts = df[df.start_of_range][["Timestamp", "Value"]]
ends = df[df.end_of_range][["Timestamp", "Value"]]

# reset the index of these two dataframe, so I can easility concat them later. 
starts.index = range(len(starts))
ends.index = range(len(starts))
“开始”和“结束”的值现在为:

                 Timestamp  Value
0  2012-05-01 08:33:47.127   41.5
1  2012-05-01 09:50:35.440   41.0
                 Timestamp  Value
0  2012-05-01 08:41:56.240   40.5
1  2012-05-01 10:00:07.847   40.0
现在只剩下
concat
两个新创建的数据帧,这样每个开始记录都与其对应的结束记录对齐

res = pd.concat([starts, ends], axis=1)
res.columns = ["StartTime", "EndTime", "StartValue", "EndValue"]
结果是:

                 StartTime  EndTime               StartValue  EndValue
0  2012-05-01 08:33:47.127     41.5  2012-05-01 08:41:56.240      40.5
1  2012-05-01 09:50:35.440     41.0  2012-05-01 10:00:07.847      40.0

我不确定我是否明白你想做什么。“计算值在范围(130-180)内并保持相同值的时间段”是什么意思。此外,在此数据帧上运行代码不会产生此输出-它会产生一个空数据帧。我想了解
值在什么时候达到130-180范围,以及它保持相同值的时间。例如,
值在
2012-05-01 08:33:47
时达到130(从129),在
2012-05-01 08:35:47
时变为
131
,则时间段为20分钟<代码>值
仅在发生更改时更新。原始数据集要大得多,这只是一个片段,很抱歉造成混淆。请包含一个数据片段,以便重现您的问题,或提出替代解决方案?@Roy2012请查看最后编辑的问题。前一种观点并没有反映出这个问题。我将范围更改为(41-46),输出也更改了。非常感谢。谢谢最后一个问题-您能否添加此特定数据集的预期输出?请告诉我这是否回答了您的问题。我是否可以知道此解决方案是否考虑了保持相同值的要求?也就是说,end_值不仅必须在41-46范围内,而且必须是与当前值(即,end_范围的start_)不相同的下一个值,因为我们假设该值仅在发生更改时更新。是的,它已经更新。您看到的值(例如40.5)是范围结束后立即记录的值。请您向我指出代码的哪一部分负责相同的值条件?当然。请参阅我对答案的最新编辑。它包含一个逐步的解释。
                 Timestamp  Value
0  2012-05-01 08:33:47.127   41.5
1  2012-05-01 09:50:35.440   41.0
                 Timestamp  Value
0  2012-05-01 08:41:56.240   40.5
1  2012-05-01 10:00:07.847   40.0
res = pd.concat([starts, ends], axis=1)
res.columns = ["StartTime", "EndTime", "StartValue", "EndValue"]
                 StartTime  EndTime               StartValue  EndValue
0  2012-05-01 08:33:47.127     41.5  2012-05-01 08:41:56.240      40.5
1  2012-05-01 09:50:35.440     41.0  2012-05-01 10:00:07.847      40.0