I have a dataframe that has a column like this:
x
0 1
1 1
2 0
3 1
4 0
5 0
6 0
7 1
8 1
9 1
I'd like to add a column that counts up every time x
changes so that my final result looks like this:
x y
0 1 0
1 1 0
2 0 1
3 1 2
4 0 3
5 0 3
6 0 3
7 1 4
8 1 4
9 1 4
I can't figure out the fastest way to do this without looping. I also don't care if y
starts at 0 or 1. I'm sure there's something innate to pandas I can use. Can you help?
PS. the reason I need to make this y
column is do be able to group the rows by each number, if there's a way to essentially accomplish the same thing without creating it, that would work too.
After diff
you can apply cumsum
df.x.diff().ne(0).cumsum()-1
Out[132]:
0 0
1 0
2 1
3 2
4 3
5 3
6 3
7 4
8 4
9 4
Name: x, dtype: int32
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With