Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I select n rows preceding an index row in a DataFrame?

I have a DataFrame and am trying to select a row (given a particular index) and the n rows preceding it.

I've tried something like:

last_10 = self.market_data.iloc[index:-10]

But this appears to give everything from the index up until the end of the dataframe minus 10 rows.

What I'd like to happen is to return the row specified by index and the 10 rows preceding it

like image 210
Shamoon Avatar asked Sep 20 '25 11:09

Shamoon


2 Answers

If general index like DatetimeIndex use DataFrame.iloc with Index.get_loc for position of val:

print (market_data)
            val
Date           
1900-01-01  2.0
1900-01-02  3.0
1900-01-03  5.1
1900-01-04  5.0
1900-01-05  6.0
1900-01-06  7.0
1900-01-07  3.0

n = 3
val = '1900-01-04'
pos = market_data.index.get_loc(val)
last_10 = market_data.iloc[pos-n+1:pos+1]
print (last_10)
            val
Date           
1900-01-02  3.0
1900-01-03  5.1
1900-01-04  5.0

If RangeIndex - get 3 values before index 4 use DataFrame.loc:

print (market_data)
         Date  val
0  1900-01-01  2.0
1  1900-01-02  3.0
2  1900-01-03  5.1
3  1900-01-04  5.0
4  1900-01-05  6.0
5  1900-01-06  7.0
6  1900-01-07  3.0

n = 3
val = 4
last_10 = market_data.loc[val-n+1:val]
print (last_10)
         Date  val
2  1900-01-03  5.1
3  1900-01-04  5.0
4  1900-01-05  6.0
like image 174
jezrael Avatar answered Sep 21 '25 23:09

jezrael


Use this:

n = 10
last_10 = self.market_data.iloc[index-n:index+1]

When slicing arrays, Python returns everything up until the last index, so you need to add one to include it.

like image 32
Nathaniel Avatar answered Sep 22 '25 00:09

Nathaniel