pandas groupby
时间: 2023-08-27 15:05:18 浏览: 94
Pandas groupby is a powerful function that allows you to group data based on one or more columns and perform various operations on the groups. The basic syntax for using groupby in Pandas is as follows:
```python
df.groupby('column_name')
```
The output of this function is a groupby object, which can be used to perform various operations on the groups. Some common operations that can be performed on the groups are:
- `mean()`: computes the mean of the values in each group
- `sum()`: computes the sum of the values in each group
- `count()`: computes the number of values in each group
- `max()`: computes the maximum value in each group
- `min()`: computes the minimum value in each group
You can also group by multiple columns by passing a list of column names to the groupby function, like this:
```python
df.groupby(['column_name_1', 'column_name_2'])
```
Additionally, you can apply custom functions to the groups using the `apply()` method. For example, you can define a custom function that computes the median of a series, and apply it to each group like this:
```python
def my_median(x):
return np.median(x)
df.groupby('column_name').apply(my_median)
```
阅读全文