Search code examples
pythongoogle-sheets-apigspread

Python/Gspread How can I add column names while creating a worksheet?


#For creating a new-sheet
sheets.add_worksheet(title=sheetName, rows=df.shape[0], cols=df.shape[1])

#For appending
values = df.values.tolist()
sheets.values_append(sheetName, {'valueInputOption': 'USER_ENTERED'}, {'values': values})

It creates a new worksheet but there are no column names in it only the values.


Solution

  • In your script, how about the following modification?

    From:

    values = df.values.tolist()
    

    To:

    values = [df.columns.values.tolist()] + df.values.tolist()
    
    • By this, values has the data including the header row.