pandas 删除python pandas中所有列值的双引号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42011882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove double quotes in python pandas for all column's values
提问by H J
so I am reading a CSV file and then only keeping certain columns and rewriting the file. The issues I have is that one column (say ColumnA) has it's values in double quotation marks. Is there an easy way for me to achieve this task and also remove those double quotes?
所以我正在读取一个 CSV 文件,然后只保留某些列并重写文件。我遇到的问题是一列(比如 ColumnA)的值用双引号括起来。有没有一种简单的方法可以让我完成这项任务并删除那些双引号?
a = pd.read_csv(filename,low_memory=False)
a1 = a[['ColumnA','ColumnB']]
a1.to_csv('filelocation')
回答by MaxU
you can suppress quoting using csv.QUOTE_NONE
:
您可以使用csv.QUOTE_NONE
以下方法禁止引用:
import csv
pd.read_csv(filename,usecols=['ColumnA','ColumnB']) \
.to_csv(new_file_name, quoting=csv.QUOTE_NONE, index=False)
but it's better (safer) to chose another delimiter (the one which doesn't occur in the ColumnA
column), so you won't have problems in future when you will read/parse that new CSV file:
但最好(更安全)选择另一个分隔符(ColumnA
列中没有出现的分隔符),因此将来读取/解析该新 CSV 文件时不会出现问题:
pd.read_csv(filename,usecols=['ColumnA','ColumnB']) \
.to_csv(new_file_name, sep=';', index=False)