Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select range in Pandas using a row

Tags:

python

pandas

I have a Pandas dataframe.

I have in another process selected a row from that dataframe.

At another method, I now need to select a range from that dataframe where the row is and going back 55 rows, if there is so many.

Here is some pseudo code, hope it helps:

df = DataFrame from csv

row = df[3454]

index = row.index
start = max(0, index - 55)
end = max(1, index)
dfRange = df[start:end]
like image 870
Tjorriemorrie Avatar asked Sep 05 '14 08:09

Tjorriemorrie


People also ask

How do you find the range of values in pandas?

numpy. ptp() is doing exactly what you want: Range of values (maximum - minimum) along an axis. The name of the function comes from the acronym for 'peak to peak'.


1 Answers

This should do it

integer_location = np.where(df.index == 3454)[0][0]
start = max(0, integer_location - 55)
end = max(1, integer_location)
dfRange = df.iloc[start:end]

This link has more info Getting the integer index of a Pandas DataFrame row fulfilling a condition?

like image 124
user1827356 Avatar answered Oct 24 '22 20:10

user1827356