将巴西货币转换为浮动货币

2024-09-30 06:29:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试将巴西货币线转换为浮动值

import pandas as pd
df = pd.read_csv (r'OfficialDataSet.csv', dtype={'Income': str}) 
df['Income'].apply(type).value_counts()
df['Income'] = df['Income'].str.replace(',','').str.replace('R$ ','').astype('float')

但我一直在犯这样的错误:

ValueError: could not convert string to float: 'R$ 374.30'

如果您有任何建议,我将不胜感激


Tags: csvimportpandasdfreadas货币float
1条回答
网友
1楼 · 发布于 2024-09-30 06:29:17

您走的是正确的道路,只需要一点零钱-在$前面加一个\就行了:

df['Income'] = df['Income'].str.replace(',','').str.replace('R\$ ','').astype('float')

或者你可以这样做:

df['Income'] = df['Income'].str.replace(r'R\$ |,','').astype('float')

相关问题 更多 >

    热门问题