Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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 3.x 在Pandas系列中查找常量区域_Python 3.x_Pandas - Fatal编程技术网

Python 3.x 在Pandas系列中查找常量区域

Python 3.x 在Pandas系列中查找常量区域,python-3.x,pandas,Python 3.x,Pandas,假设我有下面的熊猫系列 s = pd.series([1,1,1,2,2,3,3,2,2]) i | val ---- 0 | 1 1 | 1 2 | 1 3 | 2 4 | 2 5 | 3 6 | 3 7 | 2 8 | 2 我想找到值相同的区域。所以结果可能是这样的: # list of [value, [range]] pairs [[1,[0,3]], [2,[3,5]], [3,[5,7]], [2,[7,9]]] 另一种类似的表示法也可以。让我们尝试使用diff和cumsum获

假设我有下面的熊猫系列

s = pd.series([1,1,1,2,2,3,3,2,2])

i | val
----
0 | 1
1 | 1
2 | 1
3 | 2
4 | 2
5 | 3
6 | 3
7 | 2
8 | 2
我想找到值相同的区域。所以结果可能是这样的:

# list of [value, [range]] pairs
[[1,[0,3]], [2,[3,5]], [3,[5,7]], [2,[7,9]]]

另一种类似的表示法也可以。

让我们尝试使用
diff
cumsum
获取
groupby
键,然后使用
agg

l = s.reset_index().groupby(s.diff().ne(0).cumsum()).agg({0:'first','index':lambda x : [x.min(),x.max()]}).values.tolist()
l
Out[35]: [[1, [0, 2]], [2, [3, 4]], [3, [5, 6]], [2, [7, 8]]]

前面的值似乎是组号,而不是组的值。为列表编制索引以获取组的值似乎很简单,但是否有一种优雅的方法来生成输出[1,[]]、[2,[]]、[3,[]]、[2,[]](即最后的4不是组号,而是组2的值)?