Paul H Paul H - 11 months ago 54
Python Question

Pandas Dataframe apply() method provides a row object, but how do you access the index value

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.

Answer Source

I believe what you want is this:

def my_test(row):
   return "{}.{}".format(, row['b'])