Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas apply function on dataframe over multiple columns

Tags:

python

pandas

When I run the following code I get an KeyError: ('a', 'occurred at index a'). How can I apply this function, or something similar, over the Dataframe without encountering this issue?

Running python3.6, pandas v0.22.0

import numpy as np
import pandas as pd

def add(a, b):
    return a + b

df = pd.DataFrame(np.random.randn(3, 3), 
                  columns = ['a', 'b', 'c'])

df.apply(lambda x: add(x['a'], x['c']))
like image 764
labjunky Avatar asked Mar 20 '18 08:03

labjunky


1 Answers

I think need parameter axis=1 for processes by rows in apply:

axis: {0 or 'index', 1 or 'columns'}, default 0

0 or index: apply function to each column
1 or columns: apply function to each row

df = df.apply(lambda x: add(x['a'], x['c']), axis=1)
print (df)
0   -0.802652
1    0.145142
2   -1.160743
dtype: float64
like image 182
jezrael Avatar answered Oct 11 '22 19:10

jezrael