HegChin
HegChin

Reputation: 51

Group the dataframe and check number of unique values in each group

I have tried

df.groupby(['Machine','SLOTID'])['COMPONENT_ID'].unique()

The output looks like:

Machine  COMPONENT_ID          
LM5      11S02CY382YH1934472901    [N3CP1.CP]
     11S02CY382YH1934620648        [N4CP0.CP]
     11S02CY388YH1934546857        [N2CP0.CP]
     11S02CY388YH1934590637        [N0CP0.CP]
     11S02CY388YH1934591337        [N4CP0.CP]
                                  ...    
M05      11S02CY395YH1934575728    [N5CP1.CP]
     11S02CY395YH1934658824        [N3CP1.CP]
     11S02CY395YH1934662750        [N1CP1.CP]
     11S02CY395YH1934703310        [N5CP1.CP]
     11S02CY395YH1934801982        [N5CP1.CP]
Name: SLOTID, Length: 388, dtype: object

As you can see , the SLOTID has only one value in table, but the table is long enough and I want to know whether any group of (Machine,COMPONENT_ID) has more than one value of SLOTID.

Upvotes: 0

Views: 35

Answers (1)

Chris
Chris

Reputation: 16147

You can just do the groupby on the desired columns, use transform on the target column with whatever condition you want. Using this to slice the original DF will return what you want.

df[df.groupby(['Machine','COMPONENT_ID'])['SLOTID'].transform('nunique')>1] 

Upvotes: 1

Related Questions