Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shift column in pandas dataframe up by one?

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?

like image 564
natsuki_2002 Avatar asked Nov 20 '13 12:11

natsuki_2002


People also ask

How do I move a column to a specific position in pandas?

The basic idea to move a column in a pandas dataframe is to remove the column from its current place and insert it in the desired position. The pandas library offers many useful functions such as pop() and insert(). We will make use of these two functions to manipulate with our dataframe.

What does shift () do in pandas?

shift() function Shift index by desired number of periods with an optional time freq. This function takes a scalar parameter called the period, which represents the number of shifts to be made over the desired axis. This function is very helpful when dealing with time-series data.

How do I rearrange column values in pandas?

You can change the order of columns in the pandas dataframe using the df. reindex() method.


1 Answers

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 
like image 199
Wouter Overmeire Avatar answered Sep 28 '22 00:09

Wouter Overmeire