Convert monetary value (string) to Python / Pandas float

0

I'm reading a csv file where one of the columns has monetary values of '10, 000.00 ', the pandas is interpreting as a string.

I wonder if I have to convert this into a race (iterating all the items in the column) or is there an easier way to do this?

I would like the value as float, eg: 10000.00

    
asked by anonymous 03.05.2018 / 22:07

1 answer

1

One way that I find simple is to use apply

import pandas as pd
df = pd.DataFrame({'col1':pd.date_range('2015-01-02 15:00:07', periods=3),
               'col2':pd.date_range('2015-05-02 15:00:07', periods=3),
               'col3':pd.date_range('2015-04-02 15:00:07', periods=3),
               'col4':pd.date_range('2015-09-02 15:00:07', periods=3),
               'col5':[5,3,6],
               'col6':['10.000,00','10.000,00','10.000,00']})


df['col6'] = df['col6'].apply(lambda x: float(x.replace(".","").replace(",",".")))
print(df)

output

            col1                col2                col3  \
0 2015-01-02 15:00:07 2015-05-02 15:00:07 2015-04-02 15:00:07
1 2015-01-03 15:00:07 2015-05-03 15:00:07 2015-04-03 15:00:07
2 2015-01-04 15:00:07 2015-05-04 15:00:07 2015-04-04 15:00:07

             col4  col5     col6
0 2015-09-02 15:00:07     5  10000.0
1 2015-09-03 15:00:07     3  10000.0
2 2015-09-04 15:00:07     6  10000.0
    
04.05.2018 / 00:35