Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

512
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda