8

I know how to iterate through the rows of a pandas DataFrame:

for id, value in df.iterrows():

but now I'd like to go through the rows in reverse order (id is numeric, but doesn't coincide with row number). Firstly I thought of doing a sort on index data.sort(ascending = False) and then running the same iteration procedure, but it didn't work (it seem to still go from smaller id to larger).

How can I accomplish this?

sashkello
  • 17,306
  • 24
  • 81
  • 109

1 Answers1

12

Iterating through a DataFrame is usually a bad idea, unless you use Cython. If you really have to, you can use the slice notation to reverse the DataFrame:

In [8]: import pandas as pd

In [9]: pd.DataFrame(np.arange(20).reshape(4,5))
Out[9]: 
    0   1   2   3   4
0   0   1   2   3   4
1   5   6   7   8   9
2  10  11  12  13  14
3  15  16  17  18  19

In [10]: pd.DataFrame(np.arange(20).reshape(4,5))[::-1]
Out[10]: 
    0   1   2   3   4
3  15  16  17  18  19
2  10  11  12  13  14
1   5   6   7   8   9
0   0   1   2   3   4

In [11]: for row in pd.DataFrame(np.arange(20).reshape(4,5))[::-1].iterrows():
    ...:     print row
    ...:     
(3, 0    15
1    16
2    17
3    18
4    19
Name: 3)
(2, 0    10
1    11
2    12
3    13
4    14
Name: 2)
(1, 0    5
1    6
2    7
3    8
4    9
Name: 1)
(0, 0    0
1    1
2    2
3    3
4    4
Name: 0)
root
  • 76,608
  • 25
  • 108
  • 120