应用具有多个参数的函数来创建新的 Pandas 列 [英] Applying function with multiple arguments to create a new pandas column

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

问题描述

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

将pandas导入为pddf = pd.DataFrame({"A": [10,20,30], "B": [20, 30, 10]})定义 fx(x):返回 x * x打印(df)df['newcolumn'] = df.A.apply(fx)打印(df)

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

def fxy(x, y):返回 x * y

解决方案

或者,你可以使用 numpy 底层函数:

<预><代码>>>>将 numpy 导入为 np>>>df = pd.DataFrame({"A": [10,20,30], "B": [20, 30, 10]})>>>df['new_column'] = np.multiply(df['A'], df['B'])>>>dfA B new_column0 10 20 2001 20 30 6002 30 10 300

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

<预><代码>>>>def fx(x, y):...返回 x*y...>>>df['new_column'] = np.vectorize(fx)(df['A'], df['B'])>>>dfA B new_column0 10 20 2001 20 30 6002 30 10 300

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)

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

解决方案

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 列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆