Home > Back-end >  how to apply different custom function based on dataframe row values
how to apply different custom function based on dataframe row values

Time:02-04

I have following dataframe
df = pd.DataFrame({'id': ['b', 'b', 'b', 'b', 'a', 'a'], 'y': [1,2,3,4,1,2], 'v': [10, 8, 12, 18, -5, 10]})

I defined two custom functions say func1 and func2 with the df as the input. Both functions return a dataframe which have two columns x1 and x2. The function calculations are pretty complicated hence I am not posting here. My question is how do I output a dataframe with x1 and x2 column such that when id=b, apply func1 and else apply func2 to get the x1 and x2 values?
I tried this code

result=df.apply(lambda x: func(1) if df['id']=='b' else func2(x), axis=1)

It gave me the error "the truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()."

CodePudding user response:

IIUC use any for test if at least one value per row is b:

result=df.apply(lambda x: func(1) if (df['id']=='b').any() else func2(x), axis=1)

CodePudding user response:

Create a function consisting of several functions. Then feed both id and the value you need to that function:

func_collective(id_,y_value,v_value):
   if (id_ == 'b'):
        return func1(y_value,v_value)
   if (id_ == 'a'):
        return func2(y_value,v_value) # functions 1 and 2 should have a return in form of "return [x1,x2]" if you wanna return two columns

df[['x1','x2']] = df.apply(lambda x: func_collective(x['id'],x['y'],x['v']), axis=1)
  •  Tags:  
  • Related