如何将参数动态传递给DataFrame.apply()lambda函数?

时间:2018-05-16 07:56:51

标签: python pandas apply

我正在尝试为Dataframe运行Pandas apply()方法来创建新列,如下所示:

df['new_column'] = df.groupby(groupby).apply(lambda x: some_function(x, df['existing_column']))

所以,基本上,我试图动态地传递some_function的第二个参数,相对于df['existing_column']存储的每个行的值(值不同)。

我得到的错误是常见TypeError: 'Series' objects are mutable, thus they cannot be hashed。有没有正确的方法来做到这一点?我尝试为每一行进行for循环,但这会杀死我的CPU。

1 个答案:

答案 0 :(得分:0)

如果每组需要existing_column新列,我建议使用transform

df['new_column'] = df.groupby('col')['existing_column'].transform(lambda x: funct(x, par))