#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简单清理。
最后结果:



