简体   繁体   中英

pandas select row if value in another column changs

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

在此处输入图像描述

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:

在此处输入图像描述

How might I be able to achieve that via pandas?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM