-1

Im having issue applying a currency rate in pandas. Some numbers are being converted as 'nan' whenever they contains a comma, eg: 1,789 will be considered as nan.

I started with that code :

import pandas as pd
usd_rate = 0.77
salary = pd.read_csv("salary.csv")

#create revenue clean (convert usd to gbp)
salary['revenue_gbp'] = salary.usd_revenue * usd_rate

So I was getting that error :

TypeError: can't multiply sequence by non-int of type 'float'

I've read you can't multiply the column by a float. So I converted my column to numeric :

salary.net_revenue = pd.to_numeric(salary.usd_revenue, errors='coerce')
salary['revenue_gbp'] = salary.usd_revenue  * usd_rate 

Now I don't have any errors, yet when I looked at my file , all of the number above 999.99 - so the ones containing a comma - are put under 'nan'...

I thought it could be translate issue .. but I'm getting confused here.. any ideas ?

Thanks a lot

Laure
  • 1
  • 4
  • share your data and the column dtypes – Mohit Motwani Nov 28 '18 at 10:44
  • what is the data type of your column salary['usd_revenue']? You can check this by salary.usd_revenue.dtype It should be a float or int, but yours is probably a text field, which is why it's giving this error. – Sander van den Oord Nov 28 '18 at 10:44
  • Your code works for me *provided that the CSV file contains numerical types*. Your error message suggests that `salary.usd_revenue` is being read in as a sequence. You need to parse this further to extract the number you wish to multiply. – DatHydroGuy Nov 28 '18 at 10:47
  • So I've checked the dtype and I have Out : dtype('O') ? – Laure Nov 28 '18 at 10:54

1 Answers1

0

usd_revenue is probably not already a numeric type. Try this:

salary['usd_revenue'] = salary['usd_revenue'].map(float)

before your actual line:

salary['revenue_gbp'] = salary.usd_revenue * usd_rate