简体   繁体   中英

Select N rows above and below a specific row in pandas

I have this data frame and I want to select 10 rows before and after on a specific column. I have reached up to this point but I was wondering how to make it more elegant in a lambda python expression as I need to run this on a loop 10 thousand times.

import pandas as pd

df = pd.DataFrame(data=np.random.rand(90),
     index=pd.date_range('2015-01-01','2015-03-31'),columns=['A'])

I have reached to this as an solution in progress:

10 observations before and after:

df.loc['2015-01-17':].head(11)[1:11].transpose()   ! before
df.loc[:'2015-01-17'].tail(11)[0:10].transpose()   ! after

So, how can I make this is in a loop with a lambda expression and having not only one index but two indexes ?

Really simple using index.get_loc . Get the index of the label, and slice accordingly.

idx = df.index.get_loc('2015-01-17')
df.iloc[idx - 10 : idx + 10]

                   A
2015-01-07  0.262086
2015-01-08  0.836742
2015-01-09  0.094763
2015-01-10  0.133500
2015-01-11  0.285372
2015-01-12  0.338112
2015-01-13  0.451852
2015-01-14  0.163001
2015-01-15  0.247186
2015-01-16  0.227053
2015-01-17  0.837647
2015-01-18  0.918334
2015-01-19  0.514731
2015-01-20  0.207688
2015-01-21  0.700314
2015-01-22  0.363784
2015-01-23  0.811346
2015-01-24  0.079030
2015-01-25  0.051900
2015-01-26  0.520310

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM