数据分析案例:亚洲国家人口数据计算(python金融数据分析案例)

大家好!今天让小编来大家介绍下关于数据分析案例:亚洲国家人口数据计算的问题,以下是酷知号的小编对此问题的归纳整理,让我们一起来看看吧。

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

数据截图:

数据下载地址:链接:https://pan.baidu.com/s/1dGHwAC5 密码:nfd2

该数据包含了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 np
import numpy as np
In [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_index
Out[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_index
Out[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
约旦河西岸和加沙人口为:	4422143
2.计算朝鲜历史各个时期人口数据
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年人口数据为:53437159
4.计算每一个国家历史平均人口数据
In [47]:#去除第一行时间数据
every_country_data = np.delete(p_data.T,0,axis=0)
​
#处理数据中的空值为0
every_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
约旦河西岸和加沙国家历史平均人口数据:3424896
5.计算亚洲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.6724
6. 计算印度、柬埔寨、阿富汗在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.0
2012年印度,柬埔寨,阿富汗国家的人口总和为:1308148697, 平均人口为:436049565.667
2013年印度,柬埔寨,阿富汗国家的人口总和为:1325259938, 平均人口为:441753312.667
7.计算任意两个国家之间的人口差数据
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

以上就是小编对于数据分析案例:亚洲国家人口数据计算问题和相关问题的解答了,数据分析案例:亚洲国家人口数据计算的问题希望对你有用!

文章来自互联网,只做分享使用。发布者:酷知号,转转请注明出处:https://www.kuzhihao.com/article/328193.html

(0)
上一篇 2023年7月25日 12:40
下一篇 2023年7月25日 12:41

相关推荐