我正在尝试将数据帧字段从字符串转换为 Pandas 中的浮点数。
这是字段:
In:
print(merged['platnosc_total'].head(100))
Out:
0 0,00
1 4,50
2 0,00
3 0,00
4 0,00
5 4,50
6 6,10
7 7,99
8 4,00
9 7,69
10 7,50
Run Code Online (Sandbox Code Playgroud)
请注意最后一行中的 7,50,这似乎会导致错误:
In:
merged['platnosc_total'].astype(float)
Out:
ValueError: could not convert string to float: '7,50'
Run Code Online (Sandbox Code Playgroud)
这是否意味着其余的都已转换,并且只有 7,50 的行才是原因?
我怎样才能真正将该字段/列转换为浮动?
首先需要replace:
print (merged['platnosc_total'].replace(',','.', regex=True).astype(float))
0 0.00
1 4.50
2 0.00
3 0.00
4 0.00
5 4.50
6 6.10
7 7.99
8 4.00
Name: platnosc_total, dtype: float64
Run Code Online (Sandbox Code Playgroud)