Home >Backend Development >Python Tutorial >How to Find Rows with the Minimum Value in a Column Using Pandas GroupBy?
Given a Pandas DataFrame containing multiple columns, the task is to identify and select rows with the minimum value in a specific column for each unique value in another column. For instance, in a DataFrame with columns A, B, and C, you want to retrieve rows with the minimum B value for each A value.
A simple and efficient approach to solve this problem is to utilize the groupby and idxmin functions of Pandas. The groupby function groups rows by a specified column, while idxmin returns the index of the row with the minimum value in another column.
<code class="python">df.loc[df.groupby('A').B.idxmin()]</code>
This line of code achieves the desired result. First, it uses groupby('A') to group the DataFrame by column A. Then, it applies the B.idxmin() function to each group, which returns the index of the row with the minimum B value within that group. Finally, the loc function is used to select the rows corresponding to the minimum B values.
Consider the following DataFrame:
<code class="python">df = pd.DataFrame({'A': [1, 1, 1, 2, 2, 2], 'B': [4, 5, 2, 7, 4, 6], 'C': [3, 4, 10, 2, 4, 6]})</code>
Applying the solution code produces the following result:
A B C 2 1 2 10 4 2 4 4
This output shows the rows containing the minimum B value for each unique A value (2 for A=1 and 4 for A=2).
As an alternative, you can use reset_index(drop=True) to remove the index column from the resulting DataFrame:
<code class="python">df.loc[df.groupby('A').B.idxmin()].reset_index(drop=True)</code>
This will give you a DataFrame with only the columns you need.
The above is the detailed content of How to Find Rows with the Minimum Value in a Column Using Pandas GroupBy?. For more information, please follow other related articles on the PHP Chinese website!