[英]Apply function to every cell in DataFrame and include value from specific column
Say I have a pandas DataFrame like so:假设我有一个像这样的 Pandas DataFrame:
import pandas as pd
df = pd.DataFrame({'a': [1,2,3], 'b': [4,5,6], 'add': [10, 20, 30]})
I would like to perform an operation on each cell in columns 'a' and 'b' that includes both the cell value and the value of the 'add' column for that row.我想对 'a' 和 'b' 列中的每个单元格执行一个操作,其中包括该行的单元格值和 'add' 列的值。 Here's an example operation:
这是一个示例操作:
def add_vals(val, adder):
if adder % val == 0:
return val + adder
else:
return val + (val / adder)
I know I can do this with df.apply
, but I haven't been able to figure out how to add the value in the add
column to the function.我知道我可以用
df.apply
做到这df.apply
,但我一直无法弄清楚如何将add
列中的值add
到函数中。 My guess is the syntax is close to this, but I haven't gotten it to work:我的猜测是语法接近于此,但我还没有让它工作:
df.apply(lambda x: x.apply(add_vals, args=(x['add'])))
What's the best way to do this in pandas?在熊猫中做到这一点的最佳方法是什么? It doesn't have to be the most efficient, but I would like it to be good pandas code.
它不一定是最有效的,但我希望它是好的 Pandas 代码。
EDIT:编辑:
The output should look like this:输出应如下所示:
output = pd.DataFrame({'a': [11,22,33], 'b': [4.4,25,36]})
Vectorize add_vals
method with numpy.where
:使用
numpy.where
量化add_vals
方法:
import numpy as np
def add_vals(vals, adders):
return np.where(adders % vals == 0, vals + adders, vals + (vals / adders))
The method gives the transformation of a single column if you pass in a
or b
with the add
column as 2nd parameter:如果您将
a
或b
与add
列作为第二个参数一起传入,则该方法会提供单个列的转换:
add_vals(df['a'], df['add'])
# [11. 22. 33.]
And then you can apply the method to each column ( a
and b
) you want to transform:然后您可以将该方法应用于要转换的每一列(
a
和b
):
df[['a', 'b']].apply(add_vals, adders=df['add'])
# a b
#0 11.0 4.4
#1 22.0 25.0
#2 33.0 36.0
Starting with your dataframe:从您的数据框开始:
import pandas as pd
df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "add": [10, 20, 30]})
I then make a row function that apply your function to each row:然后我创建一个行函数,将您的函数应用于每一行:
def add_vals(val, adder):
if adder % val == 0:
return val + adder
else:
return val + (val / adder)
def row_add_vals(df, col):
return df.apply(lambda row: add_vals(row[col], row["add"]), axis=1)
Apply the function to each column:将函数应用于每一列:
df["a"] = row_add_vals(df, "a")
df["b"] = row_add_vals(df, "b")
Output:输出:
a b add
0 11 4.4 10
1 22 25.0 20
2 33 36.0 30
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.