Search code examples
pythonpandasdata-analysis

how to list col1 with the multiple values in col2


I need to to know

  • are there multiple Product-ID for the same barcode number
  • are there multiple barcodes number for the same Product-ID

example of the dataset:

data = {'Proudct':['SLL', 'MNO', 'Drin', 'JAJ'],
        'Product-ID':[20, 20, 19, 18]
         'barcode':['3633', '7676', '2313','3311'}
 df = pd.DataFrame(data) ``` 


I need the output to be like:

Name   ID   Code
SLL    20    3633
MNO    20    7676

Solution

  • Use groupby + transform('count'):

    filtered = df[df.groupby('Product-ID')['barcode'].transform('count') > 1]
    

    Output:

    >>> filtered
      Proudct  Product-ID barcode
    0     SLL          20    3633
    1     MNO          20    7676