0

Good Morning,

I have the following dataset:

print(df)

Product_code   Quantity_in_stock   
    01                4                     
    02                1                   
    01                2                   
    03                6                   
    02                9                    
...

I would like to create an additional column "Average_quantity", using Pandas, equals to the average "Quantity_in_stock" of that specific product; for instance:

Product_code   Quantity_in_stock    Average_quantity
    01                4                    3  
    02                1                    5
    01                2                    3
    03                6                    6
    02                9                    5
...

I tried:

for i in df["Product_code"]:
    df["Average_quantity"] = np.mean(df["Quantity_in_stock"])

But it reports the same average quantity of the whole set, not the one of that specific product code

How Can I solve it?

Alessandro Ceccarelli
  • 1,775
  • 5
  • 21
  • 41

0 Answers0