应用具有多个参数的函数以创建新的pandas列

应用具有多个参数的函数以创建新的pandas列

本文介绍了应用具有多个参数的函数以创建新的pandas列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过将一个函数应用于两个现有列在pandas数据框中创建一个新列.遵循此 answer 当我只需要一个列作为参数时,我已经能够创建一个新列:

I want to create a new column in a pandas data frame by applying a function to two existing columns. Following this answer I've been able to create a new column when I only need one column as an argument:

import pandas as pd
df = pd.DataFrame({"A": [10,20,30], "B": [20, 30, 10]})

def fx(x):
    return x * x

print(df)
df['newcolumn'] = df.A.apply(fx)
print(df)

但是,当函数需要多个参数时,我无法弄清楚该怎么做.例如,如何通过将A列和B列传递给下面的函数来创建新列?

However, I cannot figure out how to do the same thing when the function requires multiple arguments. For example, how do I create a new column by passing column A and column B to the function below?

def fxy(x, y):
    return x * y

推荐答案

或者,您可以使用numpy基础函数:

Alternatively, you can use numpy underlying function:

>>> import numpy as np
>>> df = pd.DataFrame({"A": [10,20,30], "B": [20, 30, 10]})
>>> df['new_column'] = np.multiply(df['A'], df['B'])
>>> df
    A   B  new_column
0  10  20         200
1  20  30         600
2  30  10         300

或在一般情况下矢量化任意函数:

or vectorize arbitrary function in general case:

>>> def fx(x, y):
...     return x*y
...
>>> df['new_column'] = np.vectorize(fx)(df['A'], df['B'])
>>> df
    A   B  new_column
0  10  20         200
1  20  30         600
2  30  10         300

这篇关于应用具有多个参数的函数以创建新的pandas列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 13:16