Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dataframe list comprehension "zip(...)": loop through chosen df columns efficiently with just a list of column name strings

This is just a nitpicking syntactic question...

I have a dataframe, and I want to use list comprehension to evaluate a function using lots of columns.

I know I can do this

df['result_col'] = [some_func(*var) for var in zip(df['col_1'], df['col_2'],... ,df['col_n'])]

I would like to do something like this

df['result_col'] = [some_func(*var) for var in zip(df[['col_1', 'col_2',... ,'col_n']])]

i.e. not having to write df n times. I cannot for the life of me figure out the syntax.

like image 757
mortysporty Avatar asked Sep 17 '25 16:09

mortysporty


2 Answers

this should work, but honestly, OP figured it himself as well, so +1 OP :)

df['result_col'] = [some_func(*var) for var in zip(*[df[col] for col in ['col_1', 'col_2',... ,'col_n']])]
like image 184
deadvoid Avatar answered Sep 19 '25 06:09

deadvoid


As mentioned in the comments above, you should use apply instead:

df['reult_col'] = df.apply(lambda x: some_func(*tuple(x.values)), axis=1)
like image 42
gyx-hh Avatar answered Sep 19 '25 08:09

gyx-hh