作业要求我们使用示例代码从BLS中获取数据并将其组织在同一数据集中。 但是我尝试了许多方法,但是仍然无法通过使用数据框来组织它们。有人可以帮忙吗?
示例代码与https://www.bls.gov/developers/api_python.htm类似 根据SeriesID定义,作业请求将检索以下数据:
从本节开始,从1980年到2018年,按季节进行调整(季节性调整代码)在制造,金融活动以及专业和商业服务的所有雇员的平均每周收入(数据类型代码)。
注意:该网站的最大查询数量是25次。
所以我这样写:
import requests
import json
import prettytable
headers = {'Content-type': 'application/json'}
data = json.dumps({"seriesid": ['CUUR0000SA0','CUUR0000SA0 ',"startyear":"2010", "endyear":"2018"})
p = requests.post('https://api.bls.gov/publicAPI/v2/timeseries/data/',
data=data, headers=headers)
json_data = json.loads(p.text)
for series in json_data['Results']['series']:
x=prettytable.PrettyTable(["series id","year","period","value","footnotes"])
seriesId = series['seriesID']
for item in series['data']:
year = item['year']
period = item['period']
value = item['value']
footnotes=""
for footnote in item['footnotes']:
if footnote:
footnotes = footnotes + footnote['text'] + ','
#if 'M01' <= period <= 'M12':
# x.add_row([seriesId,year,period,value,footnotes[0:-1]])
我提取数据如下:
+-------------+------+--------+---------+-----------+
| series id | year | period | value | footnotes |
+-------------+------+--------+---------+-----------+
| SUUR0000SA0 | 2014 | M12 | 134.207 | |
| SUUR0000SA0 | 2014 | M11 | 135.107 | |
| SUUR0000SA0 | 2014 | M10 | 135.891 | |
| SUUR0000SA0 | 2014 | M09 | 136.211 | |
| SUUR0000SA0 | 2014 | M08 | 136.127 | |
+-------------+------+--------+---------+-----------+
| series id | year | period | value | footnotes |
+-------------+------+--------+---------+-----------+
| CUUR0000SA0 | 2014 | M12 | 234.812 | |
| CUUR0000SA0 | 2014 | M11 | 236.151 | |
| CUUR0000SA0 | 2014 | M10 | 237.433 | |
| CUUR0000SA0 | 2014 | M09 | 238.031 | |
| CUUR0000SA0 | 2014 | M08 | 237.852 | |
| CUUR0000SA0 | 2014 | M07 | 238.250 | |
谁能告诉我如何使用pandas数据框将它们组合成数据集?