Search code examples
pythonpandasfilterconditional-statementsminimum

Filtering in pandas by condition


I have a dataframe sample looks like below. first row is header.

a  b   c   d   e
x  1   10  4   asd
y  3   12  5   aqe
z  4   14  6   rty
t  6   12  4   abd
v  7   4   8   yul

I would like the find a column members by filtering d column by its minimum value. I tried the sort it and select the 0. element but column may have 2 minimum values like in the sample.

Output should be "x" and "t"

Any suggestions?


Solution

  • Use boolean indexing with loc for select column a and last convert to list:

    L = df.loc[df['d'] == df['d'].min(), 'a'].tolist()
    print (L)
    ['x', 't']
    

    Detail:

    print (df['d'] == df['d'].min())
    0     True
    1    False
    2    False
    3     True
    4    False
    Name: d, dtype: bool