数据分析案例:亚洲国家人口数据计算
生活随笔
收集整理的這篇文章主要介紹了
数据分析案例:亚洲国家人口数据计算
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
數(shù)據(jù)截圖:
數(shù)據(jù)下載地址:鏈接:https://pan.baidu.com/s/1dGHwAC5 密碼:nfd2
該數(shù)據(jù)包含了2006年-2015年10年間亞洲地區(qū)人口數(shù)量數(shù)據(jù),共10行50列數(shù)據(jù)。我們需要使用Numpy完成如下數(shù)據(jù)任務(wù):
- 計(jì)算2015年各個(gè)國家人口數(shù)據(jù)
- 計(jì)算朝鮮歷史各個(gè)時(shí)期人口數(shù)據(jù)
- 計(jì)算緬甸2014年的人口數(shù)據(jù)
- 計(jì)算每一個(gè)國家歷史平均人口數(shù)據(jù)
- 計(jì)算亞洲2015年總?cè)丝?#xff0c;及平均人口
- 計(jì)算印度、柬埔寨、阿富汗在2011、2012、2013年總?cè)丝诩捌骄丝?/li>
- 計(jì)算任意兩個(gè)國家之間的人口差數(shù)據(jù)
- 計(jì)算2012年亞洲人口數(shù)量排名前10的國家
import numpy as np
import numpy as np
In [42]:#國家索引
country_index = np.array(open('亞洲國家20年人口數(shù)據(jù)-gb2312.csv').readline()[:-1].split(',')
)#讀取人口數(shù)據(jù)
p_data = np.genfromtxt('亞洲國家20年人口數(shù)據(jù)-gb2312.csv',delimiter = ',',skip_header = 1,dtype = np.str
)#時(shí)間索引
time_index = p_data[:,0]#數(shù)據(jù)行索引
time_index
#國家索引
country_index = np.array(open('亞洲國家20年人口數(shù)據(jù)-gb2312.csv').readline()[:-1].split(',')
)
?
#讀取人口數(shù)據(jù)
p_data = np.genfromtxt('亞洲國家20年人口數(shù)據(jù)-gb2312.csv',delimiter = ',',skip_header = 1,dtype = np.str
)
?
#時(shí)間索引
time_index = p_data[:,0]
?
#數(shù)據(jù)行索引
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]:#數(shù)據(jù)索引
country_index
Out[43]:
array(['時(shí)間', '阿富汗', '巴林', '孟加拉國', '不丹', '文萊', '緬甸', '柬埔寨', '塞浦路斯', '朝鮮','中國香港', '印度', '印度尼西亞', '伊朗', '伊拉克', '以色列', '日本', '約旦', '科威特', '老撾','黎巴嫩', '中國澳門', '馬來西亞', '馬爾代夫', '蒙古', '尼泊爾', '阿曼', '巴基斯坦', '巴勒斯坦','菲律賓', '卡塔爾', '沙特阿拉伯', '新加坡', '韓國', '斯里蘭卡', '敘利亞', '泰國', '土耳其','阿聯(lián)酋', '也門', '越南', '中國臺(tái)灣', '東帝汶', '哈薩克斯坦', '吉爾吉斯斯坦', '塔吉克斯坦','土庫曼斯坦', '烏茲別克斯坦', '馬恩島', '約旦河西岸和加沙'],dtype='<U8')
1.計(jì)算2015年各個(gè)國家人口數(shù)據(jù)
In [44]:year = '2015'
?
#獲取year年所有國家人口數(shù)據(jù)
p_by_year = p_data[time_index == year]
?
#取前國家顯示
p_by_year = p_by_year[0]
?
#給數(shù)據(jù)添加國家名稱
print('%s年各個(gè)國家人口數(shù)據(jù):'%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年各個(gè)國家人口數(shù)據(jù):
--------------------------
阿富汗人口為: 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
阿聯(lián)酋人口為: 9156963
也門人口為: 26832215
越南人口為: 91713300
中國臺(tái)灣人口為:
東帝汶人口為: 1184765
哈薩克斯坦人口為: 17544126
吉爾吉斯斯坦人口為: 5956900
塔吉克斯坦人口為: 8481855
土庫曼斯坦人口為: 5373502
烏茲別克斯坦人口為: 31298900
馬恩島人口為: 87780
約旦河西岸和加沙人口為: 4422143
2.計(jì)算朝鮮歷史各個(gè)時(shí)期人口數(shù)據(jù)
In [45]:country = '朝鮮'
#先查找朝鮮在數(shù)組的索引
country_where = np.argwhere(country_index == country)[0][0]
?
#按照索引計(jì)算人口數(shù)據(jù)
print('%s各個(gè)年份人口數(shù)據(jù):'%country)
print('=============================')
?
for data in zip(time_index,p_data[:,country_where]):print(data)print('==============================')
朝鮮各個(gè)年份人口數(shù)據(jù):
=============================
('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.計(jì)算緬甸2014年人口數(shù)據(jù)
In [46]:country = '緬甸'
year = '2014'
?
country_data = p_data[time_index==year][0][np.argwhere(country_index == country)[0][0]]
?
print('%s在%s年人口數(shù)據(jù)為:%s'%(country,year,country_data))
緬甸在2014年人口數(shù)據(jù)為:53437159
4.計(jì)算每一個(gè)國家歷史平均人口數(shù)據(jù)
In [47]:#去除第一行時(shí)間數(shù)據(jù)
every_country_data = np.delete(p_data.T,0,axis=0)
?
#處理數(shù)據(jù)中的空值為0
every_country_data = np.where(every_country_data=='',0,every_country_data).astype(np.int32)
?
?
#計(jì)算每一個(gè)國家平均人口
avg_data = np.mean(every_country_data,axis=1).astype(np.int32)
?
print('各個(gè)國家歷史平均人口數(shù)據(jù)')
print('=========================')
?
#各個(gè)國家名稱
country_name = country_index[1:]
?
for country_name,data in zip(country_index[1:],avg_data):print('%s國家歷史平均人口數(shù)據(jù):%s'%(country_name,data))
?
各個(gè)國家歷史平均人口數(shù)據(jù)
=========================
阿富汗國家歷史平均人口數(shù)據(jù):24566255
巴林國家歷史平均人口數(shù)據(jù):961489
孟加拉國國家歷史平均人口數(shù)據(jù):142511842
不丹國家歷史平均人口數(shù)據(jù):650117
文萊國家歷史平均人口數(shù)據(jù):364592
緬甸國家歷史平均人口數(shù)據(jù):49912636
柬埔寨國家歷史平均人口數(shù)據(jù):13384934
塞浦路斯國家歷史平均人口數(shù)據(jù):1030891
朝鮮國家歷史平均人口數(shù)據(jù):23752403
中國香港國家歷史平均人口數(shù)據(jù):6865960
印度國家歷史平均人口數(shù)據(jù):1150202417
印度尼西亞國家歷史平均人口數(shù)據(jù):228174990
伊朗國家歷史平均人口數(shù)據(jù):70450024
伊拉克國家歷史平均人口數(shù)據(jù):27799113
以色列國家歷史平均人口數(shù)據(jù):7016715
日本國家歷史平均人口數(shù)據(jù):127318832
約旦國家歷史平均人口數(shù)據(jù):5723731
科威特國家歷史平均人口數(shù)據(jù):2545149
老撾國家歷史平均人口數(shù)據(jù):5842897
黎巴嫩國家歷史平均人口數(shù)據(jù):4085650
中國澳門國家歷史平均人口數(shù)據(jù):487277
馬來西亞國家歷史平均人口數(shù)據(jù):25966300
馬爾代夫國家歷史平均人口數(shù)據(jù):331258
蒙古國家歷史平均人口數(shù)據(jù):2579777
尼泊爾國家歷史平均人口數(shù)據(jù):25465307
阿曼國家歷史平均人口數(shù)據(jù):2798350
巴基斯坦國家歷史平均人口數(shù)據(jù):155928633
巴勒斯坦國家歷史平均人口數(shù)據(jù):0
菲律賓國家歷史平均人口數(shù)據(jù):86371329
卡塔爾國家歷史平均人口數(shù)據(jù):1179044
沙特阿拉伯國家歷史平均人口數(shù)據(jù):25157752
新加坡國家歷史平均人口數(shù)據(jù):4551772
韓國國家歷史平均人口數(shù)據(jù):48298055
斯里蘭卡國家歷史平均人口數(shù)據(jù):19483750
敘利亞國家歷史平均人口數(shù)據(jù):18060010
泰國國家歷史平均人口數(shù)據(jù):64970255
土耳其國家歷史平均人口數(shù)據(jù):68492236
阿聯(lián)酋國家歷史平均人口數(shù)據(jù):5575669
也門國家歷史平均人口數(shù)據(jù):21019667
越南國家歷史平均人口數(shù)據(jù):82703935
中國臺(tái)灣國家歷史平均人口數(shù)據(jù):0
東帝汶國家歷史平均人口數(shù)據(jù):984978
哈薩克斯坦國家歷史平均人口數(shù)據(jù):15734151
吉爾吉斯斯坦國家歷史平均人口數(shù)據(jù):5217475
塔吉克斯坦國家歷史平均人口數(shù)據(jù):6965083
土庫曼斯坦國家歷史平均人口數(shù)據(jù):4799654
烏茲別克斯坦國家歷史平均人口數(shù)據(jù):26807230
馬恩島國家歷史平均人口數(shù)據(jù):80731
約旦河西岸和加沙國家歷史平均人口數(shù)據(jù):3424896
5.計(jì)算亞洲2015年總?cè)丝?#xff0c;以及平均數(shù)
In [48]:year = '2015'
?
#2015年亞洲各個(gè)國家人口數(shù)量
every_country_data = p_data[time_index == year]
?
#去除第一條時(shí)間數(shù)據(jù)
every_country_data = np.delete(p_data.T,0,axis=0)
?
#計(jì)算數(shù)據(jù)中的缺失值,并將數(shù)據(jù)類型轉(zhuǎn)換為數(shù)字類型
every_country_data= np.where(every_country_data=='',0,every_country_data).astype(np.int32)
?
#計(jì)算平均值
avg_data = np.mean(every_country_data)
?
#計(jì)算總?cè)丝?total_data = np.sum(every_country_data)
?
print('亞洲%s年總?cè)丝跀?shù)據(jù):%s,平均人后數(shù)據(jù)是:%s'%(year,total_data,avg_data))
亞洲2015年總?cè)丝跀?shù)據(jù):792297067,平均人后數(shù)據(jù)是:53399902.6724
6. 計(jì)算印度、柬埔寨、阿富汗在2011、2012、2013年總?cè)丝诩捌骄丝?In [54]:contry = ['印度','柬埔寨','阿富汗']
?
year = ['2011','2012','2013']
#先獲得所有國家11/12/13年的人口數(shù)據(jù)
all_country_data_by_year = []
for y in year:all_country_data_by_year.append(p_data[time_index == y][0])
?
# #計(jì)算國家所對(duì)應(yīng)的列索引
indexes = []
for c in contry:indexes.append(np.argwhere(country_index == c)[0][0])
?
# #計(jì)算指定國家的數(shù)據(jù)
all_country_data_by_year = np.array(all_country_data_by_year)
all_country_data_by_year = all_country_data_by_year[:,indexes]
?
# #處理數(shù)據(jù)中可能存在的缺失值
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)
# #計(jì)算每一年人口總和
p_sum = all_country_data_by_year.sum(axis=1)
?
# #計(jì)算每一年人口平均數(shù)
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.計(jì)算任意兩個(gè)國家之間的人口差數(shù)據(jù)
In [57]:country1 = '柬埔寨'
country2 = '越南'
year = '2013'
?
#計(jì)算2015年人口數(shù)據(jù)
data_2015 = p_data[time_index == year]
data_2015 = np.where(data_2015 == '',0,data_2015).astype(np.int32)
?
#獲得兩個(gè)國家的人口數(shù)據(jù)
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.計(jì)算2012年亞洲人口數(shù)量排名前10的國家
In [60]:#計(jì)算2012年亞洲人口數(shù)據(jù)
year = '2012'
?
#獲得2012年數(shù)據(jù)
data_2012 = p_data[time_index == year][0][1:]
#處理缺失值
data_2012 = np.where(data_2012 == '',0,data_2012)
#數(shù)據(jù)轉(zhuǎn)換為數(shù)字類型
data_2012 = data_2012.astype(np.int32)
?
#對(duì)結(jié)果排序
sorted_index = np.argsort(data_2012)
?
#人口數(shù)量前10的國家
ret_data = data_2012[sorted_index][::-1][10:]
ret_index = country_index[1:][sorted_index][::-1][:10]
?
#輸出結(jié)果
for a,b in zip(ret_index,ret_data):print('國家:%s人口:%s'%(a,b))
國家:印度人口:52543841
國家:印度尼西亞人口:50004441
國家:巴基斯坦人口:32957622
國家:孟加拉國人口:29774500
國家:日本人口:29726803
國家:菲律賓人口:29496047
國家:越南人口:29021940
國家:伊朗人口:27500515
國家:土耳其人口:24882792
國家:泰國人口:24763353
總結(jié)
以上是生活随笔為你收集整理的数据分析案例:亚洲国家人口数据计算的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中国排名前100的IT公司
- 下一篇: 疯狂坦克 高级教程(二)