Python+Pyecharts实现1995~2020世界人口分析报告

一 项目说明

1 数据来源阿里云天池数据集------1955年至2020年的国家人口
2 字段说明:
Data columns (total 14 columns):
Column Non-Null Count Dtype


0 Year 4195 non-null int64 -----年份
1 Country 4195 non-null object -----国家名称
2 Population 4195 non-null int64 -----人口总数
3 Yearly % Change 4195 non-null float64-----人口年变化率
4 Yearly Change 4195 non-null int64 -----人口年改变数量
5 Migrants (net) 3600 non-null float64-----移民人数
6 Median Age 3600 non-null float64-----年龄中位数
7 Fertility Rate 3600 non-null float64-----生育率
8 Density (P/Km²) 4195 non-null object -----人口密度(人/平方千米)
9 Urban Pop % 4082 non-null float64-----城镇人口比例
10 Urban Population 4082 non-null float64-----城镇人口数目
11 Country’s Share of World Pop % 4195 non-null float64-----国家人口数对世界人口总数的贡献率
12 World Population 4195 non-null int64 -----世界人口总数
13 Country Global Rank 4195 non-null int64 -----人口总数世界排名
dtypes: float64(7), int64(5), object(2)
memory usage: 459.0+ KB

二 数据清洗

# 1 导入模块
import pandas as pd
import pyecharts.options as opts
from pyecharts.charts import Bar,Line,Grid,Map
from pyecharts.globals import ThemeType

# 2 数据清洗
df = pd.read_csv('D:\\LearningMaterials\Countries Population from 1995 to 2020.csv')
#验证读取是否成功
print(df.head())
#查看数据集索引、内存和数据类型
print(df.info())
#发现有空值,用0填充,直接在原始数据上进行修改。即inplca=True.否则无效
df.fillna(0,inplace=True)
#验证
print(df.isnull().sum())

运行结果:
Year Country … World Population Country Global Rank
0 2020 China … 7794798739 1
1 2019 China … 7713468100 1
2 2018 China … 7631091040 1
3 2017 China … 7547858925 1
4 2016 China … 7464022049 1

[5 rows x 14 columns]
<class ‘pandas.core.frame.DataFrame’>
RangeIndex: 4195 entries, 0 to 4194
Data columns (total 14 columns):
Column Non-Null Count Dtype


0 Year 4195 non-null int64
1 Country 4195 non-null object
2 Population 4195 non-null int64
3 Yearly % Change 4195 non-null float64
4 Yearly Change 4195 non-null int64
5 Migrants (net) 3600 non-null float64
6 Median Age 3600 non-null float64
7 Fertility Rate 3600 non-null float64
8 Density (P/Km²) 4195 non-null object
9 Urban Pop % 4082 non-null float64
10 Urban Population 4082 non-null float64
11 Country’s Share of World Pop % 4195 non-null float64
12 World Population 4195 non-null int64
13 Country Global Rank 4195 non-null int64
dtypes: float64(7), int64(5), object(2)
memory usage: 459.0+ KB
None
Year 0
Country 0
Population 0
Yearly % Change 0
Yearly Change 0
Migrants (net) 0
Median Age 0
Fertility Rate 0
Density (P/Km²) 0
Urban Pop % 0
Urban Population 0
Country’s Share of World Pop % 0
World Population 0
Country Global Rank 0
dtype: int64

三 数据可视化

1 1955~2020年世界人口总数变化

# 3.1 1955~2020年世界人口总数变化情况
df1 = df.groupby('Year')['Population'].sum().to_frame('人口数量').reset_index()
year = list(df1['Year'])
population = list(df1['人口数量'])
p1 = (
    Bar(init_opts=opts.InitOpts(width='1600px',height='800px',theme=ThemeType.LIGHT))
    .add_xaxis(year)
    .add_yaxis(
        "",
        population,
        label_opts=opts.LabelOpts(is_show=False),
    )
    .set_global_opts(
        title_opts=opts.TitleOpts(title='1955~2020年世界人口总数变化情况'),
        xaxis_opts=opts.AxisOpts(
            splitline_opts=opts.SplitLineOpts(is_show=True),
            axistick_opts=opts.AxisTickOpts(is_show=False),
        ),
        yaxis_opts=opts.AxisOpts(
            splitline_opts=opts.SplitLineOpts(is_show=True),
            axistick_opts=opts.AxisTickOpts(is_show=True),
        ),
        toolbox_opts=opts.ToolboxOpts(is_show=True),
    )
    .set_series_opts(
        label_opts=opts.LabelOpts(is_show=False),
    )
    .reversal_axis()
    .render('1.html')
)

运行结果:
在这里插入图片描述
可以看到,1955~2020年世界人口总数正在逐步增加;其中,1955到2015年人口增长速度较快;
而从2015年开始,世界人口增长速度开始变慢,但是总数还是不断增加。
2 每个国家每年人口变化情况

class DrawLine:
    def line_charts(self,country):
        line = Line(init_opts=opts.InitOpts(width='1600px',height='800px',theme=ThemeType.LIGHT))
        df2 = df[df['Country'] == country]
        x_data = pd.Series(df2.Year.values.tolist(),dtype=int)
        y_data = pd.Series(df2.Population.values.tolist(),dtype=int)
        print(df2)
        print(type(x_data))#类型为list,但是pyecharts要求类型为int类型.不可x_data=df2.Year.values.tolist(),需要转换数据类型 list->int
        print(type(y_data))
        line.add_xaxis(x_data)
        line.add_yaxis(
            series_name=country,
            y_axis=y_data,
            label_opts=opts.LabelOpts(is_show=False),
        )
        line.set_global_opts(
            title_opts=opts.TitleOpts(title=country+'每年人口变化情况'),
            tooltip_opts=opts.TooltipOpts(trigger='axis',position='right'),
            xaxis_opts=opts.AxisOpts(
                type_='category',
                boundary_gap=False,
            ),
            yaxis_opts=opts.AxisOpts(
                type_='value',
                splitline_opts=opts.SplitLineOpts(is_show=True),
            )
        )
        return line
 if __name__ == '__main__':
    #1955~2020任意国家人口总数变化
    d = DrawLine()
    country = input('Please input the Country Name:\n')
    line = d.line_charts(country)
    line.render('YearlyTrendOfCountry.html')

运行结果:

Please input the Country Name:
India

在这里插入图片描述
从折线图可以看出,印度在1955到2016年间人口总数急剧上升;从2016年开始,人口增长速度开始放慢。
3 某年人口老龄化程度最严重的前10位和倒数10位国家–人口老龄化程度通常用年龄中位数来衡量

class populationAgingTop:
    def drawBar(self,year):
        df3 = df[df['Year']==year].sort_values(by='Median Age',ascending=False)
        df3_Top = df3[:10]#取排名前10位信息
        x_data1 = df3_Top.Country.values.tolist()
        y_data1 = df3_Top['Median Age'].values.tolist()
        print(x_data1)
        print(y_data1)
        #前10位
        barTop = Bar()
        barTop.add_xaxis(x_data1)
        barTop.add_yaxis(
            year,
            y_data1,
            label_opts=opts.LabelOpts(is_show=False),
        )
        barTop.set_global_opts(
            title_opts=opts.TitleOpts(title=str(year)+'年人口老龄化程度最严重的前10位国家'),
            tooltip_opts=opts.TooltipOpts(trigger='axis'),
            legend_opts=opts.LegendOpts(is_show=True),
        )
        return barTop
class populationAgingBottom:
    def drawBar2(self,year):
        #降序排列后取倒数10位,需排除0
        df3 = df[df['Year'] == year].sort_values(by='Median Age', ascending=False)
        df3_Bottom = df3[df3['Median Age'] > 0]
        print(df3_Bottom['Median Age'][-10:])
        # 倒数10位
        x_data2 = df3_Bottom['Country'][-10:].values.tolist()
        y_data2 = df3_Bottom['Median Age'][-10:].values.tolist()
        print(x_data2)
        print(y_data2)
        barBottom = Bar()
        barBottom.add_xaxis(x_data2)
        barBottom.add_yaxis(
            "",
            y_data2,
            label_opts=opts.LabelOpts(is_show=False),
        )
        barBottom.set_global_opts(
            title_opts=opts.TitleOpts(title=str(year)+'年人口老龄化程度最轻的前10位国家',pos_top='48%'),
            tooltip_opts=opts.TooltipOpts(trigger='axis'),
            legend_opts=opts.LegendOpts(is_show=True,pos_top='48%'),
        )
        barBottom.reversal_axis()
        return barBottom
 if __name__ == '__main__':
    #人口老龄化前10和倒数第10
    year = input('Please input the year:\n')
    AgingTop = populationAgingTop()
    Top_10 = AgingTop.drawBar(int(year))
    AgingBottom = populationAgingBottom()
    #一定要将year强制转换为整型,对应调用函数Year数据类型为int.而输入的year为string!!!!!
    Bottom_10 = AgingBottom.drawBar2(int(year))
    grid = (
       Grid(init_opts=opts.InitOpts(width='1600px',height='800px',theme=ThemeType.LIGHT))
        .add(Top_10,grid_opts=opts.GridOpts(pos_bottom='60%'))
        .add(Bottom_10,grid_opts=opts.GridOpts(pos_top='60%'))
        .render('PopulationAging.html')
    )

运行结果:

Please input the year:
2020

在这里插入图片描述
4 任意年份世界人口分布情况—世界地图

class globalMap:
    def drawMap(self,year):
        map = Map(init_opts=opts.InitOpts(width='1600px',height='800px',theme=ThemeType.MACARONS))
        dfMap = df[df['Year']==year]
        x_data = dfMap['Country'].values.tolist()
        y_data = dfMap['Population'].values.tolist()
        print(x_data)
        print(y_data)
        map.add(
            year,
            [list(z) for z in zip(x_data,y_data)],
            'world',
        )
        map.set_global_opts(
            title_opts=opts.TitleOpts(title=str(year)+'年世界人口分布地图'),
            visualmap_opts=opts.VisualMapOpts(max_=max(y_data))
        )
        return map
if __name__ == '__main__':
    #世界地图
    gMap = globalMap()
    map = gMap.drawMap(int(year))
    map.render('GlobalMap.html')

运行结果:
在这里插入图片描述
发现世界人口数目较多的国家集中分布在亚欧大陆,尤其是中国和印度的人口总数排名极其靠前
5 任意年份人口总数最多和最少的三位国家

class populationRanking:
    def drawBarTop(self,year):
        dfTop = df[df['Year']==year].sort_values(by='Population',ascending=False)
        x_data = dfTop[:3].Country.values.tolist()
        y_data = dfTop[:3].Population.values.tolist()
        print(x_data)
        print(y_data)
        bar = Bar()
        bar.add_xaxis(x_data)
        bar.add_yaxis(
            year,
            y_data,
            label_opts=opts.LabelOpts(is_show=False),
        )
        bar.set_global_opts(
            title_opts=opts.TitleOpts(title=str(year)+'年人口数目最多的三位国家'),
            legend_opts=opts.LegendOpts(is_show=True,pos_left='center'),

        )
        bar.reversal_axis()
        return bar
    def drawBarBottom(self,year):
        dfBottom = df[df['Year']==year].sort_values(by='Population',ascending=False)
        dfBottom = dfBottom[dfBottom['Population']>0]#排除0的可能性/空值
        x_data =dfBottom['Country'][-3:].values.tolist()
        y_data = dfBottom['Population'][-3:].values.tolist()
        print(x_data)
        print(y_data)
        bar = Bar()
        bar.add_xaxis(x_data)
        bar.add_yaxis(
            year,
            y_data,
            label_opts=opts.LabelOpts(is_show=False),
        )
        bar.set_global_opts(
            title_opts=opts.TitleOpts(title=str(year)+'年人口数目最少的三位国家',pos_top='60%'),
            legend_opts=opts.LegendOpts(is_show=True),
        )
        bar.reversal_axis()
        return bar
if __name__ == '__main__':
   #前3和倒数3位国家
   	year = input('Please input the year:\n')
    Rank = populationRanking()
    Rank.gridBar(int(year))

运行结果:
在这里插入图片描述

数据采集模块是全国热门旅游景点数据分析系统的重要组成部分,它主要负责从不同的数据源中获取数据,并将其保存到数据库中,以供后续的数据分析和可视化。在本文中,我们将介绍如何使用Pandas和Pyecharts库来实现这一模块的设计。 1. 确定数据源 在设计数据采集模块之前,我们需要确定数据源。对于全国热门旅游景点数据分析系统,我们可以从以下几个方面获取数据: - 政府公开数据:例如国家统计局、旅游部等官方网站上公布的数据。 - 第三方数据提供商:例如百度地图、高德地图等提供的POI(兴趣点)数据。 - 社交媒体数据:例如微博、微信等社交媒体平台上用户发布的旅游相关内容。 根据不同的数据源,我们需要采用不同的数据获取方式和数据处理方式。在本文中,我们以政府公开数据为例进行介绍。 2. 获取数据 获取政府公开数据可以通过爬虫的方式来实现。在Python中,我们可以使用requests和beautifulsoup4库来实现网页爬取功能。下面是一个简单的示例代码: ``` import requests from bs4 import BeautifulSoup url = 'http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2020/index.html' response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') print(soup.prettify()) ``` 在这个例子中,我们使用requests库来发送HTTP请求,获取网页内容。然后,使用beautifulsoup4库来解析HTML页面,提取所需的数据。 3. 数据处理 获取数据后,我们需要对其进行处理,以便存储到数据库中。在Python中,我们可以使用Pandas库来处理数据。Pandas是一个基于NumPy的数据处理库,提供了高效的数据结构和数据分析工具。下面是一个示例代码: ``` import pandas as pd data = [{'name': '北京市', 'population': 2154.20}, {'name': '上海市', 'population': 2424.78}, {'name': '广州市', 'population': 1500.00}] df = pd.DataFrame(data) print(df) ``` 在这个例子中,我们使用Pandas库创建了一个DataFrame对象,并将数据存储到其中。DataFrame是Pandas库中最常用的数据结构,类似于表格,可以方便地进行数据处理和分析。 4. 数据存储 最后,我们需要将处理后的数据保存到数据库中。在Python中,我们可以使用SQLAlchemy库来实现数据库连接和数据存储。SQLAlchemy是一个Python SQL工具包和ORM框架,可以方便地处理各种数据库操作。下面是一个示例代码: ``` from sqlalchemy import create_engine engine = create_engine('sqlite:///data.db') df.to_sql('cities', engine, if_exists='replace') ``` 在这个例子中,我们使用SQLAlchemy库创建了一个SQLite数据库,并将DataFrame对象存储到其中。to_sql方法可以将DataFrame对象转换为数据库表格,并将其保存到数据库中。 5. 数据可视化 除了数据存储之外,我们还可以使用Pyecharts实现数据可视化。Pyecharts是一个基于Echarts的Python可视化库,可以方便地创建各种交互式图表和地图。下面是一个示例代码: ``` from pyecharts.charts import Bar from pyecharts import options as opts bar = Bar() bar.add_xaxis(['北京市', '上海市', '广州市']) bar.add_yaxis('人口', [2154.20, 2424.78, 1500.00]) bar.set_global_opts(title_opts=opts.TitleOpts(title='城市人口')) bar.render('population.html') ``` 在这个例子中,我们使用Bar类创建了一个柱状图,并将数据添加到其中。然后,设置图表的标题和保存路径,并使用render方法将图表保存为HTML文件。 通过以上步骤,我们就可以实现数据采集模块的设计。在实际应用中,我们需要根据具体的需求和数据源进行修改和调整。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值