128

I've got a pandas dataframe. I want to 'lag' one of my columns. Meaning, for example, shifting the entire column 'gdp' up by one, and then removing all the excess data at the bottom of the remaining rows so that all columns are of equal length again.

df =
    y  gdp  cap
0   1    2    5
1   2    3    9
2   8    7    2
3   3    4    7
4   6    7    7

df_lag =
    y  gdp  cap
0   1    3    5
1   2    7    9
2   8    4    2
3   3    7    7

Anyway to do this?

martineau
  • 119,623
  • 25
  • 170
  • 301
natsuki_2002
  • 24,239
  • 21
  • 46
  • 50

6 Answers6

221
In [44]: df['gdp'] = df['gdp'].shift(-1)

In [45]: df
Out[45]: 
   y  gdp  cap
0  1    3    5
1  2    7    9
2  8    4    2
3  3    7    7
4  6  NaN    7

In [46]: df[:-1]                                                                                                                                                                                                                                                                                                               
Out[46]: 
   y  gdp  cap
0  1    3    5
1  2    7    9
2  8    4    2
3  3    7    7
cs95
  • 379,657
  • 97
  • 704
  • 746
Wouter Overmeire
  • 65,766
  • 10
  • 63
  • 43
11

shift column gdp up:

df.gdp = df.gdp.shift(-1)

and then remove the last row

PeacefulBY
  • 163
  • 8
5

First shift the column:

df['gdp'] = df['gdp'].shift(-1)

Second remove the last row which contains an NaN Cell:

df = df[:-1]

Third reset the index:

df = df.reset_index(drop=True)
Jonas Freire
  • 165
  • 3
  • 4
5

Time is going. And current Pandas documentation recommend this way:

 df.loc[:, 'gdp'] = df.gdp.shift(-1)
Vasyl Kolomiets
  • 365
  • 8
  • 20
4

To easily shift by 5 values for example and also get rid of the NaN rows, without having to keep track of the number of values you shifted by:

d['gdp'] = df['gdp'].shift(-5)
df = df.dropna()
ArmandduPlessis
  • 929
  • 1
  • 8
  • 14
3
df.gdp = df.gdp.shift(-1) ## shift up
df.gdp.drop(df.gdp.shape[0] - 1,inplace = True) ## removing the last row
niemmi
  • 17,113
  • 7
  • 35
  • 42
Bilal Mahmood
  • 169
  • 1
  • 3