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
pandas reading CSV data formatted with comma for thousands separator
提问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_csv
to 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