I've checked out map, apply, mapapply, and combine, but can't seem to find a simple way of doing the following:
我已经检查了map、apply、mapapply和combine,但似乎找不到一种简单的方法来完成以下操作:
I have a dataframe with 10 columns. I need to pass three of them into a function that takes scalars and returns a scalar ...
我有一个包含10列的dataframe。我需要将它们中的三个传递给一个函数,该函数接受标量并返回标量……
some_func(int a, int b, int c) returns int d
I want to apply this and create a new column in the dataframe with the result.
我想应用它,并在dataframe中创建一个包含结果的新列。
df['d'] = some_func(a = df['a'], b = df['b'], c = df['c'])
All the solutions that I've found seem to suggest to rewrite some_func to work with Series instead of scalars, but this is not possible as it is part of another package. How do I elegantly do the above?
我发现的所有解决方案似乎都建议重写some_func以使用级数而不是标量,但这是不可能的,因为它是另一个包的一部分。我如何优雅地完成上面的工作?
2 个解决方案
#1
13
Use pd.DataFrame.apply()
, as below:
使用pd.DataFrame.apply(),如下:
df['d'] = df.apply(lambda x: some_func(a = x['a'], b = x['b'], c = x['c']), axis=1)
NOTE: As @ashishsingal asked about columns, the axis
argument should be provided with a value of 1, as the default is 0 (as in the documentation and copied below).
注意:当@ashishsingal询问列时,axis参数应该为1,因为默认值是0(如文档中所示,并在下面复制)。
axis : {0 or ‘index’, 1 or ‘columns’}, default 0
axis:{0或' index ', 1或' columns '},默认为0
- 0 or ‘index’: apply function to each column
- 0或“索引”:将函数应用于每一列
- or ‘columns’: apply function to each row
- 或“列”:对每一行应用函数
#2
4
I'm using the following:
我使用下面的:
df['d'] = df.apply(lambda x: some_func(a = x['a'], b = x['b'], c = x['c']))
Seems to be working well, but if anyone else has a better solution, please let me know.
看起来工作得不错,但是如果有人有更好的解决办法,请告诉我。
#1
13
Use pd.DataFrame.apply()
, as below:
使用pd.DataFrame.apply(),如下:
df['d'] = df.apply(lambda x: some_func(a = x['a'], b = x['b'], c = x['c']), axis=1)
NOTE: As @ashishsingal asked about columns, the axis
argument should be provided with a value of 1, as the default is 0 (as in the documentation and copied below).
注意:当@ashishsingal询问列时,axis参数应该为1,因为默认值是0(如文档中所示,并在下面复制)。
axis : {0 or ‘index’, 1 or ‘columns’}, default 0
axis:{0或' index ', 1或' columns '},默认为0
- 0 or ‘index’: apply function to each column
- 0或“索引”:将函数应用于每一列
- or ‘columns’: apply function to each row
- 或“列”:对每一行应用函数
#2
4
I'm using the following:
我使用下面的:
df['d'] = df.apply(lambda x: some_func(a = x['a'], b = x['b'], c = x['c']))
Seems to be working well, but if anyone else has a better solution, please let me know.
看起来工作得不错,但是如果有人有更好的解决办法,请告诉我。