You use the following code
df[['c', 'd']] = df['a', 'b'].apply(calc, type='both', axis=1)
There are several issues:
- To select multi columns, you need to use a list of column names, like
df[['a', 'b']]
.
pandas.DataFrame.apply()
doesn't have type
argument.
- 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
- 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')
- 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