我想创建具有唯一产品和每个产品平均价格的数据框。
df1
product 1 black 10.0
product 2 grey 20.0
product 2 grey 20.0
product 1 black 20.0
product 1 black 10.0
product 3 red 23.0
product 1 black 10.0
我要创建看起来像这样的数据框
df2
product 1 black 12.5
product 2 grey 20.0
product 3 red 23.0
我尝试过,但这花了太长时间
df2 = pd.DataFrame()
df2['Product']=df1['Product'].unique()
df2['Color']=df1['Color']
products = data_by_material['Product'].tolist()
product_list = []
for m in range(len(products)):
product_list.append(df1[df1['Product']==products[m]]['Price'].mean())
df2['Average price'] = pd.concat(product_list, axis=0, ignore_index=True)
将来希望添加其他产品功能。
有什么想法吗?