通过应用具有多个返回的函数创建多个 Pandas DataFrame 列 [英] Create multiple pandas DataFrame columns from applying a function with multiple returns

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

问题描述

我想将一个具有多次返回的函数应用到 pandas DataFrame 并将结果放在该 DataFrame 中的单独新列中.

I'd like to apply a function with multiple returns to a pandas DataFrame and put the results in separate new columns in that DataFrame.

所以给出这样的东西:

import pandas as pd

df = pd.DataFrame(data = {'a': [1, 2, 3], 'b': [4, 5, 6]})

def add_subtract(a, b):
  return (a + b, a - b)

目标是在 ab 上调用 add_subtract 以在 df 中创建两个新列的单个命令>:sumdifference.

The goal is a single command that calls add_subtract on a and b to create two new columns in df: sum and difference.

我认为这样的事情可能会奏效:

I thought something like this might work:

(df['sum'], df['difference']) = df.apply(
    lambda row: add_subtract(row['a'], row['b']), axis=1)

但它产生了这个错误:

---->9 lambda 行:add_subtract(row['a'], row['b']),axis=1)

----> 9 lambda row: add_subtract(row['a'], row['b']), axis=1)

ValueError: 解包的值太多(预期为 2)

ValueError: too many values to unpack (expected 2)

除了以下答案,pandas apply function that returns multiple values to rows in pandas dataframe 表明可以修改该函数以返回列表或Series,即:

In addition to the below answers, pandas apply function that returns multiple values to rows in pandas dataframe shows that the function can be modified to return a list or Series, i.e.:

def add_subtract_list(a, b):
  return [a + b, a - b]

df[['sum', 'difference']] = df.apply(
    lambda row: add_subtract_list(row['a'], row['b']), axis=1)

def add_subtract_series(a, b):
  return pd.Series((a + b, a - b))

df[['sum', 'difference']] = df.apply(
    lambda row: add_subtract_series(row['a'], row['b']), axis=1)

两者都有效(后者相当于 Wen 接受的答案).

both work (the latter being equivalent to Wen's accepted answer).

推荐答案

Adding pd.Series

df[['sum', 'difference']] = df.apply(
    lambda row: pd.Series(add_subtract(row['a'], row['b'])), axis=1)
df

收益

   a  b  sum  difference
0  1  4    5          -3
1  2  5    7          -3
2  3  6    9          -3

这篇关于通过应用具有多个返回的函数创建多个 Pandas DataFrame 列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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