## Finding the Minimum Value by Group in Pandas DataFrame

Are you tired of scrolling through endless data just to find the minimum values? Do you want to increase your efficiency while working with data?

Look no further because we have the solution for you! In this article, we will be discussing how to find the minimum value by group in Pandas DataFrame using the groupby function and various methods.

### Method 1: Groupby Minimum of One Column

Firstly, we will demonstrate how to find the minimum value by group of one column using groupby in Pandas DataFrame.

Let us consider an example of a basketball tournament with the points scored by different teams in each game. We have the following data:

Team | Points |
---|---|

A | 70 |

A | 80 |

B | 60 |

B | 65 |

C | 75 |

C | 85 |

To find the minimum points scored by each team, we can use the .groupby() function.

Here’s the code:

```
import pandas as pd
data = {'team': ['A', 'A', 'B', 'B', 'C', 'C'], 'points': [70, 80, 60, 65, 75, 85]}
df = pd.DataFrame(data)
df.groupby(['team'])['points'].min()
```

### The output will be:

```
team
A 70
B 60
C 75
Name: points, dtype: int64
```

We used the groupby function to group the data by the team column and then selected the points column to find the minimum value for each group. The function returns a Pandas Series object with the minimum value for each group.

### Method 2: Groupby Minimum of Multiple Columns

Now, let’s see how to find the minimum value by group of multiple columns using groupby in Pandas DataFrame. Continuing with our basketball tournament example, let’s say we also want to find the team that scored the minimum points overall in the tournament.

We can achieve this by grouping the data by both team and points column. Here’s the code:

```
import pandas as pd
data = {'team': ['A', 'A', 'B', 'B', 'C', 'C'], 'points': [70, 80, 60, 65, 75, 85]}
df = pd.DataFrame(data)
df.groupby(['team', 'points']).size().reset_index(name='counts').sort_values(['points', 'counts']).groupby('points').first()
```

### The output will be:

```
team points counts
points
60 B 60 1
65 B 65 1
70 A 70 1
75 C 75 1
80 A 80 1
85 C 85 1
```

We first created a Pandas DataFrame using the data and then grouped it by team and points columns. We then used the size function to count the number of instances of each unique combination of team and points.

We reset the index and assigned the name ‘counts’ to the newly created column. We then sorted the data by points in ascending order and by counts in ascending order.

Finally, we grouped the data by points column and displayed only the first row of each group.

### Example 1: Groupby Minimum of One Column

For instance, let’s revisit our previous basketball tournament example and find the minimum points scored by each team.

Here’s a detailed explanation of the code:

```
import pandas as pd
data = {'team': ['A', 'A', 'B', 'B', 'C', 'C'], 'points': [70, 80, 60, 65, 75, 85]}
df = pd.DataFrame(data)
grouped_df = df.groupby(['team'])['points'].min()
print(grouped_df)
```

### The output will be:

```
team
A 70
B 60
C 75
Name: points, dtype: int64
```

We first imported the pandas library and created a dictionary with the data. We then created a DataFrame using the pandas DataFrame function.

We then grouped the data by the team column and selected the points column using the groupby function. We used the min function to find the minimum value for each group.

Finally, we printed the output, which consists of the minimum points for each team.

### Conclusion

In conclusion, finding the minimum value by group in Pandas DataFrame is very simple and can be achieved using the groupby function and various methods, depending on the requirements. We have covered two methods, namely groupby minimum of one column and groupby minimum of multiple columns.

These functions can be applied to several domains such as finance, sports, health, and many more. By using these functions, we can extract valuable insights from the data and enhance our decision-making capabilities.

### Example 2: Groupby Minimum of Multiple Columns

Let’s continue with our basketball tournament example and find the team that scored the minimum points and minimum rebounds overall in the tournament. We will use groupby minimum of multiple columns to achieve this.

The data we have is as follows:

Team | Points | Rebounds |
---|---|---|

A | 70 | 25 |

A | 80 | 30 |

B | 60 | 32 |

B | 65 | 28 |

C | 75 | 20 |

C | 85 | 22 |

We can use a similar method as before to find the minimum points for each team. The code is as follows:

```
import pandas as pd
data = {'team': ['A', 'A', 'B', 'B', 'C', 'C'], 'points': [70, 80, 60, 65, 75, 85], 'rebounds': [25, 30, 32, 28, 20, 22]}
df = pd.DataFrame(data)
grouped_df_points = df.groupby(['team'])['points'].min()
print(grouped_df_points)
```

### The output will be:

```
team
A 70
B 60
C 75
Name: points, dtype: int64
```

To find the minimum rebounds for each team, we can modify the code to group the data by team and rebounds columns instead of team and points. The code is as follows:

```
grouped_df_rebounds = df.groupby(['team'])['rebounds'].min()
print(grouped_df_rebounds)
```

### The output will be:

```
team
A 25
B 28
C 20
Name: rebounds, dtype: int64
```

Now, let’s combine the two dataframes to display the team that scored both the minimum points and rebounds overall in the tournament. The code is as follows:

```
grouped_df = pd.concat([grouped_df_points, grouped_df_rebounds], axis=1)
grouped_df['total'] = grouped_df['points'] + grouped_df['rebounds']
min_val = grouped_df['total'].min()
min_team = grouped_df[grouped_df['total'] == min_val].index[0]
print(f"Team {min_team} scored the least overall with a total of {min_val} points and rebounds combined.")
```

### The output will be:

`Team B scored the least overall with a total of 88 points and rebounds combined.`

In this case, team B scored the minimum points and minimum rebounds overall in the tournament. We can see that performing groupby minimum of multiple columns can provide powerful insights into the data.

### Additional Resources

Pandas is a powerful and flexible data manipulation tool in Python. There are many Pandas tutorials available online that can assist in learning and performing common tasks, such as groupby minimum.

Here are some resources to get started:

- Official Pandas Documentation: The official documentation provides in-depth coverage of the library’s features, data structures and functions, and examples of code.
- Pandas in 10 Minutes: This tutorial is a quick and accessible introduction to Pandas, covering the basics of creating DataFrames, selecting data, and performing common tasks such as aggregation and filtering.
- Pandas Cheat Sheet: This printable PDF is a handy one-page reference containing Pandas macros, functions, and code snippets for common tasks.
- Kaggle Kernels: Kaggle is a community-driven platform for data science, machine learning, and artificial intelligence.

Kaggle Kernels provide a wide range of community-generated examples and tutorials, including Pandas. By leveraging the power of Pandas and utilizing resources available online, analysts and data scientists can easily extract insights from their data and improve their workflows and decision-making capabilities.

In this article, we discussed how to find the minimum value by group in Pandas DataFrame using the groupby function and various methods. We covered two methods: groupby minimum of one column and groupby minimum of multiple columns.

We provided examples using basketball tournament data and demonstrated how to find the team that scored the minimum points and rebounds overall in the tournament. We also shared additional resources for learning Pandas and performing common tasks.

By utilizing these functions and resources, analysts and data scientists can easily extract valuable insights from their data, allowing them to make informed decisions that can impact their organization positively. Pandas groupby minimum is a powerful tool that should be considered when performing data analysis activities.