Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

pandas increment value on condition

In [47]: df['ans'] = (df['ix'] != df['ix'].shift(1)).cumsum()

In [48]: df
Out[48]: 
   index  ix  ans
0      1  pa    1
1      2  pa    1
2      3  pa    1
3      4  pe    2
4      5  fc    3
5      6  pb    4
6      7  pb    4
7      8  df    5
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #pandas #increment #condition
ADD COMMENT
Topic
Name
4+6 =