pandas increment value on condition

Solutions on MaxInterview for pandas increment value on condition by the best coders in the world

we are a community of more than 2 million smartest coders
registration for
employee referral programs
are now open
get referred to google, amazon, flipkart and more
register now
  
showing results for - "pandas increment value on condition"
Mats
03 Apr 2016
1In [47]: df['ans'] = (df['ix'] != df['ix'].shift(1)).cumsum()
2
3In [48]: df
4Out[48]: 
5   index  ix  ans
60      1  pa    1
71      2  pa    1
82      3  pa    1
93      4  pe    2
104      5  fc    3
115      6  pb    4
126      7  pb    4
137      8  df    5
14