获取世界银行数据指标和使用Pandas数据fram

2024-06-16 11:48:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图获取世界银行的一系列健康指标数据。在

使用以下代码访问世界银行数据:

进口:

import wbdata
import datetime

查看不同的指标:

^{pr2}$

这将返回以下结果:

SP.DYN.TFRT.IN          Fertility rate, total (births per woman)
SP.DYN.SMAM.MA          Mean age at first marriage, male
SP.DYN.SMAM.FE          Mean age at first marriage, female

要访问特定国家或地区的数据,请使用以下代码:

data_dates = (datetime.datetime(2015,1,1), datetime.datetime(2015,1,1))

top_20_data = wbdata.get_dataframe({'SP.DYN.TFRT.IN':'Fertility rate, total (births per woman)','SP.DYN.SMAM.MA':'Mean age at first marriage, male'}, 
                            country=('BE','BG','CZ','DK','DE','EE','IE','GR','ES','FR','HR','IT','CY','LV','LT','LU',
                                     'HU','MT','NL','AT','PL','PT','RO','SI','SK','FI','SE','GBR'), 
                            data_date=data_dates, 
                            convert_date=False, keep_levels=True)

我要做的是把每一个指标输入数据框和每一个描述。在

我尝试创建一个小样本pandas数据框架:

data = {'Indicator': ['SP.DYN.TFRT.IN', 'SP.DYN.SMAM.MA', 'SP.DYN.SMAM.MA'],
 'Description': ['Fertility rate, total (births per woman)', 'Mean age at first marriage, male', 'Mean age at first marriage, female']}

df = pd.DataFrame(data, columns=['Indicator', 'Description']) 

把这个传给wdata.get_daframe像这样:

top_20_data = wbdata.get_dataframe({df['Indicator']:df['Description']}, 
                            country=('BE','BG','CZ','DK','DE','EE','IE','GR','ES','FR','HR','IT','CY','LV','LT','LU',
                                     'HU','MT','NL','AT','PL','PT','RO','SI','SK','FI','SE','GBR'), 
                            data_date=data_dates, 
                            convert_date=False, keep_levels=True)

但我收到以下错误:

TypeError: 'Series' objects are mutable, thus they cannot be hashed

我在网上查了一下,但没有发现什么特别有用的东西。在


Tags: 数据agedatadatetimedatemean指标sp
1条回答
网友
1楼 · 发布于 2024-06-16 11:48:20

DataFrame转换为字典:

d = dict(df.values)
#another solution
#d = df.set_index('Indicator')['Description'].to_dict()
top_20_data = wbdata.get_dataframe(d, 
                            country=('BE','BG','CZ','DK','DE','EE','IE','GR','ES','FR','HR','IT','CY','LV','LT','LU',
                                     'HU','MT','NL','AT','PL','PT','RO','SI','SK','FI','SE','GBR'), 
                            data_date=data_dates, 
                            convert_date=False, keep_levels=True)

相关问题 更多 >