pandas 大熊猫读取以逗号分隔的千位分隔符格式的 CSV 数据

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

pandas reading CSV data formatted with comma for thousands separator

pandascsvseparator

提问by EdChum

I am trying to create a dataframe in pandas using a CSV that is semicolon-delimited, and uses commas for the thousands separator on numeric data. Is there a way to read this in so that the type of the column is float and not string?

我正在尝试使用以分号分隔的 CSV 在 Pandas 中创建一个数据框,并使用逗号作为数字数据的千位分隔符。有没有办法读取它,以便列的类型是浮点数而不是字符串?

回答by EdChum

Pass param thousands=','to read_csvto read those values as thousands:

传递 param thousands=','toread_csv以将这些值读取为数千:

In [27]:
import pandas as pd
import io

t="""id;value
0;123,123
1;221,323,330
2;32,001"""
pd.read_csv(io.StringIO(t), thousands=r',', sep=';')

Out[27]:
   id      value
0   0     123123
1   1  221323330
2   2      32001

回答by Grr

Take a look at the read_csvdocumentation there is a keyword argument 'thousands' that you can pass the ',' into. Likewise if you had European data containing a '.' for the separator you could do the same.

查看read_csv文档,有一个关键字参数“千”,您可以将“,”传递给它。同样,如果您有包含 '.' 的欧洲数据。对于分隔符,您也可以这样做。