我正在尝试使用来自两个单独的数据透视表数据帧的值来计算每100,000人口的犯罪率。
Crime Total
Year 2010 2011 2012 2013 2014 2015 2016
CountyName
Alameda 12115 11971 13412 12327 10944 10463 11410
Alpine 355 481 200 174 125 202 165
Amador 2572 3033 2525 2390 2087 1856 2064
Butte 5966 5988 5459 5533 5085 6542 6324
Calaveras 3236 4606 4586 4162 4055 3276 3012
Colusa 1219 1325 1322 1406 817 763 850
Contra Costa 966 1032 1100 857 759 775 7553
Del Norte 1923 2388 2229 1791 2140 2190 2048
El Dorado 10259 11126 10305 10173 9938 9956 10394
Fresno 688 654 698 803 470 693 20858
Glenn 841 769 1130 1862 1203 876 775
Humboldt 7087 5684 5428 5649 4582 4278 4308
Imperial 3805 3405 4011 3930 3453 3658 2706
Inyo 833 858 1061 999 997 697 1061
Kern 52636 52164 59685 55531 47718 51277 54088
Kings 3161 3573 3056 3309 2396 2450 2413
Lake 5117 4123 4646 4655 4086 4265 3296
Lassen 767 837 1021 969 1019 1130 1104
Los Angeles 4 11 0 5 24 4 96929
Madera 6620 8080 8179 6855 6243 6395 6058
Marin 4112 3768 3630 3531 3092 3550 3412
Mariposa 1502 1796 1749 1651 1495 1572 1284
Mendocino 3714 3589 3633 4076 3634 3245 3523
Merced 11111 12008 10650 9678 8123 8189 8423
Modoc 313 506 506 584 402 394 270
Mono 392 353 314 362 362 252 309
Monterey 6734 6635 6645 6973 6964 6366 7023
Napa 2021 1898 1779 1896 570 438 1478
Nevada 3031 2984 3632 3801 3096 3629 3261
Orange 6309 6645 7035 6328 6178 6410 5618
Placer 10059 10132 8756 9256 8560 8912 8151
Plumas 1990 2011 2198 1985 1604 1492 1928
Riverside 46050 39713 36571 34312 30229 34105 33843
Sacramento 12848 12649 14790 13039 13045 14709 53793
San Benito 1620 1273 1320 1048 1153 888 679
San Bernardino 28350 31145 31297 29480 25899 27084 24922
San Diego 31015 30472 34700 30709 26191 25877 25908
San Francisco 158588 158741 182747 228175 213410 242800 219601
San Joaquin 22005 23620 22615 20859 19080 18623 16330
San Luis Obispo 397 324 603 1200 1283 1479 7646
San Mateo 8060 9019 11958 13008 11485 13037 13387
Santa Barbara 8106 8173 9627 9264 8338 8685 8338
Santa Clara 8588 7139 8256 7130 6463 6646 6844
Santa Cruz 10786 11383 9439 9563 8847 9566 9902
Shasta 7440 6922 7022 6823 6748 7777 6964
Sierra 333 270 274 358 167 145 170
Siskiyou 1250 1390 1068 1034 1027 953 1296
Solano 751 2242 2341 2263 2127 2210 1719
Sonoma 9203 8420 9413 9080 9203 9869 9265
Stanislaus 13995 11303 11431 11763 10183 10646 9535
Sutter 3352 3989 3548 4067 3020 2732 2927
Tehama 2094 2268 2276 2059 1858 2072 2552
Trinity 663 668 861 705 1095 1316 1762
Tulare 15447 16816 15865 14896 10830 10775 10835
Tuolumne 3734 3706 3974 3938 3491 4162 3989
Ventura 6411 5406 5766 4885 4230 4351 3970
Yolo 1801 2079 2257 1930 1516 1656 1364
Yuba 5263 6836 6819 6736 6812 6210 5442
Population Total
Year 2010 2011 2012 2013 2014 2015 2016
CountyName
Alameda 1510271.0 1525695.0 1543027.0 1567091.0 1588348.0 1611318.0 1629233.0
Alpine 1175.0 1169.0 1166.0 1164.0 1163.0 1163.0 1160.0
Amador 38091.0 38011.0 37945.0 37304.0 37815.0 37764.0 37667.0
Butte 220000.0 220828.0 221064.0 222341.0 223301.0 224467.0 224703.0
Calaveras 45578.0 45557.0 45495.0 45424.0 45358.0 45266.0 45246.0
California 37253956.0 37536835.0 37881357.0 38238492.0 38572211.0 38915880.0 39189035.0
Colusa 21419.0 21521.0 21597.0 21698.0 21794.0 21873.0 21965.0
Contra Costa 1049025.0 1059495.0 1069977.0 1083721.0 1097644.0 1111899.0 1126824.0
Del Norte 28610.0 28235.0 28066.0 27800.0 27357.0 26992.0 27006.0
El Dorado 181058.0 181170.0 180952.0 180720.0 181977.0 183100.0 184371.0
Fresno 930450.0 938524.0 947615.0 956098.0 964757.0 975324.0 985079.0
Glenn 28122.0 28238.0 28317.0 28424.0 28497.0 28564.0 28639.0
Humboldt 134623.0 135711.0 135289.0 135081.0 135059.0 135247.0 135557.0
Imperial 174528.0 176370.0 178973.0 180224.0 181989.0 184624.0 186080.0
Inyo 18546.0 18536.0 18559.0 18557.0 18613.0 18563.0 18632.0
Kern 839631.0 845978.0 854122.0 864061.0 872123.0 880664.0 886803.0
Kings 152982.0 151322.0 151330.0 150440.0 149732.0 149704.0 149822.0
Lake 64665.0 64491.0 64347.0 64749.0 64835.0 65170.0 64790.0
Lassen 34895.0 34485.0 33735.0 32812.0 31993.0 31613.0 30841.0
Los Angeles 9818605.0 9874887.0 9956722.0 10021318.0 10089847.0 10150617.0 10182961.0
Madera 150865.0 151256.0 151423.0 151377.0 153258.0 154857.0 154933.0
Marin 252409.0 253964.0 255812.0 257420.0 260554.0 262305.0 263150.0
Mariposa 18251.0 18251.0 18238.0 18195.0 18218.0 18172.0 18167.0
Mendocino 87841.0 87436.0 87634.0 88227.0 88232.0 88278.0 88771.0
Merced 255793.0 258852.0 262147.0 264437.0 266556.0 269132.0 271547.0
Modoc 9686.0 9696.0 9670.0 9646.0 9636.0 9617.0 9620.0
Mono 14202.0 14219.0 14191.0 14059.0 13942.0 13851.0 13654.0
Monterey 415057.0 417894.0 423166.0 427087.0 429298.0 432664.0 438171.0
Napa 136484.0 136913.0 138074.0 138867.0 140263.0 140992.0 141888.0
Nevada 98764.0 98409.0 98069.0 97848.0 97927.0 98315.0 98609.0
Orange 3010232.0 3035167.0 3069454.0 3102606.0 3127083.0 3152376.0 3172152.0
Placer 348432.0 353228.0 358152.0 362551.0 367442.0 370710.0 376203.0
Plumas 20007.0 19983.0 19967.0 19920.0 19893.0 19860.0 19837.0
Riverside 2189641.0 2212874.0 2239715.0 2266290.0 2291699.0 2318762.0 2348213.0
Sacramento 1418788.0 1429653.0 1440456.0 1452994.0 1466309.0 1482542.0 1496619.0
San Benito 55269.0 55524.0 55781.0 55998.0 56219.0 56451.0 56621.0
San Bernardino 2035210.0 2054735.0 2070374.0 2086576.0 2101525.0 2122015.0 2135724.0
San Diego 3095313.0 3120688.0 3153951.0 3195215.0 3231651.0 3266192.0 3286717.0
San Francisco 805235.0 815854.0 826103.0 839242.0 848022.0 857410.0 864889.0
San Joaquin 685306.0 691818.0 698412.0 704727.0 712046.0 723985.0 735677.0
San Luis Obispo 269637.0 270057.0 271348.0 273417.0 275762.0 276862.0 278480.0
San Mateo 718451.0 726305.0 735256.0 745639.0 752355.0 759609.0 765895.0
Santa Barbara 423895.0 424610.0 428584.0 433117.0 438578.0 443143.0 447295.0
Santa Clara 1781642.0 1803362.0 1828496.0 1856416.0 1879196.0 1903209.0 1922619.0
Santa Cruz 262382.0 264761.0 266918.0 269120.0 271061.0 273856.0 275557.0
Shasta 177223.0 177694.0 177693.0 178661.0 179067.0 179312.0 178232.0
Sierra 3240.0 3234.0 3233.0 3215.0 3204.0 3195.0 3194.0
Siskiyou 44900.0 44854.0 44841.0 44825.0 44809.0 44730.0 44722.0
Solano 413344.0 413438.0 415862.0 419185.0 423283.0 427122.0 430972.0
Sonoma 483878.0 485417.0 487487.0 491993.0 495798.0 500051.0 502604.0
Stanislaus 514453.0 517748.0 521620.0 525845.0 529994.0 535125.0 541466.0
Sutter 94737.0 95591.0 95948.0 95722.0 96036.0 96390.0 96614.0
Tehama 63463.0 63531.0 63584.0 63641.0 63743.0 63837.0 63942.0
Trinity 13786.0 13758.0 13742.0 13731.0 13722.0 13676.0 13647.0
Tulare 442179.0 445662.0 450672.0 455091.0 458840.0 462061.0 466563.0
Tuolumne 55365.0 55309.0 55249.0 54938.0 55082.0 54663.0 54949.0
Ventura 823318.0 829511.0 834253.0 840955.0 847103.0 851451.0 853893.0
Yolo 200849.0 202836.0 204578.0 207329.0 208957.0 211126.0 215522.0
Yuba 72155.0 72520.0 72906.0 73343.0 73646.0 74044.0 74328.0`
我想使用以下公式组合每列中的值:
犯罪率=(犯罪总人数/总人口数)x 100,000
然后将犯罪率放在新的数据透视表数据框中,索引= CountyName,columns = Year,以及值=每100,000人口的犯罪率。
犯罪和人口数据表索引:
Index(['Alameda', 'Alpine', 'Amador', 'Butte', 'Calaveras', 'Colusa', 'Contra Costa', 'Del Norte', 'El Dorado', 'Fresno', 'Glenn', 'Humboldt', 'Imperial', 'Inyo', 'Kern', 'Kings', 'Lake', 'Lassen', 'Los Angeles', 'Madera', 'Marin', 'Mariposa', 'Mendocino', 'Merced', 'Modoc', 'Mono', 'Monterey', 'Napa', 'Nevada', 'Orange', 'Placer', 'Plumas', 'Riverside', 'Sacramento', 'San Benito', 'San Bernardino', 'San Diego', 'San Francisco', 'San Joaquin', 'San Luis Obispo', 'San Mateo', 'Santa Barbara', 'Santa Clara', 'Santa Cruz', 'Shasta', 'Sierra', 'Siskiyou', 'Solano', 'Sonoma', 'Stanislaus', 'Sutter', 'Tehama', 'Trinity', 'Tulare', 'Tuolumne', 'Ventura', 'Yolo', 'Yuba'], dtype='object', name='CountyName')
答案 0 :(得分:0)
df_pop
Out[97]:
Year 2010 2011 2012 2013 2014 2015 \
CountyName
Alameda 1510271.0 1525695.0 1543027.0 1567091.0 1588348.0 1611318.0
Alpine 1175.0 1169.0 1166.0 1164.0 1163.0 1163.0
Amador 38091.0 38011.0 37945.0 37304.0 37815.0 37764.0
Year 2016
CountyName
Alameda 1629233.0
Alpine 1160.0
Amador 37667.0
df_crime
Out[98]:
Year 2010 2011 2012 2013 2014 2015 2016
CountyName
Alameda 12115.0 11971.0 13412.0 12327.0 10944.0 10463.0 11410.0
Alpine 355.0 481.0 200.0 174.0 125.0 202.0 165.0
Amador 2572.0 3033.0 2525.0 2390.0 2087.0 1856.0 2064.0
df_rel_crime = np.round(df_crime.mul(100000).div(df_pop), 1)
df_rel_crime
Out[105]:
Year 2010 2011 2012 2013 2014 2015 2016
CountyName
Alameda 802.2 784.6 869.2 786.6 689.0 649.3 700.3
Alpine 30212.8 41146.3 17152.7 14948.5 10748.1 17368.9 14224.1
Amador 6752.3 7979.3 6654.4 6406.8 5519.0 4914.7 5479.6
由于您仍有问题,这里有一个极简主义示例,证明代码有效。复制并运行它以便自己查看:
import pandas as pd
import numpy as np
df_pop = pd.DataFrame({'2010': 1510271.0}, index=['Alameda'])
df_pop.index.name = 'County_Name'
df_pop.columns.name = 'Year'
df_crime = pd.DataFrame({'2010': 12115}, index=['Alameda'])
df_crime.index.name = 'County_Name'
df_crime.columns.name = 'Year'
crime_rate = np.round(df_crime.mul(100000).div(df_pop), 1)
crime_rate
Out[128]:
Year 2010
County_Name
Alameda 802.2