Compare 2 Consecutive Rows And Assign Increasing Value If Different (using Pandas)
I have a dataframe df_in like so: import pandas as pd dic_in = {'A':['aa','aa','bb','cc','cc','cc','cc','dd','dd','dd','ee'], 'B':['200','200','200','400','400','500','700',
Solution 1:
Use shift
and any
to compare consecutive rows, using True
to indicate where the value should change. Then take the cumulative sum with cumsum
to get the increasing value:
df_in['value'] = (df_in[['A', 'B']] != df_in[['A', 'B']].shift()).any(axis=1)
df_in['value'] = df_in['value'].cumsum()
Alternatively, condensing it to one line:
df_in['value'] = (df_in[['A', 'B']] != df_in[['A', 'B']].shift()).any(axis=1).cumsum()
The resulting output:
AB C value
0 aa 200 da 11 aa 200 cs 12 bb 200 fr 23 cc 400 fs 34 cc 400 se 35 cc 500 at 46 cc 700 yu 57dd700 j5 68dd9003179dd900 ds 710 ee 200 sz 8
Post a Comment for "Compare 2 Consecutive Rows And Assign Increasing Value If Different (using Pandas)"