使用pandas to_csv仅引用必需的列 [英] Quote only the required columns using pandas to_csv

查看:116
本文介绍了使用pandas to_csv仅引用必需的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用pandas to_csv函数生成一个csv.我尝试了quote = csv.QUOTE_NONNUMERIC.但是对于日期时间列之一,我不需要双引号.

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?

推荐答案

谢谢.我得到了答案,所以只想分享一下.

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] + '"'

这里我的quotechar是''.现在我使用了to_csv,并将quote参数设置为csv.QUOTE_NONE.这样,我们仅将双引号添加到所需的列上.

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.

这篇关于使用pandas to_csv仅引用必需的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆