I am trying to preprocess a dataset with pandas. I want to use a function with multiple arguments (one from a column of the dataframe, others are variables) which returns several outputs like this:
def preprocess(Series,var1,var2,var3,var4):
return 1,2,3,4
I want to use the native pandas.apply to use this function on one column of my dataframe like this:
import pandas as pd
df = pd.DataFrame([[4, 9]] * 3, columns=['A', 'B'])
df['C'], df['D'], df['E'], df['F'] = df.apply(lambda x: preprocess(x['A'], 1, 2, 3, 4), axis=1)
But the last line gives me the following error:
ValueError: not enough values to unpack (expected 4, got 3)
I understand my last line returns one tuple of 4 values (1,2,3,4) per line whereas I wanted to get each of these values in the columns C, D, etc.
How can I perform this?