0

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"

2
  • Removing the dollar sign should allow any other method to recast the datatype as numerical data i think. Commented Jul 14, 2021 at 13:49
  • I've tried that but got an error. Edited the post so you can see. May you can have an other idea of how to do it. Commented Jul 14, 2021 at 14:08

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! did it in a different way but it worked. gdp_g20['2014'] = gdp_g20['2014'].apply(lambda x: x.replace('$', '').replace(',', '')) gdp_g20['2014'] = pd.to_numeric(gdp_g20['2014'])
1

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'])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.