pythonpandasstringdataframeeconomics

Converting print output into a dataframe


I am currently working on a world bank data project, and would like to convert the following output into a simple pandas dataframe.

import pandas as pd
##If already have wbgapi installed, if not pip install wbgapi in cmd prompt
import wbgapi as wbgapi
print(wbgapi.economy.info())

Note, you may need to pip install wbgapi if you do not already have it. I wish to convert the output of this print statement, which pulls up what is essentially a table, as then I can use pandas functions on this table, sortby etc.

I tried, what I was 99.9% sure wouldn't work as the output is ofcourse a string not a dict,

economies = wbgapi.economy.info()
df = pd.DataFrame(economies)

Approaches such as converting a string to a list etc., I cannot figure out how to approach the string in question (the output of print(wb.economy.info())) given its spacing and column like nature, rather than being a block of text.

Any help would be greatly appreciated.


Solution

  • Try this:

    df = wbgapi.economy.DataFrame()
    

    I would suggest having a look at the documentation either way: https://pypi.org/project/wbgapi/