将熊猫转换为JVectorMap的mapData

问题描述 投票:0回答:1

我有一个flask / python网站,需要将熊猫列-CountryCode(“ US”)和Value(192656.56)-返回到呈现JVectorMap的html模板中

mapData必须看起来像:

var gdpData = {
  "AF": 16.63,
  "AL": 11.58,
  "DZ": 158.97,
  ...
};

数据框看起来像:

enter image description here

如果此pandas df是一个json列表(请参阅-http://api.worldbank.org/v2/country/all/indicator/NY.GDP.PCAP.PP.CD?format=json&mrv=1&per_page=300),则下一个python代码可以正常工作,但是我无法获得格式正确的变量以从函数中返回-带有pandas数据框。到目前为止,我已经尝试了十种循环/打印方法,并且所有方法都使所需的变量输出更加混乱。

mapData = "var mapData = {\n"
for item in y[1]:   
    mapData += item['country']['id'] + ": " + "{:.2f}".format(item['value']) +",\n"
mapData += "}"
print(mapData)

接下来,将这些数据添加到Jvector脚本中,如下所示:

function createList(country.value, country.id,indicator.id){
    var gdpData = {
        results.gdpdata   # data to be filled in from the python and pandas df
      };

    $('#world-map-gdp').vectorMap({
    map: 'world_mill',
    series: {
    regions: [{
        values: gdpData,
        scale: ['#C8EEFF', '#0071A4'],
        normalizeFunction: 'polynomial'
    }]
    },
    onRegionTipShow: function(e, el, code){
    el.html(el.html()+' (GDP - '+gdpData[code]+')');
    }
});

}

python jquery pandas flask jvectormap
1个回答
0
投票

我能够通过Regex脚本和pandas字符串转储来“欺骗”并使字符串工作(带有控制台错误)。

© www.soinside.com 2019 - 2024. All rights reserved.