Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.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_Algorithm_Pandas - Fatal编程技术网

python数据帧,获取事件的开始和结束列表

python数据帧,获取事件的开始和结束列表,python,algorithm,pandas,Python,Algorithm,Pandas,我有一个数据帧和一个整数值列(在我的例子中是0和1)。索引是时间。我需要一个列表,当“地区”的开始和结束。我可以用diff和loop来实现 例如: import pandas as pd df = pd.DataFrame(index = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) df['test'] = pd.DataFrame([0, 1, 1, 1, 0, 0, 1, 1, 1, 0], index = df.index) methodOfLooking = ((

我有一个数据帧和一个整数值列(在我的例子中是0和1)。索引是时间。我需要一个列表,当“地区”的开始和结束。我可以用diff和loop来实现

例如:

import pandas as pd
df = pd.DataFrame(index = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
df['test'] = pd.DataFrame([0, 1, 1, 1, 0, 0, 1, 1, 1, 0], index = df.index)

methodOfLooking = ((2,4),(7,9)) # something like this should be the result

有没有获得结果的有效方法?

您可以使用
diff
zip
获取开始和结束索引:

ix = df.test.diff().fillna(0)

In [74]: zip(df.index[ix==1],df.index[ix==-1]-1)
Out[74]: [(2, 4), (7, 9)]