素胚勾勒不出你
您可以使用字典以有组织的方式进行聚合。df = pd.DataFrame([[123, 30, 20, 2], [123, np.nan, 10, np.nan], [124, 50, 25, 3], [124, np.nan, 15, np.nan], [124, np.nan, 10, np.nan]], columns=['id', 'purchase_amount', 'price_products', 'num_products'] )agg_dict = { 'purchase_amount': [np.sum, np.mean], 'num_products': [np.count_nonzero], 'price_products': [np.count_nonzero, np.mean],}print(df.groupby('id').agg(agg_dict))输出: purchase_amount num_products price_products sum mean count_nonzero count_nonzero meanid 123 30.0 30.0 2.0 2 15.000000124 50.0 50.0 3.0 3 16.666667