pandas 大熊猫应用带参数的函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45513609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pandas apply function with arguments
提问by ejshin1
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))
回答by harryscholes
Pass the arguments as kwargs to apply
:
将参数作为 kwargs 传递给apply
:
df['events'] = df['data'].apply(count_ones, total_bits=60, group_size=12)
回答by Naomi Fridman
use lambda:
使用拉姆达:
def do_on_col(x, argument1):
return x+argument1
df[col] = df[col].apply(lambda x: do_on_col(x, argument1))