获取带有条件的数据帧的下一行

时间:2019-05-21 09:23:36

标签: python pandas dataframe

我想通过设置一个数据帧满足的条件从数据帧中提取下一行值。

我得到了这个数据框:
在:
df = pd.DataFrame.from_csv(file_path) df['Stock Name'] = stock_symbol print(df.head()) df = df.reset_index()
出:

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

我现在得到了以下代码:
在:
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)
出:

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

现在,我想获取“ data_from_yahoo”行之后的行,在这种情况下,它将是行5611。 问题出在我认为的data_from_yahoo_next_day=df.loc[df['Date'] == ddate].shift(1)中。

1 个答案:

答案 0 :(得分:1)

我相信您需要:

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