Search code examples
pythonexcelpandasxlsxxlsxwriter

Index names of dataframe not getting printed in the output of xlsx writer


My dataframe upi looks like:

               triggered_value  triggered_gnv   
triggered_value 2                 3
triggered_gnv   10                 5

this is a transpose() output

I am using the following to output this into excel

!pip install xlsxwriter
import io
import boto3
import xlsxwriter
bucket = 'adhoc-query-data'
filepath = xxx
with io.BytesIO() as output:
    with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
        upi.to_excel(writer,sheet_name='reminder_experiment',startrow=2,index=False)
        worksheet = writer.sheets['reminder_experiment']
        worksheet.write(0, 0, 'Updated on:')
        worksheet.write(0, 1, f"{datetime.today().strftime('%Y-%m-%d')}")
        
    data = output.getvalue()
s3 = boto3.resource('s3')
s3.Bucket(bucket).put_object(Key=filepath, Body=data)

but in my excel sheet the output does not include my index / row titles which are on the left. It only prints the column names

How can i write the dataframe exactly to excel?


Solution

  • It is probably because the index is turned off here:

    upi.to_excel(..., index=False)
    

    You can either set it to True or omit it, since it is the default.