-1

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?

r_user
  • 317
  • 3
  • 11
  • Please NEVER post dataframes as images, only as text and ideally as Python code to create the df, as in my answer below. Other useful tips here: https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – piterbarg Feb 24 '22 at 23:48

1 Answers1

0

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

piterbarg
  • 8,089
  • 2
  • 6
  • 22