我是Python新手。我有以下代码:
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)
数据帧的输出和数据帧中的列数如下:
^{pr2}$如您所见,只有数据帧(“GNI”)中的一列被识别为列。
我该怎么做才能让“国家”和“日期”也被识别为列?
我的目标是要有一个下面所示类型的面板数据集。其中有三个变量(=Stata语言):国家、日期和国民总收入。如果国家变量中没有空格,因为每个国民总收入观察值对应一个国家日期组合。
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]
当然,我在糟蹋Python的语法和语言,但任何帮助或指导将不胜感激。
您只看到GNI列,因为国家和日期被用作索引(确切地说是多重索引)。在
你需要的是reset_index:
相关问题 更多 >
编程相关推荐