pandas pivot_table ratio filter
时间: 2023-12-27 08:03:51 浏览: 81
As an AI language model, I don't have access to your data to provide a specific answer. However, here is some general information on using filters in a pandas pivot_table:
In a pandas pivot_table, you can use the "values" parameter to specify the column(s) to be aggregated, and the "index" and "columns" parameters to specify the rows and columns, respectively. You can also use the "aggfunc" parameter to specify the aggregation function (e.g. sum, mean, count, etc.).
To filter the data before aggregating, you can use the "query" method or boolean indexing. For example, if you have a DataFrame "df" with columns "A", "B", and "C", and you want to filter the rows where "A" is greater than 10, you can do:
```
filtered_df = df[df["A"] > 10]
```
Then, you can create a pivot table from the filtered DataFrame:
```
pivot_table = pd.pivot_table(filtered_df, values="C", index="A", columns="B", aggfunc="sum")
```
This will create a pivot table with the sum of "C" values for each unique combination of "A" and "B" in the filtered DataFrame.
You can also use the "query" method to filter the data directly in the pivot_table:
```
pivot_table = pd.pivot_table(df, values="C", index="A", columns="B", aggfunc="sum").query("A > 10")
```
This will create a pivot table with the sum of "C" values for each unique combination of "A" and "B", but only for the rows where "A" is greater than 10.
阅读全文