pandas apply function with arguments

Viewed 20246

I have one function that takes three arguments. And here's the heading.

def count_ones(num, total_bits, group_size):

And I am trying to apply this function to data column. But it is not returning what I expected. Could anyone help me out on this problem? total_bits are 60 and group_size is 12.

df['events'] = df['data'].apply(count_ones, args =(60, 12))
2 Answers

use lambda:

def do_on_col(x, argument1):
  return x+argument1

df[col] = df[col].apply(lambda x: do_on_col(x, argument1))
Related