简体   繁体   English

熊猫通过参数应用函数

[英]pandas apply function with arguments

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. total_bits为60,group_size为12。

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

将参数传递为kwargs以apply

df['events'] = df['data'].apply(count_ones, total_bits=60, group_size=12)

use lambda: 使用lambda:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM