1960-2018年世界各国GDP排名变化--Python动图实现

1960-2018年世界各国GDP排名变化--Python动图实现

数据

数据来源:https://data.worldbank.org.cn/

去看原文

代码

#coding=utf-8

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import PlotUtil
import sys

def is_country(x, fields):
  for field in fields:
    if field in x.encode("utf-8"):
      return False
  return True

def main():
  # 读取数据
  gdp = pd.read_excel("API_NY.GDP.MKTP.CD_DS2_zh_excel_v2_103680.xls")

  # 筛选:去掉世界、一些地区性的数据
  fields = ["世界", "收入国家", "地区", "南亚", "组织成员", "人口","北美", "联盟", "IBRD", "IDA", "重债穷国"]
  gdp["is_country"] = gdp.apply(lambda x: is_country(x["Country Name"], fields), axis = 1)
  gdp = gdp[gdp["is_country"] == True]

  datas = []
  for year in range(1960, 2019):
    year = str(year)
    gdp.sort_values(year, inplace = True, ascending = False)
    print(year,"==========================================")
    print(gdp[0:15][["Country Name", year]])

    data = gdp[0:15] #排序,取前15名
    data.sort_values(year, inplace = True, ascending = True)
    data[year] = data[year] / 10 ** 11

    datas.append([year, data[year].tolist(), data["Country Name"].tolist()])

  # 绘制动态图
  plot = PlotUtil.Plot(datas)
  plot.showGif("gdp.gif")

if __name__ == '__main__':
  main()

结果展示

在这里插入图片描述
原文有源码,更多内容,请关注地学数据处理分析。
在这里插入图片描述

评论 27
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值