擅长:python、mysql、java
<p>首先通过<code>shift</code>和<code>cumsum</code>创建唯一组,然后获取第一个重复项的掩码并通过<a href="http://pandas.pydata.org/pandas-docs/stable/indexing.html#boolean-indexing" rel="nofollow noreferrer">^{<cd3>}</a>进行筛选:</p>
<pre><code>s = pd.Series([2,3,7,10,11,16,16,9,11,12,14,16,16,16,5,7,9,17,17,4,8,18,18])
s1 = s.shift(1).ne(s).cumsum()
m = ~s1.duplicated() & s1.duplicated(keep=False)
s2 = m.index[m].tolist()
print (s2)
[5, 11, 17, 21]
</code></pre>
<hr/>
<pre><code>print (s1)
0 1
1 2
2 3
3 4
4 5
5 6
6 6
7 7
8 8
9 9
10 10
11 11
12 11
13 11
14 12
15 13
16 14
17 15
18 15
19 16
20 17
21 18
22 18
dtype: int32
</code></pre>
<hr/>
<pre><code>print (m)
dtype: int32
0 False
1 False
2 False
3 False
4 False
5 True
6 False
7 False
8 False
9 False
10 False
11 True
12 False
13 False
14 False
15 False
16 False
17 True
18 False
19 False
20 False
21 True
22 False
dtype: bool
</code></pre>