python - Pandas: How to use apply function to multiple columns -
i have problems pandas apply function, when using multiple columns following dataframe
df = dataframe ({'a' : np.random.randn(6), 'b' : ['foo', 'bar'] * 3, 'c' : np.random.randn(6)})
and following function
def my_test(a, b): return % b
when try apply function :
df['value'] = df.apply(lambda row: my_test(row[a], row[c]), axis=1)
i error message:
nameerror: ("global name 'a' not defined", u'occurred @ index 0')
i not understand message, defined name properly.
i highly appreciate on issue
update
thanks help. made indeed syntax mistakes code, index should put ''. have still same issue using more complex function such as:
def my_test(a): cum_diff = 0 ix in df.index(): cum_diff = cum_diff + (a - df['a'][ix]) return cum_diff
thank you
seems forgot ''
of string.
in [43]: df['value'] = df.apply(lambda row: my_test(row['a'], row['c']), axis=1) in [44]: df out[44]: b c value 0 -1.674308 foo 0.343801 0.044698 1 -2.163236 bar -2.046438 -0.116798 2 -0.199115 foo -0.458050 -0.199115 3 0.918646 bar -0.007185 -0.001006 4 1.336830 foo 0.534292 0.268245 5 0.976844 bar -0.773630 -0.570417
btw, in opinion, following way more elegant:
in [53]: def my_test2(row): ....: return row['a'] % row['c'] ....: in [54]: df['value'] = df.apply(my_test2, axis=1)
Comments
Post a Comment