数据分析案例:亚洲国家人口数据计算

来源:互联网 发布:资源最优化论文 编辑:程序博客网 时间:2024/04/30 09:35


数据截图:

该数据包含了2006年-2015年10年间亚洲地区人口数量数据,共10行50列数据。我们需要使用Numpy完成如下数据任务:

  1. 计算2015年各个国家人口数据
  2. 计算朝鲜历史各个时期人口数据
  3. 计算缅甸2014年的人口数据
  4. 计算每一个国家历史平均人口数据
  5. 计算亚洲2015年总人口,及平均人口
  6. 计算印度、柬埔寨、阿富汗在2011、2012、2013年总人口及平均人口
  7. 计算任意两个国家之间的人口差数据
  8. 计算2012年亚洲人口数量排名前10的国家
import numpy as npimport numpy as npIn [42]:#国家索引country_index = np.array(        open('亚洲国家20年人口数据-gb2312.csv').readline()[:-1].split(','))#读取人口数据p_data = np.genfromtxt(    '亚洲国家20年人口数据-gb2312.csv',    delimiter = ',',    skip_header = 1,    dtype = np.str)#时间索引time_index = p_data[:,0]#数据行索引time_index#国家索引country_index = np.array(        open('亚洲国家20年人口数据-gb2312.csv').readline()[:-1].split(','))​#读取人口数据p_data = np.genfromtxt(    '亚洲国家20年人口数据-gb2312.csv',    delimiter = ',',    skip_header = 1,    dtype = np.str)​#时间索引time_index = p_data[:,0]​#数据行索引time_indexOut[42]:array(['2015', '2014', '2013', '2012', '2011', '2010', '2009', '2008',       '2007', '2006', '2005', '2004', '2003', '2002', '2001', '2000',       '1999', '1998', '1997', '1996'],      dtype='<U10')In [43]:#数据索引country_indexOut[43]:array(['时间', '阿富汗', '巴林', '孟加拉国', '不丹', '文莱', '缅甸', '柬埔寨', '塞浦路斯', '朝鲜',       '中国香港', '印度', '印度尼西亚', '伊朗', '伊拉克', '以色列', '日本', '约旦', '科威特', '老挝',       '黎巴嫩', '中国澳门', '马来西亚', '马尔代夫', '蒙古', '尼泊尔', '阿曼', '巴基斯坦', '巴勒斯坦',       '菲律宾', '卡塔尔', '沙特阿拉伯', '新加坡', '韩国', '斯里兰卡', '叙利亚', '泰国', '土耳其',       '阿联酋', '也门', '越南', '中国台湾', '东帝汶', '哈萨克斯坦', '吉尔吉斯斯坦', '塔吉克斯坦',       '土库曼斯坦', '乌兹别克斯坦', '马恩岛', '约旦河西岸和加沙'],      dtype='<U8')1.计算2015年各个国家人口数据In [44]:year = '2015'​#获取year年所有国家人口数据p_by_year = p_data[time_index == year]​#取前国家显示p_by_year = p_by_year[0]​#给数据添加国家名称print('%s年各个国家人口数据:'%year)print('--------------------------')​for country_name,country_data in zip(country_index[1:],p_by_year[1:]):    print('%s人口为:\t%s'%(country_name,country_data))2015年各个国家人口数据:--------------------------阿富汗人口为:32526562巴林人口为:1377237孟加拉国人口为:160995642不丹人口为:774830文莱人口为:423188缅甸人口为:53897154柬埔寨人口为:15577899塞浦路斯人口为:1165300朝鲜人口为:25155317中国香港人口为:7305700印度人口为:1311050527印度尼西亚人口为:257563815伊朗人口为:79109272伊拉克人口为:36423395以色列人口为:8380100日本人口为:126958472约旦人口为:7594547科威特人口为:3892115老挝人口为:6802023黎巴嫩人口为:5850743中国澳门人口为:587606马来西亚人口为:30331007马尔代夫人口为:409163蒙古人口为:2959134尼泊尔人口为:28513700阿曼人口为:4490541巴基斯坦人口为:188924874巴勒斯坦人口为:菲律宾人口为:100699395卡塔尔人口为:2235355沙特阿拉伯人口为:31540372新加坡人口为:5535002韩国人口为:50617045斯里兰卡人口为:20966000叙利亚人口为:18502413泰国人口为:67959359土耳其人口为:78665830阿联酋人口为:9156963也门人口为:26832215越南人口为:91713300中国台湾人口为:东帝汶人口为:1184765哈萨克斯坦人口为:17544126吉尔吉斯斯坦人口为:5956900塔吉克斯坦人口为:8481855土库曼斯坦人口为:5373502乌兹别克斯坦人口为:31298900马恩岛人口为:87780约旦河西岸和加沙人口为:44221432.计算朝鲜历史各个时期人口数据In [45]:country = '朝鲜'#先查找朝鲜在数组的索引country_where = np.argwhere(country_index == country)[0][0]​#按照索引计算人口数据print('%s各个年份人口数据:'%country)print('=============================')​for data in zip(time_index,p_data[:,country_where]):    print(data)    print('==============================')朝鲜各个年份人口数据:=============================('2015', '25155317')('2014', '25026772')('2013', '24895705')('2012', '24763353')('2011', '24631359')('2010', '24500506')('2009', '24371806')('2008', '24243829')('2007', '24111945')('2006', '23969897')('2005', '23813324')('2004', '23639296')('2003', '23449173')('2002', '23248053')('2001', '23043441')('2000', '22840218')('1999', '22641747')('1998', '22444986')('1997', '22240826')('1996', '22016510')==============================3.计算缅甸2014年人口数据In [46]:country = '缅甸'year = '2014'​country_data = p_data[time_index==year][0][np.argwhere(country_index == country)[0][0]]​print('%s在%s年人口数据为:%s'%(country,year,country_data))缅甸在2014年人口数据为:534371594.计算每一个国家历史平均人口数据In [47]:#去除第一行时间数据every_country_data = np.delete(p_data.T,0,axis=0)​#处理数据中的空值为0every_country_data = np.where(every_country_data=='',0,every_country_data).astype(np.int32)​​#计算每一个国家平均人口avg_data = np.mean(every_country_data,axis=1).astype(np.int32)​print('各个国家历史平均人口数据')print('=========================')​#各个国家名称country_name = country_index[1:]​for country_name,data in zip(country_index[1:],avg_data):    print('%s国家历史平均人口数据:%s'%(country_name,data))​各个国家历史平均人口数据=========================阿富汗国家历史平均人口数据:24566255巴林国家历史平均人口数据:961489孟加拉国国家历史平均人口数据:142511842不丹国家历史平均人口数据:650117文莱国家历史平均人口数据:364592缅甸国家历史平均人口数据:49912636柬埔寨国家历史平均人口数据:13384934塞浦路斯国家历史平均人口数据:1030891朝鲜国家历史平均人口数据:23752403中国香港国家历史平均人口数据:6865960印度国家历史平均人口数据:1150202417印度尼西亚国家历史平均人口数据:228174990伊朗国家历史平均人口数据:70450024伊拉克国家历史平均人口数据:27799113以色列国家历史平均人口数据:7016715日本国家历史平均人口数据:127318832约旦国家历史平均人口数据:5723731科威特国家历史平均人口数据:2545149老挝国家历史平均人口数据:5842897黎巴嫩国家历史平均人口数据:4085650中国澳门国家历史平均人口数据:487277马来西亚国家历史平均人口数据:25966300马尔代夫国家历史平均人口数据:331258蒙古国家历史平均人口数据:2579777尼泊尔国家历史平均人口数据:25465307阿曼国家历史平均人口数据:2798350巴基斯坦国家历史平均人口数据:155928633巴勒斯坦国家历史平均人口数据:0菲律宾国家历史平均人口数据:86371329卡塔尔国家历史平均人口数据:1179044沙特阿拉伯国家历史平均人口数据:25157752新加坡国家历史平均人口数据:4551772韩国国家历史平均人口数据:48298055斯里兰卡国家历史平均人口数据:19483750叙利亚国家历史平均人口数据:18060010泰国国家历史平均人口数据:64970255土耳其国家历史平均人口数据:68492236阿联酋国家历史平均人口数据:5575669也门国家历史平均人口数据:21019667越南国家历史平均人口数据:82703935中国台湾国家历史平均人口数据:0东帝汶国家历史平均人口数据:984978哈萨克斯坦国家历史平均人口数据:15734151吉尔吉斯斯坦国家历史平均人口数据:5217475塔吉克斯坦国家历史平均人口数据:6965083土库曼斯坦国家历史平均人口数据:4799654乌兹别克斯坦国家历史平均人口数据:26807230马恩岛国家历史平均人口数据:80731约旦河西岸和加沙国家历史平均人口数据:34248965.计算亚洲2015年总人口,以及平均数In [48]:year = '2015'​#2015年亚洲各个国家人口数量every_country_data = p_data[time_index == year]​#去除第一条时间数据every_country_data = np.delete(p_data.T,0,axis=0)​#计算数据中的缺失值,并将数据类型转换为数字类型every_country_data= np.where(every_country_data=='',0,every_country_data).astype(np.int32)​#计算平均值avg_data = np.mean(every_country_data)​#计算总人口total_data = np.sum(every_country_data)​print('亚洲%s年总人口数据:%s,平均人后数据是:%s'%(year,total_data,avg_data))亚洲2015年总人口数据:792297067,平均人后数据是:53399902.67246. 计算印度、柬埔寨、阿富汗在2011、2012、2013年总人口及平均人口In [54]:contry = ['印度','柬埔寨','阿富汗']​year = ['2011','2012','2013']#先获得所有国家11/12/13年的人口数据all_country_data_by_year = []for y in year:    all_country_data_by_year.append(p_data[time_index == y][0])​# #计算国家所对应的列索引indexes = []for c in contry:    indexes.append(np.argwhere(country_index == c)[0][0])​# #计算指定国家的数据all_country_data_by_year = np.array(all_country_data_by_year)all_country_data_by_year = all_country_data_by_year[:,indexes]​# #处理数据中可能存在的缺失值all_country_data_by_year = np.where(all_country_data_by_year == '',0,all_country_data_by_year)​all_country_data_by_year = all_country_data_by_year.astype(np.int32)# #计算每一年人口总和p_sum = all_country_data_by_year.sum(axis=1)​# #计算每一年人口平均数p_mean = all_country_data_by_year.mean(axis=1)​for y,s,m in zip(year,p_sum,p_mean):     print('%s年%s国家的人口总和为:%s, 平均人口为:%s' % (y, ','.join(contry), s, m))2011年印度,柬埔寨,阿富汗国家的人口总和为:1290848277, 平均人口为:430282759.02012年印度,柬埔寨,阿富汗国家的人口总和为:1308148697, 平均人口为:436049565.6672013年印度,柬埔寨,阿富汗国家的人口总和为:1325259938, 平均人口为:441753312.6677.计算任意两个国家之间的人口差数据In [57]:country1 = '柬埔寨'country2 = '越南'year = '2013'​#计算2015年人口数据data_2015 = p_data[time_index == year]data_2015 = np.where(data_2015 == '',0,data_2015).astype(np.int32)​#获得两个国家的人口数据country1_data = data_2015.T[country_index == country1][0][0]country2_data = data_2015.T[country_index == country2][0][0]​print('%s和%s的人口差是:%s !'%(country1,country2,np.abs(country1_data-country2_data)))柬埔寨和越南的人口差是:74680936 !8.计算2012年亚洲人口数量排名前10的国家In [60]:#计算2012年亚洲人口数据year = '2012'​#获得2012年数据data_2012 = p_data[time_index == year][0][1:]#处理缺失值data_2012 = np.where(data_2012 == '',0,data_2012)#数据转换为数字类型data_2012 = data_2012.astype(np.int32)​#对结果排序sorted_index = np.argsort(data_2012)​#人口数量前10的国家ret_data = data_2012[sorted_index][::-1][10:]ret_index = country_index[1:][sorted_index][::-1][:10]​#输出结果for a,b in zip(ret_index,ret_data):    print('国家:%s人口:%s'%(a,b))国家:印度人口:52543841国家:印度尼西亚人口:50004441国家:巴基斯坦人口:32957622国家:孟加拉国人口:29774500国家:日本人口:29726803国家:菲律宾人口:29496047国家:越南人口:29021940国家:伊朗人口:27500515国家:土耳其人口:24882792国家:泰国人口:24763353


原创粉丝点击