Quote only the required columns using pandas to_csv

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 17:52:10

问题


I need to generate a csv using pandas to_csv function. I tried quote=csv.QUOTE_NONNUMERIC. But for one of the date time column I dont need double quotes.

Is there a way to select the columns for which we want double quotes?


回答1:


Thanks people. I got the answer so just thought to share.

First I got the list of headers to be quoted and looped them something like below:

for col in quoteColumnsList:
    df[col] = '"' + df[col] + '"'

Here my quotechar is '"'. Now I used to_csv with quote parameter as csv.QUOTE_NONE. This way we get the double quotes only to the required columns.



来源:https://stackoverflow.com/questions/47647319/quote-only-the-required-columns-using-pandas-to-csv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!