1

I need to create a new series on my pandas DataFrame to count the number of switch of another value. Here an example:

enter image description here

I need a function to calculate the column new_counter. The DataFrame is sorted by foo and date. The counter increases when the variable foo or the variable baz change.

paolof89
  • 1,319
  • 5
  • 17
  • 31

1 Answers1

3

Create helper Series for consecutive counter and then pass it to groupby with GroupBy.ngroup:

s = df['baz'].ne(df['baz'].shift()).cumsum()
df['counter'] = df.groupby(['foo', s]).ngroup() + 1
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • 1
    Thank you it works. Can you explain the ne() function? – paolof89 Dec 13 '18 at 12:50
  • 1
    @paolof89 - It is function [`ne`](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.ne.html) like `!=`, better is see it in this answer [link](https://stackoverflow.com/a/53542712/2901002) how it working. – jezrael Dec 13 '18 at 12:59