Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iterate through a dataframe bottom to top

Tags:

python

pandas

I have a dataframe rollArray which looks like:

rollArray       mavStart     mavEnd buildupStart buildupEnd insampleStart insampleEnd  \
0  2007-01-19 2008-01-04   2008-01-07 2008-02-01    2008-02-04  2008-04-11   
1  2007-01-12 2007-12-28   2007-12-31 2008-01-25    2008-01-28  2008-04-04   
2  2007-01-05 2007-12-21   2007-12-24 2008-01-18    2008-01-21  2008-03-28   
3  2006-12-29 2007-12-14   2007-12-17 2008-01-11    2008-01-14  2008-03-21   
4  2006-12-22 2007-12-07   2007-12-10 2008-01-04    2008-01-07  2008-03-14   
5  2006-12-15 2007-11-30   2007-12-03 2007-12-28    2007-12-31  2008-03-07   
6  2006-12-08 2007-11-23   2007-11-26 2007-12-21    2007-12-24  2008-02-29   
7  2006-12-01 2007-11-16   2007-11-19 2007-12-14    2007-12-17  2008-02-22   
8  2006-11-24 2007-11-09   2007-11-12 2007-12-07    2007-12-10  2008-02-15   
9  2006-11-17 2007-11-02   2007-11-05 2007-11-30    2007-12-03  2008-02-08   
10 2006-11-10 2007-10-26   2007-10-29 2007-11-23    2007-11-26  2008-02-01   
11 2006-11-03 2007-10-19   2007-10-22 2007-11-16    2007-11-19  2008-01-25   
12 2006-10-27 2007-10-12   2007-10-15 2007-11-09    2007-11-12  2008-01-18   
13 2006-10-20 2007-10-05   2007-10-08 2007-11-02    2007-11-05  2008-01-11   
14 2006-10-13 2007-09-28   2007-10-01 2007-10-26    2007-10-29  2008-01-04   

   outsampleStart outsampleEnd  
0      2008-04-14   2008-04-18  
1      2008-04-07   2008-04-11  
2      2008-03-31   2008-04-04  
3      2008-03-24   2008-03-28  
4      2008-03-17   2008-03-21  
5      2008-03-10   2008-03-14  
6      2008-03-03   2008-03-07  
7      2008-02-25   2008-02-29  
8      2008-02-18   2008-02-22  
9      2008-02-11   2008-02-15  
10     2008-02-04   2008-02-08  
11     2008-01-28   2008-02-01  
12     2008-01-21   2008-01-25  
13     2008-01-14   2008-01-18  
14     2008-01-07   2008-01-11 

I would like to iterate through the dataframe from the bottom row to top row, so doing the opposite to:

for index, row in rollArray.iterrows():
    roll = rollArray.index[index]
    print('Roll ', roll)

which would produce the output 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14. The output that I am looking to return is 14,13,12,11,10,9,8,7,6,5,4,3,2,1,0. Is this possible?

like image 887
Stacey Avatar asked Jan 05 '23 02:01

Stacey


2 Answers

For me works add [::-1]:

for index, row in rollArray[::-1].iterrows():
    roll = rollArray.index[index]
print('Roll ', roll)
Roll  14
Roll  13
Roll  12
Roll  11
Roll  10
Roll  9
Roll  8
Roll  7
Roll  6
Roll  5
Roll  4
Roll  3
Roll  2
Roll  1
Roll  0

Or iloc[::-1]:

for index, row in rollArray.iloc[::-1].iterrows():
    roll = rollArray.index[index]
print('Roll ', roll)
Roll  14
Roll  13
Roll  12
Roll  11
Roll  10
Roll  9
Roll  8
Roll  7
Roll  6
Roll  5
Roll  4
Roll  3
Roll  2
Roll  1
Roll  0
like image 112
jezrael Avatar answered Jan 06 '23 16:01

jezrael


Reindex your dataframe

rollArray.reindex(index=rollArray.index[::-1])

or:

rollArray = rollArray.iloc[::-1]

Then

for index, row in rollArray.iterrows():
    roll = rollArray.index[index]
    print('Roll ', roll)
like image 25
Jean S Avatar answered Jan 06 '23 15:01

Jean S