我有貨幣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)
我想將「貨幣」 D型浮動轉換,但我有括號字符串的麻煩(這表示負數)。這是我當前的代碼:
df[['Currency']] = df[['Currency']].replace('[\$,]','',regex=True).astype(float)
產生一個錯誤:
ValueError: could not convert string to float: (3000.00)
我想要什麼作爲D型浮動是:
Currency
0 1.00
1 2000.00
2 -3000.00
@George這裏你從JohnE那裏得到它。速度更快。 –