想要的效果是这样的:
In [3]: a = pandas.Series([1,2,2,3,2], index=[1,2,3,4,5])
In [4]: a.something()
Out[4]:
1 1
2 2
4 3
5 2
dtype: int64
解决方案很简单,如下所示:
In [5]:a.loc[a.shift(1) != a]
Out[5]:
1 1
2 2
4 3
5 2
dtype: int64
同理,如果想要保留最后一个的话,直接把shift函数里的数值改为-1就可以
In [6]:a.loc[a.shift(-1) != a]
Out[6]:
1 1
3 2
4 3
5 2
dtype: int64