0

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!

Community
  • 1
  • 1
Alexis Perez
  • 201
  • 2
  • 9
  • Where have you defined `exception`? If inside the function add a `global exception` line at the start – Bhargav Rao Jan 02 '15 at 19:58
  • Are you missing your return in your analysis function or did you just omit it when you pasted your code here? Also, you said your zip didn't work. Was there and error? – Bob Haffner Jan 02 '15 at 20:06
  • @BobHaffner I didn't add a return function, this is one of my first time creating a function. Where would I add it? – Alexis Perez Jan 02 '15 at 21:05
  • As a side note, **Par** and **Var** should be called **tuple** instead, not **list** – Anzel Jan 02 '15 at 21:06
  • @BhargavRao exception is defined within the function. If I call out exception=DataFrame() before the function, I still just get an empty dataframe without the function adding anything to it as I would like – Alexis Perez Jan 02 '15 at 21:07
  • @AlexisPerez, you're trying to feed some data back to the dataframe, how's your original dataframe looks like (dtype etc.)? – Anzel Jan 02 '15 at 21:09
  • @AlexisPerez You would add it at the beginning of your last function line. I don't think it would get you much closer to your goal though. I think you want a new dataframe (data_new) that's based on a subset of data by applying a multi-column 'filter'. Is that correct? – Bob Haffner Jan 02 '15 at 21:41

0 Answers0