-2

I have pandas dataframe, I want to convert them to million and billion. How can I restore it back to a number?

picture

FAP
  • 1
  • 2

1 Answers1

0

You can try,

df["views"] = (map(eval, df["views"].str.replace("M", "*10**9")))

Here I am firstly replacing all "M" with "10**9". If you do eval("5*10**9") you would get 5 billion. That's because the eval function converts any string to a Python statement. Because of this functionality, I pass each item in the replaced series in the eval function using the map function, convert it into a list and store it in df["views"].

If you just want to make "674M" to "674B", do this -

df["views"] = df["views"].str.replace("M", "B")

And please put the dataframe in the question as text or table.

Zero
  • 1,800
  • 1
  • 5
  • 16