Python data frame Export to csv with Quotation marks (")
Asked Answered
N

1

6

How to export python dataframe to csv with double quotes. I have tried with below code but its not coming in output file. I need results like "column1","column2",column3"... Please help.

exportPath=exportPath+'\\data2Upload.csv'
header=['Country','Indicator','Unit','Frequency','Date','Value']
data.to_csv(exportPath,columns=header,sep=',',quotechar='"',index=False)
Nagano answered 1/5, 2017 at 13:23 Comment(0)
P
8

You can tell pandas to quote everything by default using the csv.QUOTE_ALL property:

data.to_csv(exportPath, columns=header,sep=",",quotechar='"',index=False,
            quoting=csv.QUOTE_ALL)

Official Python docs: https://docs.python.org/2/library/csv.html#csv.QUOTE_ALL

Plump answered 1/5, 2017 at 13:26 Comment(1)
Thanks. Note: csv is required: import csv.Dynel

© 2022 - 2024 — McMap. All rights reserved.