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?
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.
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.
You can change the order of columns in the pandas dataframe using the df. reindex() method.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With