Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

503
Views
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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!