Python using lambda to use pd.DataFrame instead for a nested loop, perhaps?

I am trying to avoid a nested loop in python here, using lambda apply to create a new column using this argument below:

from pandas import *
import pandas as pd    
df = pd.DataFrame((np.random.rand(100, 4)*100), columns=list('ABCD'))
df['C'] = df.apply(lambda A,B: A+B)

TypeError: ('() takes exactly 2 arguments (1 given)', u'occurred at index A ')

Obviously, this does not give any recommendations?

+4
source share
1 answer

Do you want to add column Aand column Band save the result in C? Then you can make it easier:

df.C = df.A + df.B

@EdChum , apply 0, ( 1 ):

>>> df.apply(lambda s: s)[:3]
           A          B          C          D
0  57.890858  72.344298  16.348960  84.109071
1  85.534617  53.067682  95.212719  36.677814
2  23.202907   3.788458  66.717430   1.466331

:

>>> df.apply(lambda s: s[0] + s[1])
A    143.425475
B    125.411981
C    111.561680
D    120.786886
dtype: float64

axis=1:

>>> df.apply(lambda s: s[0] + s[1], axis=1)
0     130.235156
1     138.602299
2      26.991364
3     143.229523
...
98    152.640811
99     90.266934

, :

>>> (df.apply(lambda s: s[0] + s[1], axis=1) == 
     df.apply(lambda s: s['A'] + s['B'], axis=1))
0     True
1     True
2     True
3     True
...
98    True
99    True
+7

All Articles