Winston
Winston

Reputation: 1428

Pandas shifted index column

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.

Upvotes: 0

Views: 42

Answers (2)

Ynjxsjmh
Ynjxsjmh

Reputation: 30032

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

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

Upvotes: 1

olegr
olegr

Reputation: 2019

Try this:

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

Upvotes: 1

Related Questions