Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

I am trying to access the index of a row in a function applied across an entire DataFrame in Pandas. I have something like this:

df = pandas.DataFrame([[1,2,3],[4,5,6]], columns=['a','b','c'])

>>> df

   a  b  c

0  1  2  3

1  4  5  6

and I'll define a function that access elements with a given row

def rowFunc(row):

    return row['a'] + row['b'] * row['c']

I can apply it like so:

df['d'] = df.apply(rowFunc, axis=1)

>>> df

   a  b  c   d

0  1  2  3   7

1  4  5  6  34

Awesome! Now what if I want to incorporate the index into my function? The index of any given row in this DataFrame before adding d would be Index([u'a', u'b', u'c', u'd'], dtype='object'), but I want the 0 and 1. So I can't just access row.index.

I know I could create a temporary column in the table where I store the index, but I"m wondering if it is sotred in the row object somewhere.

1 Answer

0 votes
by (41.4k points)

To access the index in this case you access the name attribute:

In [182]:

df = pd.DataFrame([[1,2,3],[4,5,6]], columns=['a','b','c'])

def rowFunc(row):

    return row['a'] + row['b'] * row['c']

def rowIndex(row):

    return row.name

df['d'] = df.apply(rowFunc, axis=1)

df['rowIndex'] = df.apply(rowIndex, axis=1)

df

Output:

   a  b  c   d  rowIndex

0  1  2  3   7         0

1  4  5  6  34         1

If you want to learn more about Pandas then visit this Python Course designed by the industrial experts.

Browse Categories

...