擅长:python、mysql、java
<p>你可以用<code>shift</code></p>
<pre><code>In [3815]: s = pd.Series(series)
In [3816]: cond = (s == s.shift(-1))
In [3817]: cond.index[cond]
Out[3817]: Int64Index([5, 11, 12, 17, 21], dtype='int64')
</code></pre>
<p>或者,<code>diff</code></p>
<pre><code>In [3828]: cond = s.diff(-1).eq(0)
In [3829]: cond.index[cond]
Out[3829]: Int64Index([5, 11, 12, 17, 21], dtype='int64')
</code></pre>
<p>对于列表输出,使用<code>tolist</code></p>
<pre><code>In [3833]: cond.index[cond].tolist()
Out[3833]: [5, 11, 12, 17, 21]
</code></pre>
<hr/>
<p>详细信息</p>
<pre><code>In [3823]: s.head(10)
Out[3823]:
0 2
1 3
2 7
3 10
4 11
5 16
6 16
7 9
8 11
9 12
dtype: int64
In [3824]: cond.head(10)
Out[3824]:
0 False
1 False
2 False
3 False
4 False
5 True
6 False
7 False
8 False
9 False
dtype: bool
</code></pre>