Reputation: 1
I have a pandas dataframe loaded with end of day data for AAPL from Yahoo Finance. I'm looking for a certain condition in the data to meet (say, when close of a day is near the lows of the day) and after which (say, on 2012-01-01 the condition was met), I would like to immediately see the next few rows (I'm interested in the closing price of AAPL on 2012-01-02 and 2012-01-03 and so on).
I used dataframe query which results in just the row where the condition was matched. I don't know how to extract the rows immediately following the matched condition row.
How do I go about accomplishing this? Is "query" the wrong method?
Thank you, D
Upvotes: 0
Views: 24
Reputation: 18201
Do you know that you'll have exactly one result? Then you can get them more or less by hand as follows:
In [44]: df # Example DataFrame
Out[44]:
c1 c2
0 a 5
1 b 10
2 c 15
In [45]: index = df[df['c1'] == 'b'].index[0] # Get the index of the row of interest.
In [47]: df.iloc[range(index, index + 2)] # Get that row and whatever follows immediately after
Out[47]:
c1 c2
1 b 10
2 c 15
If you have more than one result, iterate over df[df['c1'] == 'b'].index
instead.
Upvotes: 1