gaurav gurnani
gaurav gurnani

Reputation: 2859

Conditional selection of data in a pandas DataFrame

I have two columns in my pandas DataFrame.

   A      B
0  1      5
1  2      3
2  3      2
3  4      0
4  5      1

I need the value in A where the value of B is minimum. In the above case my answer would be 4 since the minimum B value is 0.

Can anyone help me with it?

Upvotes: 2

Views: 3264

Answers (2)

Alex Riley
Alex Riley

Reputation: 176750

To find the minimum in column B, you can use df.B.min(). For your DataFrame this returns 0.

To find values at particular locations in a DataFrame, you can use loc:

>>> df.loc[(df.B == df.B.min()), 'A']
3    4
Name: A, dtype: int64

So here, loc picks out all of the rows where column B is equal to its minimum value (df.B == df.B.min()) and selects the corresponding values in column A.

This method returns all values in A corresponding to the minimum value in B. If you only need to find one of the values, the better way is to use idxmin as @aus_lacy suggests.

Upvotes: 5

alacy
alacy

Reputation: 5064

Here's one way:

b_min = df.B.idxmin()
a_val = df.A[b_min]

idxmin() returns the index of the minimum value within column B. You then locate the value at that same index in column A.

or if you want a single, albeit less readable, line you can do it like:

a_val = df.A[df.B.idxmin()]

Also, as a disclaimer this solution assumes that the minimum value in column B is unique. For example if you were to have a data set that looked like this:

A  B
1  2
2  5
3  0
4  3
5  0

My solution would return the first instance where B's minimum value is located which in this case is in the third row and has a corresponding A value of 3. If you believe that the minimum value of B is not unique then you should go with @ajcr's solution.

Upvotes: 3

Related Questions