Home > OS >  Pandas shifted index column
Pandas shifted index column

Time:06-21

I got a filtered pandas dataframe and I would like create a new column that is based on the shifted index value.

Point
124 12
559 1
717 12

Goal:

Point Pre_Index
124 12 559
559 1 717
717 12 NaN

How could I do that? Thanks in advance.

CodePudding user response:

Try this:

data['index'] = data.index
data['Pre_Index'] = data['index'].shift(-1)

CodePudding user response:

IIUC, you can use Series.shift(-1)

df['Pre_Index'] = df['Index'].shift(-1)
# or
df['Pre_Index'] = df.index.shift(-1)
  • Related