Python Forum
How to shift data frame rows of specified column
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to shift data frame rows of specified column
#1
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to filter data frame dynamically with the columns psahay 0 168 Aug-24-2020, 01:10 PM
Last Post: psahay
  Extracting rows based on condition on one column Robotguy 2 197 Aug-07-2020, 02:27 AM
Last Post: Robotguy
  Dropping Rows From A Data Frame Based On A Variable JoeDainton123 1 269 Aug-03-2020, 02:05 AM
Last Post: scidam
  Filter data based on a value from another dataframe column and create a file using lo pawanmtm 1 291 Jul-15-2020, 06:20 PM
Last Post: pawanmtm
  HELP- DATA FRAME INTO TIME SERIES- BASIC bntayfur 0 235 Jul-11-2020, 09:04 PM
Last Post: bntayfur
  Pandas data frame creation from Kafka Topic vboppa 0 208 Jul-01-2020, 04:23 PM
Last Post: vboppa
  Assigning Column nunique values to another DataFrame column Pythonito 0 264 Jun-25-2020, 05:04 PM
Last Post: Pythonito
  Filter rows by multiple text conditions in another data frame i.e contains strings an Pan 0 458 Jun-09-2020, 06:05 AM
Last Post: Pan
  Pandas - Dynamic column aggregation based on another column theroadbacktonature 0 341 Apr-17-2020, 04:54 PM
Last Post: theroadbacktonature
  Select column between to dates CSV data PythonJD 0 400 Apr-14-2020, 12:22 PM
Last Post: PythonJD

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020