本文介绍了 pandas 读取以逗号分隔的CSV数据以千位分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用以分号分隔的CSV格式在熊猫中创建数据框,并对数字数据的千位分隔符使用逗号.有没有一种方法可以读取此内容,以使列的类型为浮点型而不是字符串型?
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?
推荐答案
将参数thousands=','
传递到 read_csv
读取成千上万的值:
Pass param thousands=','
to read_csv
to read those values as thousands:
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
这篇关于 pandas 读取以逗号分隔的CSV数据以千位分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!