Search code examples
pandascsvescapingexport-to-csvquoting

Pandas exporting to_csv() with quotation marks around column names


For some reason I need to output to a csv in this format with quotations around each columns names, my desired output looks like:

"date" "ret"
2018-09-24 0.00013123989025119056

I am trying with

import csv
import pandas as pd

Y_pred.index.name = "\"date\""
Y_pred.name = "\'ret\'"
Y_pred = Y_pred.to_frame()
path = "prediction/Q1/"
try:
    os.makedirs(path)
except:
    pass

Y_pred.to_csv(path+instrument_tmp+"_ret.txt",sep=' ')   

and got outputs like:

"""date""" 'ret'
2018-09-24 0.00013123989025119056

I can't seem to find a way to use quotation to wrap at the columns. Does anyone know how to? Thanks.

My solution: using quoting=csv.QUOTE_NONE together with Y_pred.index.name = "\"date\"", Y_pred.name = "\"ret\""

Y_pred.index.name = "\"date\""
Y_pred.name = "\"ret\""
Y_pred = Y_pred.to_frame()
path = "prediction/Q1/"
try:
    os.makedirs(path)
except:
    pass

Y_pred.to_csv(path+instrument_tmp+"_ret.txt",sep=' ',quoting=csv.QUOTE_NONE)   

and then I get

"date" "ret"
2018-09-24 0.00013123989025119056

Solution

  • IIUC, you can use the quoting argument with csv.QUOTE_NONE

    import csv
    df.to_csv('test.csv',sep=' ',quoting=csv.QUOTE_NONE)
    

    And your resulting csv will look like:

     "date" "ret"
    0 2018-09-24 0.00013123989025119056
    

    Side Note: To facilitate the adding of quotations to your columns, you can use add_prefix and add_suffix. If your starting dataframe looks like:

    >>> df
             date       ret
    0  2018-09-24  0.000131
    

    Then do:

    df = df.add_suffix('"').add_prefix('"')
    df.to_csv('test.csv',sep=' ',quoting=csv.QUOTE_NONE)