Fourier
Fourier

Reputation: 2993

Pandas apply function to groups of columns and indexing

Given a dataframe df consisting of multiple columns:

Col1   Col2   Col3   Col4   Col5   Col6
   4      2      5      3      4      1
   8      3      9      7      4      5
   1      3      6      7      4      7

I want to apply a function func for a group of columns

df.apply(lambda x: func(x[['Col1', 'Col2', 'Col3']]), axis=1)

This works fine as expected. However, using

df.apply(lambda x: func(x.iloc[:,0:3]), axis=1)  

I get the following error:

IndexingError: ('Too many indexers', u'occurred at index 0')

Since I want to automate the function using a loop in groups of three columns I would prefer using pandas iloc or ix as an indexing method.

Can someone please explain this error?

Upvotes: 2

Views: 3428

Answers (1)

jezrael
jezrael

Reputation: 863166

You need remove first : in iloc, because working with Series in apply, not with DataFrame:

print (df.apply(lambda x: func(x.iloc[0:3]), axis=1))

Test:

def func(x):
    return x.sum()

print (df.apply(lambda x: func(x[['Col1', 'Col2', 'Col3']]), axis=1))
0    11
1    20
2    10
dtype: int64

print (df.apply(lambda x: func(x.iloc[0:3]), axis=1))
0    11
1    20
2    10
dtype: int64

You can also check it by print (print return nothing, so in output are None):

print (df.apply(lambda x: print(x.iloc[0:3]), axis=1))
dtype: int64
Col1    4
Col2    2
Col3    5
Name: 0, dtype: int64
Col1    8
Col2    3
Col3    9
Name: 1, dtype: int64
Col1    1
Col2    3
Col3    6
Name: 2, dtype: int64
0    None
1    None
2    None

Upvotes: 3

Related Questions