Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating through DataFrame row index in reverse order [duplicate]

Tags:

python

pandas

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?

like image 564
sashkello Avatar asked Apr 22 '13 05:04

sashkello


People also ask

How do I reverse the order of rows in pandas DataFrame?

Reversing the rows of a data frame in pandas can be done in python by invoking the loc() function. The panda's dataframe. loc() attribute accesses a set of rows and columns in the given data frame by either a label or a boolean array.

How do I iterate through a row in pandas DataFrame?

DataFrame. iterrows() method is used to iterate over DataFrame rows as (index, Series) pairs. Note that this method does not preserve the dtypes across rows due to the fact that this method will convert each row into a Series .

What is faster than Iterrows?

While slower than apply , itertuples is quicker than iterrows , so if looping is required, try implementing itertuples instead. Using map as a vectorized solution gives even faster results.


1 Answers

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)
like image 55
root Avatar answered Nov 14 '22 23:11

root