I've been trying to figure out how to loop through multiple inputs for a defined function For the inputs I have:
#These lists will always be same size
Par= ('cat','dog','pig','mouse')
Var = ('john','mike','stan','steve')
Then I defined a function:
def analysis(par,var):
if data[par+'_cost'].dtype is np.dtype('O'):
data[par+'_cost']=data[par+'_cost'].str.replace(',','').astype('float32')
if data[var+'_payment'].dtype is np.dtype('O'):
data[var+'_payment']=data[var+'_payment'].str.replace(',','').astype('float32')
exception = data[(data[par+'_cost']==0) & (data[var+'_payment'] >0)]
data_new=data[-data['index'].isin(exception['index'])]
I tested parts of this small example function and the first two if statements worked but the last two lines where a new dataframe is produced doesn't actually return a dataframe. In the end, I want data_new to account for each column title in Par and Var.
Tried multiple loops:
for par in Par:
for var in Var:
analysis(par,var)
Used the zip function (for my first time) unsuccessfully
for par,var in zip(Par,Var):
analysis(par,var)
When trying to check if the function resulted in a dataframe, data_new, as I'd like, continued to get the error that it didn't actually create the dataframe.
exception.describe()
NameError: name 'exception' is not defined
I am still new to Python and mostly use PANDAS so am not sure of other functions. The following questions gave me an idea of what to do but didn't quite solve my problem:
Iterating through two lists: How can I iterate through two lists in parallel?
Multiprocessing : Python multiprocessing a function with several inputs
Converting list of several inputs: Pythonic way of converting a list to inputs of a function of several variables
Any ideas if my function isn't correctly defined, as in the reason I am not seeing a data_new as result of the output is because of what I wrote, or if the code isn't looping through each pair of values and applying the function to it.
Any insight is, as always, much appreciated!