Search code examples
pythonpandasshift

How to query in dataframe combined with .shift()


I have a temporary df like

tmp = df.loc[(df['Home Team'] == team) | (df['Away Team']== team)]

I would like to get a shifted value of a specific row. I tried

tmp.loc[(df['Primary Key'] == key)].shift(1)

but this obviously reduces the df first, so shift(1) will result in a non existing row. Any help to achieve this?


Solution

  • You can first shifting column and then compare:

    tmp.loc[(tmp['Primary Key'].shift(1) == key)]