Passing all pandas DataFrame columns to function as separate parameters
tjol at tjol.eu
Fri Apr 27 10:18:24 EDT 2018
On 27/04/18 15:50, zljubisic at gmail.com wrote:
> I have pandas DataFrame with several columns. I have to pass all columns as separate parameter to a function.
> Something like this if I have 4 columns.
> f, p = stats.f_oneway(df_piv.iloc[:, 0], df_piv.iloc[:, 1], df_piv.iloc[:, 2], df_piv.iloc[:, 3])
> As number of columns varies, how to do it in the most efficient way?
You could get the DataFrame as a 2d array (df.values) and work from there:
Of course, this is completely unreadable, and it has another problem: if
your columns have different types, this will cast some or all of your
columns to a different type.
It's probably clearer if you start with the column labels:
func(*(df.loc[:,col] for col in df.columns))
or use df.items():
func(*(values for col, values in df.items()))
More information about the Python-list