2

In pandas, is it possible to reference the row number for a function. I am not talking about .iloc.

iloc takes a location i.e. a row number and returns a dataframe value.

I want to access the location number in the dataframe.

For instance, if the function is in the cell that is 3 rows down and 2 columns across, I want a way to return the integer 3. Not the entry that is in that location.

Thanks.

2 Answers2

1

Consider the dataframe df

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

df

enter image description here

And the row row

row = df.ix['a']

row

c    1
d    2
Name: a, dtype: int64

There is nothing about this row that indicates that it was the first row of df. That information is essentially lost... sort of.

You can take @jezrael's advice and use get_loc

df.index.get_loc(row.name)

0

But this only works if your index is unique.

Your only other option is to track its position when you get it. The best way to do that is:

Use enumerate on df.iterrows()

for i, (idx, row) in enumerate(df.iterrows()):
    print "row position: {:>3d}\nindex value: {:>4s}".format(i, idx)
    print row, '\n'

row position:   0
index value:    a
c    1
d    2
Name: a, dtype: int64 

row position:   1
index value:    b
c    3
d    4
Name: b, dtype: int64 
piRSquared
  • 285,575
  • 57
  • 475
  • 624
0

Sorry I couldnt add a code sample but Im on my phone. piRSquared confirmed my fears when he said the info is lost. I guess ill have to do a loop everytime or add a column with numbers ( that will get scrambled if i sort them : / ).

Thanks everyone.