将逗号小数点分隔符转换为数据框内的点 [英] Convert commas decimal separators to dots within a Dataframe

查看:103
本文介绍了将逗号小数点分隔符转换为数据框内的点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用pandas.read_csv导入以下CSV文件:

I am importing a CSV file like the one below, using pandas.read_csv:

df = pd.read_csv(Input, delimiter=";")

CSV文件示例:

10;01.02.2015 16:58;01.02.2015 16:58;-0.59;0.1;-4.39;NotApplicable;0.79;0.2
11;01.02.2015 16:58;01.02.2015 16:58;-0.57;0.2;-2.87;NotApplicable;0.79;0.21

问题是,当我稍后在代码中尝试使用这些值时,出现以下错误:TypeError: can't multiply sequence by non-int of type 'float'

The problem is that when I later on in my code try to use these values I get this error: TypeError: can't multiply sequence by non-int of type 'float'

错误是因为我要使用的数字不是用点(.)作为小数点分隔符,而是逗号(,).手动将逗号更改为点后,我的程序可以工作.

The error is because the number I'm trying to use is not written with a dot (.) as a decimal separator but a comma(,). After manually changing the commas to a dots my program works.

我无法更改输入格式,因此必须替换DataFrame中的逗号才能使代码正常工作,我希望python无需手动执行此操作.你有什么建议吗?

I can't change the format of my input, and thus have to replace the commas in my DataFrame in order for my code to work, and I want python to do this without the need of doing it manually. Do you have any suggestions?

推荐答案

pandas.read_csv为此具有decimal参数:即尝试:

df = pd.read_csv(Input, delimiter=";", decimal=",")

这篇关于将逗号小数点分隔符转换为数据框内的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆