将逗号小数点分隔符转换为 Dataframe 中的点 [英] Convert commas decimal separators to dots within a Dataframe
问题描述
我正在使用 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乘序列乘以非 int '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
参数:doc
即尝试:
df = pd.read_csv(Input, delimiter=";", decimal=",")
这篇关于将逗号小数点分隔符转换为 Dataframe 中的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!