Python pandas:如何更改两个不同类别之间的分类值

Python pandas: how to change categorical values in between 2 different categories

大家好,我正在研究股票市场数据,我想知道如何更改 pandas 数据框,如下所示

From: Signal To: Signal_new
Buy Buy
Buy Hold
Buy Hold
Sell Sell
Sell Hold
Sell Hold
Sell Hold
Buy Buy

提前致谢!

试试这个:

df[df['Signal'] == df['Signal'].shift(1)] = 'Hold'

输出:

>>> df
  Signal
0    Buy
1   Hold
2   Hold
3   Sell
4   Hold
5   Hold
6   Hold
7    Buy