Python Forum

Full Version: How to shift data frame rows of specified column
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)