给出一个数据帧中的列:df [“ BILL_AMT”],它具有字符串类型的值,我想将它们转换为float然后求和。
for b in df["BILL_AMT"]:
print(b)
output:
2,879,403.00
-2,938,167.00
1,990,609.00
-2,006.00
s=0
for b in df["BILL_AMT"]:
s=s+float(b.strip(','))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-51-742469fb1c93> in <module>
1 s=0
2 for b in df["BILL_AMT"]:
----> 3 s=s+float(b.strip(','))
ValueError: could not convert string to float: '2,879,403.00'