I wrote this code for convert a binary to date and create new data frame df. But it have a problem.
df2 only declares the last value of the 'for' loop. So The last data df is missing.
How can it fix? Thank you.
start_date = '2020-12-13'
i = 0
df1 = pd.DataFrame({'id':data1.id[0],'Statut':data1.statut[0],'Date send': data1.date_send[0],'Day': pd.date_range(start_date, periods=len(data1.binairy[0]), freq='D'),'Code': list(data1.binairy[0]),'Commentaire ':data1.commentaire[0]})
for i in range(1,len(data1)):
df2 = pd.DataFrame({'id':data1.id[i],'Statut':data1.statut[i],'Date send': data1.date_send[i],'Day': pd.date_range(start_date, periods=len(data1.binairy[i]), freq='D'),'Code': list(data1.binairy[i]),'Commentaire ':data1.commentaire[i]})
df = pd.concat([df1,df2], ignore_index=True)
df
CodePudding user response:
You're initiating your df
variable inside the loop hence after one loop finished and another comes, the new loop will re-creating the df
variable. I would suggest you to just declare df outside of the for
loop. For example:
df1 = pd.DataFrame({'id':data1.id[0],'Statut':data1.statut[0],
'Date send': data1.date_send[0],
'Day': pd.date_range(start_date, periods=len(data1.binairy[0]), freq='D'),
'Code': list(data1.binairy[0]),'Commentaire ':data1.commentaire[0]})
df = pd.DataFrame({'id':data1.id[0],'Statut':data1.statut[0],
'Date send': data1.date_send[0],
'Day': pd.date_range(start_date, periods=len(data1.binairy[0]), freq='D'),
'Code': list(data1.binairy[0]),'Commentaire ':data1.commentaire[0]})
for i in range(1,len(data1)):
df2 = pd.DataFrame({'id':data1.id[i],'Statut':data1.statut[i],
'Date send': data1.date_send[i],
'Day': pd.date_range(start_date, periods=len(data1.binairy[i]), freq='D'),
'Code': list(data1.binairy[i]),'Commentaire ':data1.commentaire[i]})
# Some modification here, concat df and df2 instead df1 vs df2
df = pd.concat([df,df2], ignore_index=True)
Or you can just replace the df with df1 and concat df1 with df2 like this:
df1 = pd.DataFrame({'id':data1.id[0],'Statut':data1.statut[0],
'Date send': data1.date_send[0],
'Day': pd.date_range(start_date, periods=len(data1.binairy[0]), freq='D'),
'Code': list(data1.binairy[0]),'Commentaire ':data1.commentaire[0]})
for i in range(1,len(data1)):
df2 = pd.DataFrame({'id':data1.id[i],'Statut':data1.statut[i],
'Date send': data1.date_send[i],
'Day': pd.date_range(start_date, periods=len(data1.binairy[i]), freq='D'),
'Code': list(data1.binairy[i]),'Commentaire ':data1.commentaire[i]})
df1 = pd.concat([df1,df2], ignore_index=True)