Python 如何将pd.apply与接受1个输入参数的自定义函数一起使用

Python 如何将pd.apply与接受1个输入参数的自定义函数一起使用,python,pandas,lambda,Python,Pandas,Lambda,因此,我有一个自定义函数,我想应用于数据帧中的一行数据,但是如何包含我需要的额外参数,我给出了下面的一个示例 # Using df.apply df = pd.DataFrame({"A": [1,2,3]}) sum_A = np.sum(df.A) def calc_weight(row, total): row["weights"] = row["A"]/total df.apply(calc_weight(row, sum_A), axis = 1) # Gives Name

因此,我有一个自定义函数,我想应用于数据帧中的一行数据,但是如何包含我需要的额外参数,我给出了下面的一个示例

# Using df.apply
df = pd.DataFrame({"A": [1,2,3]})
sum_A = np.sum(df.A)

def calc_weight(row, total):
    row["weights"] = row["A"]/total

df.apply(calc_weight(row, sum_A), axis = 1)
# Gives NameError: name 'row' is not defined

df.apply(calc_weight(row, sum_A), axis = 1)
# TypeError: calc_weight() missing 1 required positional argument: 'total'
我想要的输出类似于:

  A weights
0 1  0.166 
1 2  0.333
2 3   0.5

我已经在线查看了,但似乎找不到任何内容,或者我必须默认使用for循环来执行类似操作吗?

尝试在apply函数中添加参数,如下所示:

import pandas as pd                                                                                                  
import numpy as np

df = pd.DataFrame({"A": [1,2,3]})                                                                                    
sum_A = np.sum(df.A)                                                                                                 

def f(a, total):
    return float(a)/total                                                                                            

df['weight'] = df['A'].apply(f, args=(sum_A,))                                                                       
print df    
输出:

   A    weight
0  1  0.166667
1  2  0.333333
2  3  0.500000
~