Home > database >  I want to add a copy of a column but with an added row
I want to add a copy of a column but with an added row

Time:08-11

I have a table (pivot table) like this:

value       B   N   S      date   
date                    
2020-12-31  1   11  0   2020-12-31  
2021-01-01  3   80  0   2021-01-01  
2021-01-02  4   99  0   2021-01-02  
2021-01-03  3   78  0   2021-01-03  
2021-01-04  0   50  0   2021-01-04

and now I want to make it like this by adding a new column:

value       B   N   S      date   yesterday_B
date                    
2020-12-31  1   11  0   2020-12-31  NaN
2021-01-01  3   80  0   2021-01-01  1
2021-01-02  4   99  0   2021-01-02  3
2021-01-03  3   78  0   2021-01-03  4
2021-01-04  0   50  0   2021-01-04  3

is there and pandas function for it? what should I do?? (after all, I want to use it as a feature (consider B and the value for its previous day) in the machine learning model. can I do it without adding a new column?

CodePudding user response:

Use Series.shift()

df['yesterday_B'] = df['B'].shift()
  • Related