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

468490-b82b93a279ea8094.png
image.png

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

计算2015年各个国家人口数据
计算朝鲜历史各个时期人口数据
计算缅甸2014年的人口数据
计算每一个国家历史平均人口数据
计算亚洲2015年总人口,及平均人口
计算印度、柬埔寨、阿富汗在2011、2012、2013年总人口及平均人口
计算任意两个国家之间的人口差数据
计算2012年亚洲人口数量排名前10的国家

时间,阿富汗,巴林,孟加拉国,不丹,文莱,缅甸,柬埔寨,塞浦路斯,朝鲜,中国香港,印度,印度尼西亚,伊朗,伊拉克,以色列,日本,约旦,科威特,老挝,黎巴嫩,中国澳门,马来西亚,马尔代夫,蒙古,尼泊尔,阿曼,巴基斯坦,巴勒斯坦,菲律宾,卡塔尔,沙特阿拉伯,新加坡,韩国,斯里兰卡,叙利亚,泰国,土耳其,阿联酋,也门,越南,中国台湾,东帝汶,哈萨克斯坦,吉尔吉斯斯坦,塔吉克斯坦,土库曼斯坦,乌兹别克斯坦,马恩岛,约旦河西岸和加沙
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
2014,31627506,1361930,159077513,765008,417394,53437159,15328136,1153658,25026772,7241700,1295291543,254454778,78143644,35273293,8215700,127131800,7416083,3753121,6689300,5612096,577914,29901997,401000,2909871,28174724,4236057,185044286,,99138690,2172065,30886545,5469724,50423955,20771000,18772481,67725979,77523788,9086139,26183676,90728900,,1157360,17289224,5835500,8295840,5307188,30757700,87127,4294682
2013,30682500,1349427,157157394,754637,411499,52983829,15078564,1141652,24895705,7187500,1279498874,251268276,77152445,34107366,8059500,127338621,7214832,3593689,6579985,5286990,568056,29465372,393000,2859174,27834981,3906912,181192646,,97571676,2101288,30201051,5399162,50219669,20585000,19322593,67451422,76223639,9039978,25533217,89759500,,1129315,17035275,5719600,8111894,5240088,30243200,86462,4169506
2012,29726803,1333577,155257387,743711,405512,52543841,14832255,1129303,24763353,7154600,1263589639,248037853,76156975,32957622,7910500,127561489,6994451,3419581,6473050,4924257,557763,29021940,385000,2808339,27500515,3545192,177392252,,96017322,2015624,29496047,5312437,50004441,20425000,19978756,67164130,74849187,8952542,24882792,88809200,,1102076,16791425,5607200,7930929,5172941,29774500,85779,4046901
2011,28809167,1306014,153405612,732246,399443,52125411,14593099,1116644,24631359,7071600,1247446011,244808254,75184322,31867758,7765900,127817277,6760371,3239181,6366909,4591698,546682,28572970,377000,2759074,27179237,3210003,173669648,,94501233,1905437,28788438,5183688,49779000,20271000,20501167,66902958,73517002,8734722,24234940,87860300,,1077602,16558676,5514600,7753925,5106672,29341200,85069,3927051
2010,27962207,1261319,151616777,720246,393302,51733013,14363586,1103647,24500506,7067800,1230984504,241613126,74253373,30868156,7623600,128070000,6517912,3059473,6260544,4337156,543656,28119500,367000,2712657,26875910,2943747,170043918,,93038902,1765513,28090647,5076732,49410000,20119000,20720602,66692024,72310416,8329453,23591972,86932500,,1057122,16323287,5447900,7581696,5041995,28562400,84327,3811102
2009,27207291,1196774,149905836,707830,387080,51369725,14144337,1090486,24371806,7003700,1214182182,238465165,73370982,29970634,7485600,128047000,6266865,2881243,6153153,4181742,521405,27661017,360000,2669572,26592666,2762073,166520983,,91641881,1591151,27409491,4987573,49182000,19968000,20566871,66548197,71261307,7705423,22954226,86025000,,1041827,16093481,5383300,7414960,4978960,27767400,83548,3702218
2008,26528741,1115777,148252473,694990,380786,51030006,13933660,1077010,24243829,6977700,1197070109,235360765,72530693,29163327,7308800,128063000,6010035,2705290,6045439,4109389,507274,27197419,362000,2629666,26325183,2652281,163096985,,90297115,1388962,26742842,4839396,48949000,19817000,20097057,66453255,70344357,6900142,22322699,85118700,,1030915,15673999,5318700,7254072,4917541,27302700,82737,3596688
2007,25877544,1026568,146592687,681471,374459,50698814,13728700,1063040,24111945,6925900,1179685631,232296830,71720859,28423538,7180100,128001000,5759424,2538591,5939634,4085426,493022,26730607,349000,2592776,26063619,2593750,159767672,,88965508,1178955,26083522,4588599,48598000,19668000,19425597,66353572,69515492,6010100,21701105,84218500,,1021235,15484192,5268400,7099021,4858235,26868000,81914,3494496
2006,25183615,940808,144839238,666920,368150,50355559,13525360,1048293,23969897,6857100,1162088305,229263980,70923164,27716983,7053700,127854000,5530218,2389498,5838837,4057041,479728,26263048,333000,2558484,25794344,2553376,156524189,,87592899,988448,25419994,4401365,48372000,19520000,18728200,66174486,68704721,5171255,21093973,83311200,,1008389,15308085,5218400,6949566,4801594,26488200,81110,3406334
2005,24399948,867014,142929979,651163,361889,49984704,13320058,1032562,23813324,6813200,1144326293,226254703,70122115,27017712,6930100,127773000,5332982,2263604,5745012,4052420,481390,25796124,321000,2526447,25506847,2506891,153356383,,86141373,836924,24745230,4265762,48138000,19373000,18132842,65863973,67860617,4481976,20504385,82392100,,989497,15147029,5162600,6453240,4747839,26167000,79994,3320396
2004,23499850,807989,140843786,634235,355700,49582751,13112334,1015827,23639296,6783500,1126419321,223268606,69321953,26320530,6809000,127761000,5171633,2166344,5658894,3863271,458579,25332026,312000,2496621,25198130,2448194,150267989,,84596249,732096,24055573,4166664,48039000,19228000,17671913,65404522,66973561,3975945,19931617,81436400,,962634,15012984,5104700,6666628,4696771,25864400,79636,3236626
2003,22507368,761595,138600174,616474,349557,49151958,12901217,998150,23449173,6730800,1108369577,220307809,68522074,25630426,6689700,127718000,5042538,2095993,5579003,3701464,450754,24869423,304000,2469045,24868900,2385075,147251530,,82971734,668165,23357887,4114826,47859000,19083000,17304339,64817254,66060121,3625798,19374012,80467400,,929431,14909019,5043300,6532871,4648036,25567700,78975,3154969
2002,21487079,725365,136228456,598421,343383,48689952,12681984,979883,23248053,6744100,1090189358,217369087,67696677,24943793,6570000,127445000,4938935,2042215,5502340,3522842,444178,24401977,297000,2443503,24516969,2323203,144271586,,81294378,634388,22668102,4175950,47622000,18939000,16997521,64136669,65125766,3394060,18832097,79537700,,894837,14858948,4990700,6406518,4600172,25271800,78318,3075373
2001,20531160,694893,133776064,580784,337074,48195684,12448881,961482,23043441,6714300,1071888190,214448301,66812736,24258794,6439000,127149000,4850227,1990022,5424701,3359875,438088,23920963,292000,2419729,24140941,2272547,141282077,,79604541,613720,22007937,4138012,47357000,18797000,16694414,63415174,64182694,3217865,18306287,78620500,,865848,14858335,4945100,6290413,4551737,24964400,77608,2997784
2000,19701940,666855,131280739,564187,330554,47669791,12197905,943294,22840218,6665000,1053481072,211540428,65850062,23574751,6289000,126843000,4767476,1929470,5342879,3742329,431867,23414909,286000,2397438,23740145,2239403,138250487,,77932247,593453,21392273,4027887,47008000,18655000,16354050,62693322,63240157,3050128,17795219,77630900,,847185,14883626,4898400,6172807,4501419,24650400,76803,2922153
1999,19038420,640904,128746273,548387,323812,47106923,11928306,925504,22641747,6606500,1034976626,208644079,64780362,22888600,6125000,126631000,4691402,1853152,5256207,3156661,424292,22898579,280000,2376165,23315053,2225481,135158132,,76285225,570643,20825955,3958723,46617000,18532000,15972430,61973957,62295617,2884188,17304422,76596700,,841063,14928426,4840400,6094126,4449427,24311700,75879,2848431
1998,18511480,618054,126169583,533506,316873,46509586,11641509,908059,22444986,6543700,1016402907,205753493,63616065,22200835,5971000,126400000,4621329,1766247,5165072,3113960,417435,22358128,271000,2355588,22866496,2224922,132013680,,74656228,548618,20302193,3927213,46287000,18425000,15568797,61250974,61344874,2733770,16829935,75456300,,845106,15071300,4769000,6012308,4395293,24051000,74853,2776568
1997,18034130,597834,123574107,520917,309746,45895991,11338733,890758,22240826,6489300,997817250,202853850,62426086,21519356,5836000,126057000,4545248,1686256,5068658,3092684,411050,21808125,264000,2335694,22395247,2227596,128845692,,73042605,528213,19809633,3796038,45954000,18323000,15151962,60544937,60394104,2595220,16349809,74306900,,853867,15333703,4696400,5936780,4335991,23667000,73817,2706518
1996,17481800,579855,120987124,512377,302449,45290888,11022162,873277,22016510,6435500,979290432,199926615,61306632,20855408,5692000,125757000,4448113,1637906,4966303,3070974,404808,21260881,262000,2316571,21902534,2219768,125697651,,71437381,511864,19331311,3670704,45525000,18210000,14736209,59878955,59451488,2467726,15834747,73156700,,859496,15577894,4628400,5862224,4267690,23225000,72899,2587997

import numpy as np

国家索引
country_index = np.array(
open('国际数据主要国家(地区)20年年度数据-utf8.csv').readline()[:-1].split(',')
)
读取人口数据
population_data = np.genfromtxt(
'国际数据主要国家(地区)20年年度数据-utf8.csv',
delimiter=',',
skip_header=1,
dtype=np.str
)

时间索引
time_index = population_data[:, 0]
数据行索引
time_index
array(['2015', '2014', '2013', '2012', '2011', '2010', '2009', '2008',
'2007', '2006', '2005', '2004', '2003', '2002', '2001', '2000',
'1999', '1998', '1997', '1996'],
dtype='<U10')
数据列索引
country_index
array(['时间', '阿富汗', '巴林', '孟加拉国', '不丹', '文莱', '缅甸', '柬埔寨', '塞浦路斯', '朝鲜',
'中国香港', '印度', '印度尼西亚', '伊朗', '伊拉克', '以色列', '日本', '约旦', '科威特', '老挝',
'黎巴嫩', '中国澳门', '马来西亚', '马尔代夫', '蒙古', '尼泊尔', '阿曼', '巴基斯坦', '巴勒斯坦',
'菲律宾', '卡塔尔', '沙特阿拉伯', '新加坡', '韩国', '斯里兰卡', '叙利亚', '泰国', '土耳其',
'阿联酋', '也门', '越南', '中国台湾', '东帝汶', '哈萨克斯坦', '吉尔吉斯斯坦', '塔吉克斯坦',
'土库曼斯坦', '乌兹别克斯坦', '马恩岛', '约旦河西岸和加沙'],
dtype='<U8')

  1. 计算2015年各个国家人口数据.
    year = '2015'

获取year年所有国家人口数据
population_by_year = population_data[time_index == year]
取前10个国家显示
population_by_year = population_by_year[0][:10]

给数据添加国家名称
print('%s年各个国家人口数据:' % year)
print('--------------------')

for country_name, country_data in zip(country_index, population_by_year):
print('"%s"人口为:\t%s' % (country_name, country_data))
2015年各个国家人口数据:


"时间"人口为: 2015
"阿富汗"人口为: 32526562
"巴林"人口为: 1377237
"孟加拉国"人口为: 160995642
"不丹"人口为: 774830
"文莱"人口为: 423188
"缅甸"人口为: 53897154
"柬埔寨"人口为: 15577899
"塞浦路斯"人口为: 1165300
"朝鲜"人口为: 25155317

  1. 计算朝鲜历史各个时期人口数据.
    country = '朝鲜'

方法一
先查找朝鲜在数组的索引
country_where = np.argwhere(country_index == country)[0][0]
按照索引计算人口数据
print('%s各个年份人口数据:' % country)
print('-----------------')
for data in zip(time_index, population_data[:, country_where][:5]):
print(data)

print('-'*20)

方法二
for data in zip(time_index, population_data.T[country_index == country][0][:5]):
print(data)
朝鲜各个年份人口数据:


('2015', '25155317')
('2014', '25026772')
('2013', '24895705')
('2012', '24763353')
('2011', '24631359')


('2015', '25155317')
('2014', '25026772')
('2013', '24895705')
('2012', '24763353')
('2011', '24631359')

  1. 计算缅甸2014年的人口数据.
    country = '缅甸'
    year = '2014'

方式一
country_data = population_data[time_index == year][0][np.argwhere(country_index == country)[0][0]]
方式二
country_data = population_data.T[country_index == country][0][np.argwhere(time_index == year)[0][0]]

print('%s在%s年人口数据为:%s.' % (country, year, country_data))
缅甸在2014年人口数据为:53437159.

  1. 计算每一个国家历史平均人口数据.
    去除第一行时间数据
    every_country_data = np.delete(population_data.T, 0, axis=0)
    处理数据中的空值为0
    every_country_data = np.where(every_country_data == '', 0, every_country_data).astype(np.int32)
    计算每一个国家的平均人口数据
    average_data = np.mean(every_country_data, axis=1).astype(np.int32)

print('各个国家近10年平均人口数据:')
print('----------------------')
for data in zip(country_index[1:], average_data):
输出国家对应的平均人口数据
print(data)
各个国家近10年平均人口数据:


array([ 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], dtype=int32)

  1. 计算亚洲2015年总人口,及平均人口.
    year = '2015'

2015年亚洲各个国家人口数量
every_country_data = population_data[time_index == year]
去除第一条时间数据
every_country_data = every_country_data.flatten()[1:]
处理数据中的缺失值, 并将数据类型转换为数字类型
every_country_data = np.where(every_country_data == '', 0, every_country_data).astype(np.int32)
计算平均值
average_data = np.mean(every_country_data)
计算总人口
total_data = np.sum(every_country_data)

print('亚洲%s年总人口数据:%d, 平均人口数据:%d.' % (year, total_data, average_data))
亚洲2015年总人口数据:2977813083, 平均人口数据:60771695.

  1. 计算印度、柬埔寨、阿富汗在2011、2012、2013年总人口及平均人口
    需要计算的国家和时间列表
    country_list = ['印度', '柬埔寨', '阿富汗']
    time_list = ['2011', '2012', '2013']

计算列表中元素在另外列表中的位置
def get_country_index(country_list, country_index):
indexes = []
for country in country_list:
indexes.append(np.argwhere(country_index == country)[0][0])

return indexes

计算国家索引
multi_country_index = get_country_index(country_list, country_index)
计算时间索引
multi_time_index = get_country_index(time_list, time_index)

获得在2011、2012、2013年所有国家数据
multi_country_data = population_data[multi_time_index]
获得country_list国家的人口数据
multi_country_data = multi_country_data[:, multi_country_index]
处理数据中的缺失数据
multi_country_data = np.where(multi_country_data == '', 0, multi_country_data).astype(np.int32)

def show_population_by_time(time_list, country_list):
year_list = np.array(time_list)
for year in time_list:
total = multi_country_data[year_list== year].sum()
average = multi_country_data[year_list == year].mean()
print('%s年%s总人口:%s, 平均人口: %s.' % (year, ','.join(country_list),total, average))

计算各个时间节点,多个国家总人口和平均人口
show_population_by_time(time_list, country_list)
2011年印度,柬埔寨,阿富汗总人口:1290848277, 平均人口: 430282759.0.
2012年印度,柬埔寨,阿富汗总人口:1308148697, 平均人口: 436049565.667.
2013年印度,柬埔寨,阿富汗总人口:1325259938, 平均人口: 441753312.667.
2014年印度,柬埔寨,阿富汗总人口:1342247185, 平均人口: 447415728.333.

  1. 计算任意两个国家之间的人口差数据.
    country1 = '印度'
    country2 = '乌兹别克斯坦'
    year = '2015'

def get_population_by_time_and_country(country, year):
获得指定国家在指定年限的数据
data = population_data[time_index == year].T[country_index == country]
处理数据缺失值
data = np.where(data == '', 0, data).astype(np.int32)[0][0]

return data

country1_data = get_population_by_time_and_country(country1, year)
country2_data = get_population_by_time_and_country(country2, year)

print('"%s"和"%s"两个国家在%s年的人口数量差为:%d.'
%(country1, country2, year, np.abs(country1_data - country2_data)))
"印度"和"乌兹别克斯坦"两个国家在2015年的人口数量差为:1279751627.

  1. 计算2015年亚洲人口数量排名前10的国家.
    year = '2015'

获得指定年份各个国家人口数据
all_country_data = population_data[time_index == year][0][1:]
处理缺失值
all_country_data = np.where(all_country_data == '', 0, all_country_data).astype(np.int32)
计算排序索引
sort_index = all_country_data.argsort()
计算人口数量排序
sort_data = all_country_data[sort_index]
计算人口对应国家名称
sort_country = country_index[1:][sort_index]
显示排序之后国家对应人口
all_sorted_data = []
for data in zip(sort_country, sort_data):
all_sorted_data.append(data)

打印前10名国家信息
all_sorted_data[-10:][::-1]
[('印度', 1311050527),
('印度尼西亚', 257563815),
('巴基斯坦', 188924874),
('孟加拉国', 160995642),
('日本', 126958472),
('菲律宾', 100699395),
('越南', 91713300),
('伊朗', 79109272),
('土耳其', 78665830),
('泰国', 67959359)]

猜你喜欢

转载自blog.csdn.net/weixin_33807284/article/details/87049524
今日推荐