简体   繁体   English

将函数应用于 DataFrame 中的每个单元格并包含来自特定列的值

[英]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:如果您将abadd列作为第二个参数一起传入,则该方法会提供单个列的转换:

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:然后您可以将该方法应用于要转换的每一列( ab ):

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.

相关问题 如何将函数应用于pandas数据框中列的每个值? - How to apply a function to every value in a column in a pandas dataframe? 根据特定列中的值将函数应用于熊猫中的数据框行 - Apply function to dataframe row in pandas based on value in specific column 将函数应用于pandas中数据框的每一列 - Apply a function to every column of a dataframe in pandas pandas DataFrame,如何将函数应用于特定列? - pandas DataFrame, how to apply function to a specific column? Pandas DataFrame将特定功能应用于每列 - Pandas DataFrame apply Specific Function to Each column 对于数据框中的每个列和单元格,使用该列中的随机值填写NaN / Null - For every column and cell in dataframe fill in NaNs/Nulls with random value from that column 使用来自特定列的信息将 function 应用于 pandas dataframe 的每个单元格 - Apply a function to each cell of a pandas dataframe using information from a particular column 是否将函数应用于pandas数据框的每一列而没有for循环? - Apply function to every column of pandas dataframe without for loop? 如何将自定义函数应用于pandas中数据框的每一列? - How to apply a custom function to every column of a dataframe in pandas? 将函数应用于pandas数据框列中每一行的每个单词 - apply function to each word of every row in pandas dataframe column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM