bikoman57
bikoman57

Reputation: 126

getting the next row of a data frame with a condition

I want to pull the next row value from a data frame by setting a condition it will meet.

I got this data frame:
in:
df = pd.DataFrame.from_csv(file_path) df['Stock Name'] = stock_symbol print(df.head()) df = df.reset_index()
out:

Date   Open    High      Low     ...      Adj Close   Volume  Stock Name                                     ...
1996-12-12 1.81250 1.8125 1.68750 ... 0.743409 1984400 CALM 1996-12-13 1.71875 1.8125 1.65625 ... 0.777510 996800 CALM 1996-12-16 1.81250 1.8125 1.71875 ... 0.750229 122000 CALM 1996-12-17 1.75000 1.8125 1.75000 ... 0.774094 239200 CALM 1996-12-18 1.81250 1.8125 1.75000 ... 0.791151 216400 CALM

I got this code now:
in:
data_from_yahoo=df.loc[df['Date'] == ddate] data_from_yahoo_next_day=df.loc[df['Date'] == ddate].shift(1) print(data_from_yahoo) print(data_from_yahoo_next_day)
out:

Date       Open  High     ...      Adj Close  Volume  Stock Name
5610 2019-04-01  46.700001  47.0     ...      42.987827  846900        CALM
Date  Open  High  Low  Close  Adj Close  Volume Stock Name
5610  NaT   NaN   NaN  NaN    NaN        NaN     NaN        NaN

now I want to get the row that is after the "data_from_yahoo" row, in this case, it would be row 5611. the problem is in the data_from_yahoo_next_day=df.loc[df['Date'] == ddate].shift(1) i think.

Upvotes: 1

Views: 1034

Answers (1)

jezrael
jezrael

Reputation: 862521

I believe you need:

data_from_yahoo_next_day = df[df['Date'].shift(1) == ddate]

Upvotes: 2

Related Questions