Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 427
user_dhrn Avatar asked Feb 05 '18 19:02

user_dhrn


People also ask

How do I select top and rows in Pandas?

Use pandas. DataFrame. head(n) to get the first n rows of the DataFrame. It takes one optional argument n (number of rows you want to get from the start).

How will you get the top 2 rows from a DataFrame in Pandas?

pandas.DataFrame.head() In Python's Pandas module, the Dataframe class provides a head() function to fetch top rows from a Dataframe i.e. It returns the first n rows from a dataframe.

How do you get Top 100 rows in Pandas?

You can use df. head() to get the first N rows in Pandas DataFrame. Alternatively, you can specify a negative number within the brackets to get all the rows, excluding the last N rows.


1 Answers

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
like image 184
cs95 Avatar answered Oct 04 '22 20:10

cs95