删除 Pandas 中的双引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44615807/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:49:18  来源:igfitidea点击:

Remove double quotes in Pandas

pythoncsvpandas

提问by KcFnMi

I have the following file:

我有以下文件:

"j"; "x"; y
"0"; "1"; 5
"1"; "2"; 6
"2"; "3"; 7
"3"; "4"; 8
"4"; "5"; 3
"5"; "5"; 4

Which I read by:

我阅读的内容:

df = pd.read_csv('test.csv', delimiter='; ', engine='python')

Then I print print dfand see:

然后我打印print df并查看:

   "j"  "x"  y
0  "0"  "1"  5
1  "1"  "2"  6
2  "2"  "3"  7
3  "3"  "4"  8
4  "4"  "5"  3
5  "5"  "5"  4

Instead, I would like to see:

相反,我想看到:

   j  x  y
0  0  1  5
1  1  2  6
2  2  3  7
3  3  4  8
4  4  5  3
5  5  5  4

How to remove the double quotes?

如何去掉双引号?

回答by KcFnMi

I did it with:

我是这样做的:

rm_quote = lambda x: x.replace('"', '')

df = pd.read_csv('test.csv', delimiter='; ', engine='python', 
     converters={'\"j\"': rm_quote, 
                 '\"x\"': rm_quote})

df = df.rename(columns=rm_quote)

回答by omri_saadon

You can pass the type as an argument to the read_csvfunction.

您可以将类型作为参数传递给read_csv函数。

pd.read_csv('test.csv', delimiter='; ', engine='python', dtype=np.float32)

You can read more in read_csv

您可以在read_csv 中阅读更多内容

Also, you can use to_numericfunction.

此外,您可以使用to_numeric函数。

df = df.apply(pd.to_numeric)