How to get the index of a Pandas DataFrame Row

I know how to find an item in a DataFrame using logic, but I was curious is there a way to return the index value of a row from there.

lets say I wanted to first find the row with the name “Jane Doe” I could use something like this:

df[(df.name == 'Jane Doe')]

This would return the entire row for ‘Jane Doe’

but what if I just wanted to return the index for this row and save it to a variable.

my other part of this question is, is there any way to edit a row? The lesson didn’t go over that and I was curious!

You could get the index from there by addressing df.index, e.g. df.index[conditional test]. Bear in mind that there could be multiple matches.

If you wanted to edit the row then you could use df.loc[index] to access that row (be careful without multiple returns).

2 Likes

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.