How to efficiently handle European decimal separators using the pandas read_csv function?

吃可爱长大的小学妹 提交于 2019-12-04 08:31:13

You can use the converters kw in read_csv. Given /tmp/data.csv like this:

"x","y"                                                                         
"one","1.234,56"                                                                
"two","2.000,00"   

you can do:

In [20]: pandas.read_csv('/tmp/data.csv', converters={'y': lambda x: float(x.replace('.','').replace(',','.'))})
Out[20]: 
     x        y
0  one  1234.56
1  two  2000.00

For European style numbers, use the thousands and decimal parameters in pandas.read_csv.

For example:

pandas.read_csv('data.csv', thousands='.', decimal=',')

From the docs:

thousands :

str, optional Thousands separator.

decimal :

str, default ‘.’ Character to recognize as decimal point (e.g. use ‘,’ for European data).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!