0

I need help..i'm trying to create a column where the new column is a previous date taken from date column..The result should resemble something like this..

description    date               count  previous date
abc               2010-01-05        1        NaT
khl                2010-01-08        3      2010-01-05 
nop               2010-01-10        6      2010-01-08

This is what I have done and my for loop seems to produce no results, it keeps on running forever I tried it many times.

skus=prod_by_date.Description.value_counts()

#Create empty dataframe
new_df= pd.DataFrame()

`for sku in skus:
    a=prod_by_date[prod_by_date.Description==skus[0]]
    a['previous_date']=a['date'].shift(1)
    new_df=pd.concat([new_df,a],axis=0)`
Mr. T
  • 11,960
  • 10
  • 32
  • 54
D Alam
  • 23
  • 5
  • 1
    Your example and your code imply different aims. Are you sure you don't just look for `df['previous_date']=df['date'].shift()`? Your code implies some groupwise date shift but this has not been specified in your task description. – Mr. T Mar 06 '21 at 09:46
  • I'm fairly new to coding..I may have done something wrong in code, what changes do I need to make in code so that I get the desired results..Another thing can the results be obtained from any other way apart from using for loop...I get so confused doing loops and functions – D Alam Mar 07 '21 at 07:03
  • We don't know what the desired outcome is, and you do not provide a [reproducible example](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). – Mr. T Mar 07 '21 at 10:28
  • I already showed the desired output in the tabular format above. – D Alam Mar 07 '21 at 11:05
  • In this case, I showed you already how to get the desired output for a dataframe called `df`. – Mr. T Mar 07 '21 at 11:13

0 Answers0