3

I'm new to Python. I have the below code:

import wbdata # World Bank's API
import pandas
import matplotlib.pyplot as plt

#countries I want
countries = ["CL","UY","HU"]

#indicators I want
indicators = {'NY.GNP.PCAP.CD':'GNI per Capita'}

#grab indicators above for countries I want and load into data frame
df = wbdata.get_dataframe(indicators, country=countries, convert_date=False)

#list the columns in data frame
list(df.columns.values)

The output for my data frame and the number of columns in the data frame is the following:

In [1]:df
Out[1]: 
                GNI
country date         
Chile   2017  13610.0
        2016  13430.0
        2015  14270.0
        2014  15140.0
        2013  15360.0
        2012  14410.0
        2011  12380.0
              ...
Uruguay 2017  23410.0
        2016  11430.0
        2015  11270.0
        2014  11440.0
        2013  65360.0
        2012  94410.0
        2011  10380.0

[174 rows x 1 columns]

In [2]: list(df.columns.values)
Out[2]: ['GNI']

As you see, only one of the columns in the data frame ("GNI") is recognized as a column.

What can I do to have 'country' and 'date' be recognized as columns as well?

My objective is to have a panel dataset of the type seen below. Where there are three variables (=Stata language): Country, Date and GNI. And where no blanks exist in the Country variable, as each GNI observation corresponds to a country-date combination.

Country Date   GNI   
Chile   2017  13610.0
Chile   2016  13430.0
Chile   2015  14270.0
Chile   2014  15140.0
Chile   2013  15360.0
Chile   2012  14410.0
Chile   2011  12380.0
              ...
Uruguay 2017  23410.0
Uruguay 2016  11430.0
Uruguay 2015  11270.0
Uruguay 2014  11440.0
Uruguay 2013  65360.0
Uruguay 2012  94410.0
Uruguay 2011  10380.0

[174 rows × 3 columns]

Surely I'm butchering the Python syntax and language, but any help or guidance would be appreciated.

2

1 Answer 1

2

You see only GNI as column because Country and Date are used as indexes (a MultiIndex, to be precise).

What you need is reset_index:

df = df.reset_index(drop=False)
2
  • thanks for the explanation. What's the point of indexes in Pandas? I've searched online and online find explanations that require prior knowledge of Python terminology and data structures. Aug 16, 2018 at 14:48
  • 1
    This could be a good reference for your question: stackoverflow.com/questions/27238066/… Personally I tend to see different columns as 'entities' and rows as observations of that entity, hence the index is the label for the observations. For example, in a time-series you might want to refer to an observation by date
    – FLab
    Aug 16, 2018 at 15:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.