不是将结果附加在行中。
country year data
0 CAN 2017 1.653043e+12
1 UKR 2017 1.535768e+12
2 USA 2017 1.559623e+12
我希望将它们添加为新列:
year CAN UKR USA
0 2017 1.653043e+12 1.535768e+12 1.559623e+12
我首先将初始输出拆分为独立的数据帧,然后将它们组合在一起,但这并不是很简单。有一种雄辩的方法吗?
import pandas as pd
import requests
country = 'NOR;CAN;USA;UKR'
series = 'NY.GDP.MKTP.CD'
GDP_url = 'http://api.worldbank.org/v2/countries/' + country +
'/indicators/'+ series +'?per_page=1000&format=json'
GDP_results = requests.get(GDP_url)
x = GDP_results.json()
clist = country.split(";")
years = x[1]
if years ==None:
print ('empty results')
else:
df = pd.DataFrame(columns=['country','year','data'])
lst_dict = []
for y in years:
for c in clist:
if y['countryiso3code'] == c:
lst_dict.append({'country':y['countryiso3code'], 'year': y['date'], 'data' : y['value']})
df = df.append(lst_dict)
print (df)