Shamoon
Shamoon

Reputation: 43501

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

Upvotes: 4

Views: 1352

Answers (3)

Pascal Sigel
Pascal Sigel

Reputation: 11

I think that te most generic way is by searching up to the index, and then search the last 10 of the previous search:

just:

self.market_data.loc[:index].iloc[-10]

that because your index could be a timestamp.

Upvotes: 1

jezrael
jezrael

Reputation: 862611

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

Upvotes: 2

Nathaniel
Nathaniel

Reputation: 3290

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.

Upvotes: 2

Related Questions