Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python函数在多个系列中的应用_Python_Function_Dataframe_Apply - Fatal编程技术网

Python函数在多个系列中的应用

Python函数在多个系列中的应用,python,function,dataframe,apply,Python,Function,Dataframe,Apply,我有一个函数(假设): 我想将此函数应用到假设的数据帧中: df = number1 number2 0 20 30 1 25 10 其结果是: number1 number2 multiply 0 20 30 600 1 25 10 250 我尝试使用apply: df_['multiply'] = example_fct(df.number1,df.num

我有一个函数(假设):

我想将此函数应用到假设的数据帧中:

df = 
   number1   number2
0    20        30
1    25        10
其结果是:

   number1   number2   multiply
0    20        30        600
1    25        10        250
我尝试使用apply:

df_['multiply'] = example_fct(df.number1,df.number2)
但这不起作用,因为函数参数是标量而不是级数。我始终可以使用.apply,用于具有单个输入参数的函数,但此函数使用2个输入参数

此外,我还想知道这个函数是否可以用于来自不同数据帧的序列(但两个数据帧的长度相同)。

df.number1.mul(df.number2)
np.multiply(df.number1,df.number2)
np.multiply(df.number1,df.number2)
df_['multiply'] = example_fct(df.number1,df.number2)
In [81]: df
Out[81]:
   number1  number2
0       20       30
1       25       10

In [82]: def example_fct(x,y):
    ...:     return x*y
    ...:

In [83]: df["multiply"] = df.apply(lambda x:example_fct(x["number1"], x["number2"]), axis=1)

In [84]: df
Out[84]:
   number1  number2  multiply
0       20       30       600
1       25       10       250