Reputation: 17
I have this DF (gdp_g20) with information of GDP, first line example below:
Country Name Country Code Indicator Name Indicator Code 2014
Argentina ARG GDP (current US$) NY.GDP.MKTP.CD $526,320,000,000.00
I want to plot it but as the value types of column '2014' are string, it's not possible.
So anyone know how to convert gdp_g20['2014'] to numeric data? I've seen some posts but none of them worked.
Thanks in advance.
I tried removing the $ sign and convert with to_numeric, as below:
gdp_g20['2014'] = gdp_g20['2014'].apply(lambda x: x.replace('$', ''))
gdp_g20['2014'] = pd.to_numeric(gdp_g20['2014'])
But then I got the error:
"Unable to parse string " 526,320,000,000.00 " at position 0"
Upvotes: 0
Views: 149
Reputation: 61
Looks like you were on the right track you just need to remove the "," too
Adding another line to your code like this works
gdp_g20["2014"] = gdp_g20['2014'].apply(lambda x: x.replace('$', ''))
gdp_g20["2014"] = gdp_g20['2014'].apply(lambda x: x.replace(',', ''))
gdp_g20['2014'] = pd.to_numeric(gdp_g20['2014'])
Upvotes: 1
Reputation: 9857
You need to remove the $
and get rid of the ,
, this should do it.
gdp_g20['2014'] = data['2014'].str[1:].str.replace(',','').astype(float)
Upvotes: 1