Python Forum
Help with making a formula using variables that I have declared within for loops. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Help with making a formula using variables that I have declared within for loops. (/thread-23667.html)



Help with making a formula using variables that I have declared within for loops. - Tomf96 - Jan-11-2020

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!


RE: Help with making a formula using variables that I have declared within for loops. - buran - Jan-12-2020

It's always odd, when one try to describe what their code is doing, but don't show single line of code...


RE: Help with making a formula using variables that I have declared within for loops. - jefsummers - Jan-13-2020

Need the code, and need you to print head() of that frame once you have it in final form