How to show the itens that make a product

Question:

I have this data:

df = pd.DataFrame({"Fruits": ["orange_farm1", "orange_farm2", "orange_farm3", "orange_farm4", "orange_farm5", "apple_farm1", "apple_farm2", "apple_farm3", "apple_farm4", "banana_farm1", "banana_farm2", "banana_farm3", "banana_farm4", "orange_farm5", "apple_farm4", "orange_farm4", "apple_farm1","banana_farm1"], "Quantity":[400, 580, 300, 260, 540, 590, 400, 350, 480, 500, 590, 400, 350, 260, 540, 590, 400, 350], "Juice": ["mixedjuice1","mixedjuice1", "mixedjuice2", "mixedjuice2", "mixedjuice3", "mixedjuice3", "mixedjuice1", "mixedjuice3", "mixedjuice2", "mixedjuice2", "mixedjuice1", "mixedjuice2", "mixedjuice3", "mixedjuice3", "mixedjuice1", "mixedjuice2", "mixedjuice2", "mixedjuice3"], "used_qty": [50,60,70,2,40,12,34,40,50,20,13,22,14,40,50,20,13,24]})

I need to know how can i find the total of each fruit made the juice in quantity and percentage. Can someone help me?

Output:

df2 = pd.DataFrame({"Fruits":["orange_farm1", "orange_farm2", "apple_farm2", "banana_farm2", "apple_farm4", "orange_farm3", "orange_farm4", "apple_farm4", "banana_farm1", "banana_farm3", "orange_farm4", "apple_farm1","orange_farm5", "apple_farm1", "apple_farm3", "banana_farm4", "orange_farm5",  "banana_farm1"], 
                "Juice": ["mixedjuice1", "mixedjuice1", "mixedjuice1", "mixedjuice1", "mixedjuice1", "mixedjuice2", "mixedjuice2", "mixedjuice2", "mixedjuice2", "mixedjuice2", "mixedjuice2", "mixedjuice2", "mixedjuice3",  "mixedjuice3",  "mixedjuice3", "mixedjuice3", "mixedjuice3", "mixedjuice3"], 
                "% used_qty": [24.15, 28.99, 16.43,6.28, 24.15,35.53,1.02,25.38,10.15,11.17,10.15,6.60,23.53,7.06,23.53,8.24,23.53,14.12], 
                "% Quantity": [12.50, 10.34,8.50,2.20,9.26,23.33,0.24,10.42,4.00,5.50,2.13,3.25,5.00,2.03,11.43,4.00,5.00,6.86]})
Asked By: Ewallk

||

Answers:

You can apply your calculation on each row using lambda function as below,

Don’t know your calculation for '% used_qty' column, hence haven’t done but still you can insert your calculation here df.apply(lambda row: <<YourCalculation>>, axis=1)

Code:

df['% Quantity'] = df.apply(lambda row: row.used_qty/row.Quantity*100,axis=1)
Answered By: R. Baraiya
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.