How to SUM columns with Currency (object type) values in Python / Pandas?

Solution 1:

To my knowledge, there is no currency type in Python. I guess your "Price" column is a string? The first operation would be to turn this column into a float. To do so, you have to remove the "U$ " in front of the digits, and then turn the "," into "." to be able to turn your string into a float. So a function doing that would be:

def price_to_float(price):
    return(float(price[3:].replace(',','.')))`

Then, just apply this function to your whole column with:

df['Price']=df.apply(lambda row : price_to_float(row['Price']), axis = 1)
df['Tax']=df.apply(lambda row : price_to_float(row['Price']), axis = 1)

Now you can perform all the operations you want to! You can then turn your column back to a currency string if you need to.