【发布时间】:2015-10-09 20:56:30
【问题描述】:
我有一个带有货币的df:
df = pd.DataFrame({'Currency':['$1.00','$2,000.00','(3,000.00)']})
Currency
0 $1.00
1 $2,000.00
2 (3,000.00)
我想将“货币”dtype 转换为浮点数,但括号字符串(表示负数)有问题。这是我当前的代码:
df[['Currency']] = df[['Currency']].replace('[\$,]','',regex=True).astype(float)
产生错误:
ValueError: could not convert string to float: (3000.00)
我想要的 dtype float 是:
Currency
0 1.00
1 2000.00
2 -3000.00
【问题讨论】: