Python Forum
How to shift data frame rows of specified column - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: How to shift data frame rows of specified column (/thread-28501.html)



How to shift data frame rows of specified column - Mekala - Jul-21-2020

Hi,
I have dataframe, I wan to define new column same as Column1, and re-arrange the new columns rows as:
new_col rows 0-end should be replaced with first column row1-end

import pandas as pd
df = pd.DataFrame({'Col1': [1, 2, 3, 4, 5],'Col2': [6, 7, 8, 9, 10],
                   'Col3': [11, 12, 13, 14, 15]})

df['new_col'] = df['Col1']
df[0:-1,'new_col']=df.ix[1:,'Col1']
TypeError: unhashable type: 'slice'

my desired output:
Col1  Col2 Col3 new_col
1     6    11   2
2     7    12   3 
3     8    13   4
4     9    14   5
5     10   15   5

I use this way, and it works:
df['new_col']=df['new_col'].shift(-1)