I've got a very simple problem. I have a dataframe:
A
0 1.0
1 2.0
2 3.0
and when I do df.shift(1) the data looks like:
A
0 NaN
1 1.0
2 2.0
and I would like it to look like:
A
0 NaN
1 1.0
2 2.0
3 3.0
simply speaking - how do I shift the data without losing the last row?
CodePudding user response:
Try this:
df.shift(1).append(
df.iloc[[-1]]).reset_index(drop = True)
CodePudding user response:
just dup a nan row, then append it to the top:
import pandas as pd
import numpy as np
data = [1.0,2.0,3.0]
df = pd.DataFrame(data, columns = ['A'])
df1 = pd.DataFrame([[np.nan] * len(df.columns)], columns=df.columns)
df = df1.append(df, ignore_index=True)
print(df)
A
0 NaN
1 1.0
2 2.0
3 3.0