Python Forum

Full Version: Help with making a formula using variables that I have declared within for loops.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am using python 3.8.1 and pandas.

I have two sets of data as csv files. orders.csv and vouchers.csv. Using vouchers.csv I have grouped the dataframe by 'Name' by iterating through the csv file in a for loop I can come up with a variable containing the Name, Discount1 and Discount2 (these discounts need to be applied differently that is why they are separate.

From orders.csv i also group by 'Name' and iterate through a for loop to calculate total sales and deductions. (I do this by filtering and summing columns of the csv for each sub dataframe(which is a list of orders for the 'Name').

I need to use the Discount1 and Discount2 in my final calculation. I am unsure how to get the program to recognise that if the name matches then apply this discount otherwise do not or apply 0.

Sidenotes: When I print my Discount1 and discount2 variables outside the loop it only prints the last iteration of the loop. I can get it to work by appending the variable into a list or even a nested list, however, I do not know where to go from here or whether that is the correct thing to do.

Just ask if you need more clarity anywhere I struggle to understand my issue myself let alone write it down.

Thanks in advance!
It's always odd, when one try to describe what their code is doing, but don't show single line of code...
Need the code, and need you to print head() of that frame once you have it in final form