3

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?

Fatafim
  • 287
  • 2
  • 13
  • 3
    maybe insert the NaN. alternatively, you could do an outer merge back to the original dataframe : ``A.merge(A.shift(1), on='A', how = 'outer')`` ... the efficiency/simplicity of that though is another question – sammywemmy Oct 24 '21 at 23:02
  • thank you so much for the idea! nevertheless I'm actually dealing with someone else's code, so I'd like to keep it as simple as possible. is there any other prebuilt way? – Fatafim Oct 24 '21 at 23:04
  • No there is not a prebuilt way (in base `pandas` perhaps there exists some addon that supports this) to expand a dataframe on shift. Your options are to merge/join back. Append concat together. Or reindex to include the new values. – Henry Ecker Oct 24 '21 at 23:06
  • Seems like the adding a row idea was used here to just add another row before shifting [How to shift a column in Pandas DataFrame without losing value](https://stackoverflow.com/q/36042804/15497888) I think `df.reindex(index=df.index.union([df.index.max() + 1])).shift()` would be my approach though. – Henry Ecker Oct 24 '21 at 23:09
  • 2
    Oh also increment index then reindex [Panda Dataframe - Shifting rows down and maintaining data](https://stackoverflow.com/q/44192167/15497888) – Henry Ecker Oct 24 '21 at 23:11

2 Answers2

3

Try this:

df.shift(1).append(
    df.iloc[[-1]]).reset_index(drop = True)
AfterFray
  • 1,751
  • 3
  • 17
  • 22
0

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
barker
  • 1,005
  • 18
  • 36