1 可以使用pd.merge()函数包聚合操作的计算结果添加到df的每一行
d1={'item':['luobo','baicai','lajiao','donggua','luobo','baicai','lajiao','donggua'], 'color':['white','white','red','green','white','white','red','green'], 'weight':np.random.randint(10,size = 8), 'price':np.random.randint(10,size = 8)} df = DataFrame(d1) sums = df.groupby('color').sum().add_prefix('total_') items = df.groupby('item')['price','weight'].sum() means = items['price']/items['weight'] means = DataFrame(means,columns=['means_price']) df2 = pd.merge(df,sums,left_on = 'color',right_index = True) df3 = pd.merge(df2,means,left_on = 'item',right_index = True) display(df2,df3) #输出: color item price weight 0 white luobo 9 2 1 white baicai 5 9 2 red lajiao 5 8 3 green donggua 1 1 4 white luobo 7 4 5 white baicai 8 0 6 red lajiao 6 8 7 green donggua 4 3 total_price total_weight color green 5 4 red 11 16 white 29 15 pandas.core.frame.DataFrame pandas.core.frame.DataFrame Out[141]: color item price weight total_price total_weight 0 white luobo 9 2 29 15 1 white baicai 5 9 29 15 4 white luobo 7 4 29 15 5 white baicai 8 0 29 15 2 red lajiao 5 8 11 16 6 red lajiao 6 8 11 16 3 green donggua 1 1 5 4 7 green donggua 4 3 5 42 可以使用transform和apply实现相同功能
使用transform
d1={'item':['luobo','baicai','lajiao','donggua','luobo','baicai','lajiao','donggua'], 'color':['white','white','red','green','white','white','red','green'], 'weight':np.random.randint(10,size = 8), 'price':np.random.randint(10,size = 8)} df = DataFrame(d1) sum1 = df.groupby('color')['price','weight'].sum().add_prefix("total_") sums2 = df.groupby('color')['price','weight'].transform(lambda x:x.sum()).add_prefix('total_') sums3 = df.groupby('color')['price','weight'].transform(sum).add_prefix('total_') display(sum,df,sum1,sums2,sums3) 输出: <function sum> color item price weight 0 white luobo 7 7 1 white baicai 7 7 2 red lajiao 2 7 3 green donggua 6 6 4 white luobo 1 2 5 white baicai 3 6 6 red lajiao 7 0 7 green donggua 0 2 total_price total_weight color green 6 8 red 9 7 white 18 22 total_price total_weight 0 18 22 1 18 22 2 9 7 3 6 8 4 18 22 5 18 22 6 9 7 7 6 8 total_price total_weight 0 18 22 1 18 22 2 9 7 3 6 8 4 18 22 5 18 22 6 9 7 7 6 8