'pandas select row if value in another column changs

Let's say I have a large data set that follows a similar structure:

enter image description here

where the id repeats multiple times. I would like to select any id where the value in column b changed with the desired output as such:

enter image description here

How might I be able to achieve that via pandas?



Solution 1:[1]

It is not entirely clear what you are asking for. You say

I would like to select any id where the value in column b changed

but 'changed' from what?

Perhaps the following can be helpful -- it will show you all unique ColumnB strings for each id

Using a sample df:

df = pd.DataFrame({'id':[1,1,1,2,2,2,2], 'colb':['a','a','b','c','d','c','c']})

we use groupby and unique:

df.groupby('id')['colb'].unique().explode().to_frame()

output:


    colb
id  
1   a
1   b
2   c
2   d

so for id=1 we have a and b as unique phrases, and for id=2 we have c and d

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 piterbarg