Pandas exporting to_csv() with quotation marks around column names












1















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









share|improve this question

























  • This is called quoted output. In general there is no need to manually hack quotes into the column names. Use one of the quoting=csv.QUOTE_... options

    – smci
    Nov 20 '18 at 1:00











  • Possible duplicate of Quote only the required columns using pandas to_csv

    – smci
    Nov 20 '18 at 1:14
















1















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









share|improve this question

























  • This is called quoted output. In general there is no need to manually hack quotes into the column names. Use one of the quoting=csv.QUOTE_... options

    – smci
    Nov 20 '18 at 1:00











  • Possible duplicate of Quote only the required columns using pandas to_csv

    – smci
    Nov 20 '18 at 1:14














1












1








1








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









share|improve this question
















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






pandas csv escaping export-to-csv quoting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 1:04









smci

14.7k672104




14.7k672104










asked Nov 19 '18 at 23:25









user40780user40780

423826




423826













  • This is called quoted output. In general there is no need to manually hack quotes into the column names. Use one of the quoting=csv.QUOTE_... options

    – smci
    Nov 20 '18 at 1:00











  • Possible duplicate of Quote only the required columns using pandas to_csv

    – smci
    Nov 20 '18 at 1:14



















  • This is called quoted output. In general there is no need to manually hack quotes into the column names. Use one of the quoting=csv.QUOTE_... options

    – smci
    Nov 20 '18 at 1:00











  • Possible duplicate of Quote only the required columns using pandas to_csv

    – smci
    Nov 20 '18 at 1:14

















This is called quoted output. In general there is no need to manually hack quotes into the column names. Use one of the quoting=csv.QUOTE_... options

– smci
Nov 20 '18 at 1:00





This is called quoted output. In general there is no need to manually hack quotes into the column names. Use one of the quoting=csv.QUOTE_... options

– smci
Nov 20 '18 at 1:00













Possible duplicate of Quote only the required columns using pandas to_csv

– smci
Nov 20 '18 at 1:14





Possible duplicate of Quote only the required columns using pandas to_csv

– smci
Nov 20 '18 at 1:14












2 Answers
2






active

oldest

votes


















1














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)





share|improve this answer
























  • is this version dependent? I didn't get the desired output when quoting=csv.QUOTE_NONE

    – user40780
    Nov 19 '18 at 23:41











  • I wouldn't think so, but just for reference I'm using '0.21.1'

    – sacul
    Nov 19 '18 at 23:42



















1














This is called quoted output.
Instead of manually hacking in quotes into your column names (which will mess with other dataframe functionality), use the quoting option:



df = pd.DataFrame({"date": ["2018-09-24"], "ret": [0.00013123989025119056]})

df.to_csv("out_q_esc.txt", sep=' ', escapechar='\', quoting=csv.QUOTE_ALL, index=None)
"date" "ret"
"2018-09-24" "0.00013123989025119056"


The 'correct' way is to use quoting=csv.QUOTE_ALL (and optionally escapechar='\'), but note however that QUOTE_ALL will force all columns to be quoted, even obviously numeric ones like the index; if we hadn't specified index=None, we would get:



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




  • csv.QUOTE_MINIMAL refuses to quote these fields because they don't strictly need quotes (they're neither multiline nor do they contain internal quote or separator chars)






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384099%2fpandas-exporting-to-csv-with-quotation-marks-around-column-names%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    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)





    share|improve this answer
























    • is this version dependent? I didn't get the desired output when quoting=csv.QUOTE_NONE

      – user40780
      Nov 19 '18 at 23:41











    • I wouldn't think so, but just for reference I'm using '0.21.1'

      – sacul
      Nov 19 '18 at 23:42
















    1














    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)





    share|improve this answer
























    • is this version dependent? I didn't get the desired output when quoting=csv.QUOTE_NONE

      – user40780
      Nov 19 '18 at 23:41











    • I wouldn't think so, but just for reference I'm using '0.21.1'

      – sacul
      Nov 19 '18 at 23:42














    1












    1








    1







    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)





    share|improve this answer













    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)






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 19 '18 at 23:34









    saculsacul

    30k41740




    30k41740













    • is this version dependent? I didn't get the desired output when quoting=csv.QUOTE_NONE

      – user40780
      Nov 19 '18 at 23:41











    • I wouldn't think so, but just for reference I'm using '0.21.1'

      – sacul
      Nov 19 '18 at 23:42



















    • is this version dependent? I didn't get the desired output when quoting=csv.QUOTE_NONE

      – user40780
      Nov 19 '18 at 23:41











    • I wouldn't think so, but just for reference I'm using '0.21.1'

      – sacul
      Nov 19 '18 at 23:42

















    is this version dependent? I didn't get the desired output when quoting=csv.QUOTE_NONE

    – user40780
    Nov 19 '18 at 23:41





    is this version dependent? I didn't get the desired output when quoting=csv.QUOTE_NONE

    – user40780
    Nov 19 '18 at 23:41













    I wouldn't think so, but just for reference I'm using '0.21.1'

    – sacul
    Nov 19 '18 at 23:42





    I wouldn't think so, but just for reference I'm using '0.21.1'

    – sacul
    Nov 19 '18 at 23:42













    1














    This is called quoted output.
    Instead of manually hacking in quotes into your column names (which will mess with other dataframe functionality), use the quoting option:



    df = pd.DataFrame({"date": ["2018-09-24"], "ret": [0.00013123989025119056]})

    df.to_csv("out_q_esc.txt", sep=' ', escapechar='\', quoting=csv.QUOTE_ALL, index=None)
    "date" "ret"
    "2018-09-24" "0.00013123989025119056"


    The 'correct' way is to use quoting=csv.QUOTE_ALL (and optionally escapechar='\'), but note however that QUOTE_ALL will force all columns to be quoted, even obviously numeric ones like the index; if we hadn't specified index=None, we would get:



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




    • csv.QUOTE_MINIMAL refuses to quote these fields because they don't strictly need quotes (they're neither multiline nor do they contain internal quote or separator chars)






    share|improve this answer






























      1














      This is called quoted output.
      Instead of manually hacking in quotes into your column names (which will mess with other dataframe functionality), use the quoting option:



      df = pd.DataFrame({"date": ["2018-09-24"], "ret": [0.00013123989025119056]})

      df.to_csv("out_q_esc.txt", sep=' ', escapechar='\', quoting=csv.QUOTE_ALL, index=None)
      "date" "ret"
      "2018-09-24" "0.00013123989025119056"


      The 'correct' way is to use quoting=csv.QUOTE_ALL (and optionally escapechar='\'), but note however that QUOTE_ALL will force all columns to be quoted, even obviously numeric ones like the index; if we hadn't specified index=None, we would get:



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




      • csv.QUOTE_MINIMAL refuses to quote these fields because they don't strictly need quotes (they're neither multiline nor do they contain internal quote or separator chars)






      share|improve this answer




























        1












        1








        1







        This is called quoted output.
        Instead of manually hacking in quotes into your column names (which will mess with other dataframe functionality), use the quoting option:



        df = pd.DataFrame({"date": ["2018-09-24"], "ret": [0.00013123989025119056]})

        df.to_csv("out_q_esc.txt", sep=' ', escapechar='\', quoting=csv.QUOTE_ALL, index=None)
        "date" "ret"
        "2018-09-24" "0.00013123989025119056"


        The 'correct' way is to use quoting=csv.QUOTE_ALL (and optionally escapechar='\'), but note however that QUOTE_ALL will force all columns to be quoted, even obviously numeric ones like the index; if we hadn't specified index=None, we would get:



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




        • csv.QUOTE_MINIMAL refuses to quote these fields because they don't strictly need quotes (they're neither multiline nor do they contain internal quote or separator chars)






        share|improve this answer















        This is called quoted output.
        Instead of manually hacking in quotes into your column names (which will mess with other dataframe functionality), use the quoting option:



        df = pd.DataFrame({"date": ["2018-09-24"], "ret": [0.00013123989025119056]})

        df.to_csv("out_q_esc.txt", sep=' ', escapechar='\', quoting=csv.QUOTE_ALL, index=None)
        "date" "ret"
        "2018-09-24" "0.00013123989025119056"


        The 'correct' way is to use quoting=csv.QUOTE_ALL (and optionally escapechar='\'), but note however that QUOTE_ALL will force all columns to be quoted, even obviously numeric ones like the index; if we hadn't specified index=None, we would get:



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




        • csv.QUOTE_MINIMAL refuses to quote these fields because they don't strictly need quotes (they're neither multiline nor do they contain internal quote or separator chars)







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 20 '18 at 1:09

























        answered Nov 20 '18 at 0:58









        smcismci

        14.7k672104




        14.7k672104






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384099%2fpandas-exporting-to-csv-with-quotation-marks-around-column-names%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            'app-layout' is not a known element: how to share Component with different Modules

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            WPF add header to Image with URL pettitions [duplicate]