有没有办法使用一些预先构建的 Pandas 函数一次性计算任意数量的不同 groupby 级别?下面是一个包含两列的简单示例。import pandas as pddf1 = pd.DataFrame( { "name" : ["Alice", "Bob", "Mallory", "Mallory", "Bob" , "Mallory"], "city" : ["Seattle", "Seattle", "Portland", "Seattle", "Seattle", "Portland"], "dollars":[1, 1, 1, 1, 1, 1] })group1 = df1.groupby("city").dollars.sum().reset_index()group1['name']='All'group2 = df1.groupby("name").dollars.sum().reset_index()group2['city']='All'group3 = df1.groupby(["name", "city"]).dollars.sum().reset_index()total = df1.dollars.sum()total_df=pd.DataFrame({ "name" : ["All"], "city" : ["All"], "dollars": [total] })all_groups = group3.append([group1, group2, total_df], sort=False) name city dollars0 Alice Seattle 11 Bob Seattle 22 Mallory Portland 23 Mallory Seattle 10 All Portland 21 All Seattle 40 Alice All 11 Bob All 22 Mallory All 30 All All 6所以我带了本。T 示例并将其从 sum() 重建为 agg()。对我来说,下一步是构建一个选项来传递特定的 groupby 组合列表,以防不需要所有组合。from itertools import combinationsimport pandas as pddf1 = pd.DataFrame( { "name" : ["Alice", "Bob", "Mallory", "Mallory", "Bob" , "Mallory"], "city" : ["Seattle", "Seattle", "Portland", "Seattle", "Seattle", "Portland"], "dollars":[1, 2, 6, 5, 3, 4], "qty":[2, 3, 4, 1, 5, 6] , "id":[1, 1, 2, 2, 3, 3] })col_gr = ['name', 'city']agg_func={'dollars': ['sum', 'max', 'count'], 'qty': ['sum'], "id":['nunique']}def multi_groupby(in_df, col_gr, agg_func, all_value="ALL"): tmp1 = pd.DataFrame({**{col: all_value for col in col_gr}}, index=[0]) tmp2 = in_df.agg(agg_func)\ .unstack()\ .to_frame()\ .transpose()\ .dropna(axis=1) tmp2.columns = ['_'.join(col).strip() for col in tmp2.columns.values] total = tmp1.join(tmp2)
查看完整描述