Get index where value changes in pandas dataframe column Get index where value changes in pandas dataframe column pandas pandas

Get index where value changes in pandas dataframe column


Use

In [91]: df.ne(df.shift()).apply(lambda x: x.index[x].tolist())Out[91]:Group 1             [0, 4]Group 2       [0, 2, 4, 6]Product ID       [0, 3, 6]dtype: objectIn [92]: df.ne(df.shift()).filter(like='Group').apply(lambda x: x.index[x].tolist())Out[92]:Group 1          [0, 4]Group 2    [0, 2, 4, 6]dtype: object

Also for dict,

In [107]: {k: s.index[s].tolist() for k, s in df.ne(df.shift()).filter(like='Group').items()}Out[107]: {'Group 1': [0L, 4L], 'Group 2': [0L, 2L, 4L, 6L]}


This is one non-pandas solution. I like it because it is intuitive and requires no understanding of the large pandas library.

changes = {}for col in df.columns:    changes[col] = [0] + [idx for idx, (i, j) in enumerate(zip(df[col], df[col][1:]), 1) if i != j]# {'Group 1': [0, 4], 'Group 2': [0, 2, 4, 6], 'Product ID': [0, 3, 6]}