plot_df.groupby
时间: 2023-10-19 15:08:19 浏览: 82
Python DataFrame.groupby()聚合函数,分组级运算
The `groupby` method in pandas is used to split the data into groups based on one or more criteria, and then apply a function to each group separately.
For example, if you have a DataFrame `df` with columns 'A', 'B', and 'C', you could use `groupby` to group the data by the values in column 'A' and then apply a function to each group of rows.
Here is an example of how to use `groupby` to group a DataFrame `plot_df` by the values in the 'Year' column and then calculate the mean value of the 'Value' column for each group:
```
grouped_df = plot_df.groupby('Year')['Value'].mean()
```
This will create a new DataFrame `grouped_df` that contains the mean value of the 'Value' column for each unique value in the 'Year' column.
You can also group by multiple columns by passing a list of column names to the `groupby` method:
```
grouped_df = plot_df.groupby(['Year', 'Month'])['Value'].mean()
```
This will group the data by the values in both the 'Year' and 'Month' columns and calculate the mean value of the 'Value' column for each group.
阅读全文