4

My original Dataframe (df):

    column1  column2
0   1        a
1   2        b
2   3        c
3   4        d
4   5        e
5   6        f

I want to shift the values down by 6 like so:

    column1 column2
0       
1       
2       
3       
4       
5       
6   1        a
7   2        b
8   3        c
9   4        d
10  5        e
11  6        f

When I use df = df.shift(6), I end up loosing data.

I found this post (How to shift a column in Pandas DataFrame without losing value) but it only seems to work if the values are shifted down by 1.

How can I shift multiple spots down while retaining the data?

2 Answers 2

2

You can try

df.index = df.index+6
df = df.reindex(np.arange(12))

    column1 column2
0   NaN     NaN
1   NaN     NaN
2   NaN     NaN
3   NaN     NaN
4   NaN     NaN
5   NaN     NaN
6   1.0     a
7   2.0     b
8   3.0     c
9   4.0     d
10  5.0     e
11  6.0     f
Sign up to request clarification or add additional context in comments.

2 Comments

Good thinking!!
Thanks, I ended up substituting 'df = df.reindex(np.arange(12))' with 'df = df.reindex(np.arange(max(df.index)+1))' to allow the dataset to be dynamic (which I think is working).
0

shift doesn't change the shape of the data frame, what you are looking for is adding some extra rows in front of the data frame, for the simplest case you are showing (i.e. index is consisted of integers) you can manipulate the index as follows to achieve what you need:

df.index = df.index + 6
df.reindex(range(6) + df.index.tolist())    

#column1    column2
#0   NaN    NaN
#1   NaN    NaN
#2   NaN    NaN
#3   NaN    NaN
#4   NaN    NaN
#5   NaN    NaN
#6   1.0    a
#7   2.0    b
#8   3.0    c
#9   4.0    d
#10  5.0    e
#11  6.0    f

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.