Search code examples
pythonpandasdataframeminpandas-loc

Get the minimum for each data in the data frame


I have this data frame:

data = {name:  ['a', 'a','b', 'c', 'd', 'b', 'b', 'a', 'c'],
    number: [32, 25, 9 , 43,8, 5, 11, 21, 0]
    }

and I want to get min number for each name where data in the number column for that name is not 0. for my example, I want this result:

data = {'col1':  ['a', 'b', 'c', 'd'],
    'col2': [21, 5, 43, 8]
    }

I don't want the repetitive name.


Solution

  • IIUC, you can try:

    df = df.mask(df.number.eq(0)).dropna().groupby('name', as_index = False).min()
    

    OUTPUT:

      name  number
    0    a    21.0
    1    b     5.0
    2    c    43.0
    3    d     8.0