I don't understand well how the apply
function works.
Here's my code which works fine:
dftest = pd.DataFrame({'a': ['A BERTHOU'], 'b': ['BERTHOU']})
def test2(a, b):
return a + b
dftest['concat'] = dftest.apply(lambda row: test2(row['a'], row['b']), axis=1)
But I want to do the same without using lambda function. I tried this:
dftest['concat'] = dftest.apply(test2(dftest['a'], dftest['b']), axis=1)
and this
dftest['concat'] = dftest.apply(test2(dftest['a'].str, dftest['b'].str), axis=1)
But none works.
Can you tell me how to use my function without using lambda function?
A precision : I want to use complex function so
df['sum'] = df.col1 + df.col2 or
dftest['concat'] = dftest[['a', 'b']].sum(axis=1)
wont' work.
I knew the solution
dftest['concat'] = dftest.apply(test2, axis=1)
def test2(row):
return row.a + row.b
but I don't like it : it's impossible to undestand what is applied without looking to function (no parameter in the apply ligne) + the function is ugly : the function is not generic and tied to row.a and row.b
Conclusion : for the moment the best solution seems to be
dftest['concat'] = dftest.apply(lambda row: test2(row['a'], row['b']), axis=1)
and it seems imposssible to do it without the use of lambda on a complex function and using good coding pratices