0

I have this dataframe in Pandas

date value
2023-01 0
2023-02 0
2023-03 -1
2023-04 -1
2023-05 0
2023-06 0
2023-07 -1
2023-08 -1

I would like to transform it to:

date value
2023-01 0
2023-03 -1
2023-05 0
2023-07 -1
Ken White
  • 123,280
  • 14
  • 225
  • 444
p.magalhaes
  • 7,595
  • 10
  • 53
  • 108
  • 1
    what happens if more than 2 of same value. remove all except first or remove only last? if keep only first maybe `df[df['value'] != df['value'].shift(1)]` – cmgchess Aug 31 '23 at 04:32

1 Answers1

1

Use boolean indexing with shift:

out = df[df['value'].ne(df['value'].shift())]

Output:

      date  value
0  2023-01      0
2  2023-03     -1
4  2023-05      0
6  2023-07     -1

Intermediates:

      date  value  value_shift     ne
0  2023-01      0          NaN   True
1  2023-02      0          0.0  False
2  2023-03     -1          0.0   True
3  2023-04     -1         -1.0  False
4  2023-05      0         -1.0   True
5  2023-06      0          0.0  False
6  2023-07     -1          0.0   True
7  2023-08     -1         -1.0  False
mozway
  • 194,879
  • 13
  • 39
  • 75