pandas shift a column forwards in matlab

In Python, pandas is a separate library from Matlab. If you want to shift a column forwards in pandas, you can use the shift() method. Here's an example:

main.py
import pandas as pd

data = {'col1': [1, 2, 3, 4, 5], 'col2': [6, 7, 8, 9, 10]}
df = pd.DataFrame(data)

# shift column 'col1' forwards by one position
df['col1'] = df['col1'].shift(1)

print(df)
196 chars
10 lines

This will output the following DataFrame:

main.m
   col1  col2
0   NaN     6
1   1.0     7
2   2.0     8
3   3.0     9
4   4.0    10
84 chars
7 lines

Note that shifting a column forwards will result in the first value becoming NaN. If you want to shift the column backwards, you can use a negative integer argument to the shift() method.

related categories

gistlibby LogSnag