栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

使用pandas和pygal绘制地图

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用pandas和pygal绘制地图

#coding=utf-8

import pandas as pd
import pygal

from pygal_maps_world.i18n import COUNTRIES
import pygal_maps_world.maps
from pygal.style import RotateStyle as RS, LightColorizedStyle as LCS

#导入数据,使用pandas清洗数据,不使用with open
filename = 'Adolescent fertility rate (births per 1,000 women ages 15-19).csv'
#第一行是表名,省略第一行,使用skiprows
df = pd.read_csv(filename, skiprows=1)
#只使用国家名和2019年的数据
df = df[['Country Name', '2019']]

#按行读取csv
country_dict = {}
rate_dict = {}
for i in range(len(df)):
    country_dict[i] = df['Country Name'][i]
    rate_dict[i] = df['2019'][i]

def get_country_code(country_name):
    """取国家对应的code,返回两个字母的国别码"""
    for code, name in COUNTRIES.items():
        if name == country_name:
            return code
    return None

#取值每个国家2019年每1千人中青少年人数
country_cc = {}
for key, value in country_dict.items():
    for key1, value1 in rate_dict.items():
        if key == key1:
            country_name = value
            rate = round(value1, 2)
            code = get_country_code(country_name)
            if code:
                country_cc[code] = rate
            else:
                ''
#对青少年人数将国家分成6段
country_cc1, country_cc2, country_cc3, country_cc4, country_cc5, country_cc6 = {}, {}, {}, {}, {}, {}
for cc, rate in country_cc.items():
    if rate <20.00:
        country_cc1[cc] = rate
    elif rate <40.00:
        country_cc2[cc] = rate
    elif rate <60.00:
        country_cc3[cc] = rate
    elif rate <80.00:
        country_cc4[cc] = rate
    elif rate <100.00:
        country_cc5[cc] = rate
    else:
        country_cc6[cc] = rate

#绘制地图,使用pygal样式
wm_style = RS('#336699', base_style=LCS)
wm = pygal_maps_world.maps.World(style = wm_style)
wm.title = 'world Adolescent fertility rate'

wm.add('0-20', country_cc1)
wm.add('20-40', country_cc2)
wm.add('40-60', country_cc3)
wm.add('60-80', country_cc4)
wm.add('80-100', country_cc5)
wm.add('>=100', country_cc6)

wm.render_to_file('world.svg')

看《python编程 从入门到实践》例子是使用csv清理数据源,但我想使用pandas简单清理。

最后结果:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/487242.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号