-1

Lets say i have a dataframe df with columns a, b like below: a b 1 4 2 5 3 6

Lets assume we have similar function which returns 2 values

fun calc(a, b, type):
 if type=='both':
  c=a+b
  d=a-b
return c, d

how to store the returned values to a new column in dataframe df i tried below syntax, but getting error mentioning

df[['c', 'd']] = df['a', 'b'].apply(calc, type='both', axis=1)

but getting error as calc got an unexpected keyword argument 'axis'

3
  • Neither of the code snippets run. functions are defined with def not fun in python. I think you also meant df[['a', 'b']].apply(calc... to select those two columns. Commented May 7, 2021 at 2:31
  • 1
    See: pandas apply function that returns multiple values to rows in pandas dataframe Commented May 7, 2021 at 2:31
  • Although this isn't related to your question, I would suggest that you don't use type as a keyword argument for your function as type is a built-in function in Python. Commented May 7, 2021 at 4:14

1 Answer 1

0

You use the following code

df[['c', 'd']] = df['a', 'b'].apply(calc, type='both', axis=1)

There are several issues:

  1. To select multi columns, you need to use a list of column names, like df[['a', 'b']].
  2. pandas.DataFrame.apply() doesn't have type argument.
  3. Since you use axis=1, calc should only take one argument which is the row of dataframe.

If you want to pass extra arguments to calc, you can do in two ways

  1. Use args argument of apply()
def calc(row, type):
    a = row['a']
    b = row['b']

    if type=='both':
        c = a+b
        d = a-b

    return c, d

df[['c', 'd']] = df[['a', 'b']].apply(calc, args=('both',), axis=1, result_type='expand')
  1. You can use lambda funciton
def calc(row, type):
    a = row['a']
    b = row['b']

    if type=='both':
        c = a+b
        d = a-b

    return c, d

df[['c', 'd']] = df[['a', 'b']].apply(lambda row: calc(row, 'both'), axis=1, result_type='expand')

At last, since type is a built-in function of Python. You'd better not use it as variable name.

A MRE

import pandas as pd


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

def calc(row, type):
    a = row['a']
    b = row['b']

    if type=='both':
        c = a+b
        d = a-b

    return c, d

df[['c', 'd']] = df[['a', 'b']].apply(lambda row: calc(row, 'both'), axis=1, result_type='expand')

df[['e', 'f']] = df[['a', 'b']].apply(calc, args=('both',), axis=1, result_type='expand')
print(df)

   a  b  c  d  e  f
0  1  4  5 -3  5 -3
1  2  5  7 -3  7 -3
2  3  6  9 -3  9 -3
Sign up to request clarification or add additional context in comments.

2 Comments

Tried both suggesstions: getting error case1 - TypeError calc() got an unexpected keyword argument 'axis' case 2 - TypeError: <lambda>() got an unexpected keyword argument 'axis'
@crybaby I added a MRE in my answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.