I want to loop through my data and save every component based data in separate sheet in the same excel workbook in S3 bucket.
Dataframe df looks as below:
Below is my code:
today = datetime.datetime.now().strftime('%m_%d_%Y_%H_%M_%S')
components=["COMP1","COMP2","COMP3"]
filename = 'auto_export_'+today
for comp in components:
df1= df[df['component']==comp]
print(comp)
print(df1)
with io.BytesIO() as output:
with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
df1.append_df_to_excel(writer, sheet_name=comp, index=False)
data = output.getvalue()
s3 = boto3.resource('s3')
s3.Bucket('mybucket').put_object(Key='folder1/'+filename+'.xlsx', Body=data)
This is generating the excel file correctly but writing only COMP3 data into it. It is not writing COMP1 and COMP2 sheets. Any guidance on how to fix this problem?
2
Answers
You are writing each component/dataframe as an excel file to S3, overwriting the previous file. You’re doing everything inside the
for
loop, so you are writing out component 1, then completely obliterating all that work by overwriting it with component 2, then doing the same thing again by overwriting that with component 3.You need to refactor your code like so:
Using a list comp and .groupby: