I am new to Panda's and DataFrames and have run into an issue. The DataFrame.apply() method passes a row parameter to the provided function. However I can't seem to find out what the index value corresponding to that row is from this row parameter.
An example
df = DataFrame ({'a' : np.random.randn(6),
'b' : ['foo', 'bar'] * 3,
'c' : np.random.randn(6)})
df = df.set_index('a')
def my_test2(row):
return "{}.{}".format(row['a'], row['b'])
df['Value'] = df.apply(my_test2, axis=1)
Yields a KeyError
KeyError: ('a', u'occurred at index -1.16119852166')
The problem is that the row['a'] in the my_test2 method fails. If I don't do the df.set_index('a') it works fine, but I do want to have an index on a.
I tried duplicating column a (once as index, and once as a column) and this works, but this just seems ugly and problematic.
Any ideas on how to get the corresponding index value given the row object?
Many thanks in advance.
df.index(b)instead ofdf = df.set_index("b"), which is why you're getting aNameErrorinstead of aKeyError. (Fixing that won't solve your problem, but it will make this question make more sense..)