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]
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'.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With