简体   繁体   中英

Find matching rows based on a conditional grouping in a pandas dataframe

I've look everywhere for this answer but none seem to do what I need. Here's a dummy example of what I need:

data = {'id':[1, 2, 3, 4, 1, 1, 3, 4, 1], 
        'parent':['a', 'b', 'f', 'j', 'a', 'n', 'f', 'z', 'x'], 
        'vehicle':['car', 'car', 'truck', 'suv', 'car', 'hatch', 'truck', 'suv', 'car'], 
        'color':['red', 'blue', 'grey', 'green', 'red', 'purple', 'grey', 'green', 'red'],
        'serial': [324234, 23464, 5667, 1245, 786, 34546, 8537, 111111, 8376251537]}
df = pd.DataFrame(data)
df.sort_values(by=['id', 'parent'], inplace=True)

    id  parent  vehicle   color   serial
0   1   a        car      red     324234
4   1   a        car      red     786
5   1   n        hatch    purple  34546
8   1   x        car      red     8376251537
1   2   b        car      blue    23464
2   3   f        truck    grey    5667
6   3   f        truck    grey    8537
3   4   j        suv      green   1245
7   4   z        suv      green   111111

And what I need is to get all rows where the id is the same but the parent differs and the vehicle and color are the same.


So I want:

    id  parent  vehicle color   serial
0   1   a       car     red     324234
4   1   a       car     red     786
8   1   x       car     red     8376251537
3   4   j       suv     green   1245
7   4   z       suv     green   111111

Note that I want to include the top two of the above because they have a different serial number. Edit: and they are part of a grouping that has differing parent w/ same id.


I've tried this and get close:

target = df[df.duplicated(['id', 'vehicle', 'color'], keep=False)]

    id  parent  vehicle   color   serial
0   1   a       car       red     324234
4   1   a       car       red     786
8   1   x       car       red     8376251537
2   3   f       truck     grey    5667
6   3   f       truck     grey    8537
3   4   j       suv       green   1245
7   4   z       suv       green   111111

But I don't want the rows that have matching id, vehicle, color i f the corresponding parent is also the same . So in this case, I don't want

    id  parent  vehicle   color   serial
2   3   f       truck     grey    5667
6   3   f       truck     grey    8537

because they have the same parent. I've thought about grouping and changing the index but what I'm doing isn't working. This seems like an easy problem and maybe it is, but I just cant's crack it!

IIUC, Let's try this:

df[df.groupby(['id','vehicle','color'])['parent'].transform('nunique') > 1]

Output:

   id parent vehicle  color      serial
0   1      a     car    red      324234
4   1      a     car    red         786
8   1      x     car    red  8376251537
3   4      j     suv  green        1245
7   4      z     suv  green      111111

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