分组数据框并检查每组中唯一值的数量

时间:2020-02-25 15:55:12

标签: python pandas group-by

我尝试过

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

输出如下:

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

如您所见,SLOTID在表中只有一个值,但是表足够长,我想知道(Machine,COMPONENT_ID)的任何组是否具有多个SLOTID值。

1 个答案:

答案 0 :(得分:1)

您可以只对所需的列进行分组,并在任意条件下对目标列进行转换。用它来切片原始DF将返回您想要的。

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