Pandas Pivot Table to Groupby: Efficient Data Aggregation in Python
This code snippet demonstrates how to convert a Pandas pivot table operation into an equivalent groupby operation. The pivot table utilizes pd.pivot_table(df, values=['销售额', '数量'], index=['省/自治区', '类别'], aggfunc=['mean', sum], margins=True) to calculate the mean and sum of '销售额' and '数量' based on '省/自治区' and '类别', including margins.
The equivalent groupby operation can be achieved using df.groupby(['省/自治区', '类别']).agg({'销售额': ['mean', sum], '数量': ['mean', sum]}).fillna('All'). This method groups the data by '省/自治区' and '类别' and then applies the specified aggregation functions ('mean' and 'sum') to '销售额' and '数量' columns. The fillna('All') is used to fill any missing values with 'All'.
By utilizing the groupby approach, you can achieve the same data aggregation results as the pivot table but potentially with improved performance, especially for larger datasets.
原文地址: https://www.cveoy.top/t/topic/pfWv 著作权归作者所有。请勿转载和采集!