{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"import pandas as pd\n",
"\n",
"from pandas import Series,DataFrame"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# csv类型文件呢,文本文件,excel打开,格式化的文件,所以excel可以直接读取成表格\n",
"# 美国人口的一些情况\n",
"# pandas分析一下美国人口数据"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" state | \n",
" area (sq. mi) | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Alabama | \n",
" 52423 | \n",
"
\n",
" \n",
" 1 | \n",
" Alaska | \n",
" 656425 | \n",
"
\n",
" \n",
" 2 | \n",
" Arizona | \n",
" 114006 | \n",
"
\n",
" \n",
" 3 | \n",
" Arkansas | \n",
" 53182 | \n",
"
\n",
" \n",
" 4 | \n",
" California | \n",
" 163707 | \n",
"
\n",
" \n",
" 5 | \n",
" Colorado | \n",
" 104100 | \n",
"
\n",
" \n",
" 6 | \n",
" Connecticut | \n",
" 5544 | \n",
"
\n",
" \n",
" 7 | \n",
" Delaware | \n",
" 1954 | \n",
"
\n",
" \n",
" 8 | \n",
" Florida | \n",
" 65758 | \n",
"
\n",
" \n",
" 9 | \n",
" Georgia | \n",
" 59441 | \n",
"
\n",
" \n",
" 10 | \n",
" Hawaii | \n",
" 10932 | \n",
"
\n",
" \n",
" 11 | \n",
" Idaho | \n",
" 83574 | \n",
"
\n",
" \n",
" 12 | \n",
" Illinois | \n",
" 57918 | \n",
"
\n",
" \n",
" 13 | \n",
" Indiana | \n",
" 36420 | \n",
"
\n",
" \n",
" 14 | \n",
" Iowa | \n",
" 56276 | \n",
"
\n",
" \n",
" 15 | \n",
" Kansas | \n",
" 82282 | \n",
"
\n",
" \n",
" 16 | \n",
" Kentucky | \n",
" 40411 | \n",
"
\n",
" \n",
" 17 | \n",
" Louisiana | \n",
" 51843 | \n",
"
\n",
" \n",
" 18 | \n",
" Maine | \n",
" 35387 | \n",
"
\n",
" \n",
" 19 | \n",
" Maryland | \n",
" 12407 | \n",
"
\n",
" \n",
" 20 | \n",
" Massachusetts | \n",
" 10555 | \n",
"
\n",
" \n",
" 21 | \n",
" Michigan | \n",
" 96810 | \n",
"
\n",
" \n",
" 22 | \n",
" Minnesota | \n",
" 86943 | \n",
"
\n",
" \n",
" 23 | \n",
" Mississippi | \n",
" 48434 | \n",
"
\n",
" \n",
" 24 | \n",
" Missouri | \n",
" 69709 | \n",
"
\n",
" \n",
" 25 | \n",
" Montana | \n",
" 147046 | \n",
"
\n",
" \n",
" 26 | \n",
" Nebraska | \n",
" 77358 | \n",
"
\n",
" \n",
" 27 | \n",
" Nevada | \n",
" 110567 | \n",
"
\n",
" \n",
" 28 | \n",
" New Hampshire | \n",
" 9351 | \n",
"
\n",
" \n",
" 29 | \n",
" New Jersey | \n",
" 8722 | \n",
"
\n",
" \n",
" 30 | \n",
" New Mexico | \n",
" 121593 | \n",
"
\n",
" \n",
" 31 | \n",
" New York | \n",
" 54475 | \n",
"
\n",
" \n",
" 32 | \n",
" North Carolina | \n",
" 53821 | \n",
"
\n",
" \n",
" 33 | \n",
" North Dakota | \n",
" 70704 | \n",
"
\n",
" \n",
" 34 | \n",
" Ohio | \n",
" 44828 | \n",
"
\n",
" \n",
" 35 | \n",
" Oklahoma | \n",
" 69903 | \n",
"
\n",
" \n",
" 36 | \n",
" Oregon | \n",
" 98386 | \n",
"
\n",
" \n",
" 37 | \n",
" Pennsylvania | \n",
" 46058 | \n",
"
\n",
" \n",
" 38 | \n",
" Rhode Island | \n",
" 1545 | \n",
"
\n",
" \n",
" 39 | \n",
" South Carolina | \n",
" 32007 | \n",
"
\n",
" \n",
" 40 | \n",
" South Dakota | \n",
" 77121 | \n",
"
\n",
" \n",
" 41 | \n",
" Tennessee | \n",
" 42146 | \n",
"
\n",
" \n",
" 42 | \n",
" Texas | \n",
" 268601 | \n",
"
\n",
" \n",
" 43 | \n",
" Utah | \n",
" 84904 | \n",
"
\n",
" \n",
" 44 | \n",
" Vermont | \n",
" 9615 | \n",
"
\n",
" \n",
" 45 | \n",
" Virginia | \n",
" 42769 | \n",
"
\n",
" \n",
" 46 | \n",
" Washington | \n",
" 71303 | \n",
"
\n",
" \n",
" 47 | \n",
" West Virginia | \n",
" 24231 | \n",
"
\n",
" \n",
" 48 | \n",
" Wisconsin | \n",
" 65503 | \n",
"
\n",
" \n",
" 49 | \n",
" Wyoming | \n",
" 97818 | \n",
"
\n",
" \n",
" 50 | \n",
" District of Columbia | \n",
" 68 | \n",
"
\n",
" \n",
" 51 | \n",
" Puerto Rico | \n",
" 3515 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" state area (sq. mi)\n",
"0 Alabama 52423\n",
"1 Alaska 656425\n",
"2 Arizona 114006\n",
"3 Arkansas 53182\n",
"4 California 163707\n",
"5 Colorado 104100\n",
"6 Connecticut 5544\n",
"7 Delaware 1954\n",
"8 Florida 65758\n",
"9 Georgia 59441\n",
"10 Hawaii 10932\n",
"11 Idaho 83574\n",
"12 Illinois 57918\n",
"13 Indiana 36420\n",
"14 Iowa 56276\n",
"15 Kansas 82282\n",
"16 Kentucky 40411\n",
"17 Louisiana 51843\n",
"18 Maine 35387\n",
"19 Maryland 12407\n",
"20 Massachusetts 10555\n",
"21 Michigan 96810\n",
"22 Minnesota 86943\n",
"23 Mississippi 48434\n",
"24 Missouri 69709\n",
"25 Montana 147046\n",
"26 Nebraska 77358\n",
"27 Nevada 110567\n",
"28 New Hampshire 9351\n",
"29 New Jersey 8722\n",
"30 New Mexico 121593\n",
"31 New York 54475\n",
"32 North Carolina 53821\n",
"33 North Dakota 70704\n",
"34 Ohio 44828\n",
"35 Oklahoma 69903\n",
"36 Oregon 98386\n",
"37 Pennsylvania 46058\n",
"38 Rhode Island 1545\n",
"39 South Carolina 32007\n",
"40 South Dakota 77121\n",
"41 Tennessee 42146\n",
"42 Texas 268601\n",
"43 Utah 84904\n",
"44 Vermont 9615\n",
"45 Virginia 42769\n",
"46 Washington 71303\n",
"47 West Virginia 24231\n",
"48 Wisconsin 65503\n",
"49 Wyoming 97818\n",
"50 District of Columbia 68\n",
"51 Puerto Rico 3515"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 美国各州的面积\n",
"areas = pd.read_csv('./state-areas.csv')\n",
"areas"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(52, 2)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"areas.shape"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" state | \n",
" abbreviation | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Alabama | \n",
" AL | \n",
"
\n",
" \n",
" 1 | \n",
" Alaska | \n",
" AK | \n",
"
\n",
" \n",
" 2 | \n",
" Arizona | \n",
" AZ | \n",
"
\n",
" \n",
" 3 | \n",
" Arkansas | \n",
" AR | \n",
"
\n",
" \n",
" 4 | \n",
" California | \n",
" CA | \n",
"
\n",
" \n",
" 5 | \n",
" Colorado | \n",
" CO | \n",
"
\n",
" \n",
" 6 | \n",
" Connecticut | \n",
" CT | \n",
"
\n",
" \n",
" 7 | \n",
" Delaware | \n",
" DE | \n",
"
\n",
" \n",
" 8 | \n",
" District of Columbia | \n",
" DC | \n",
"
\n",
" \n",
" 9 | \n",
" Florida | \n",
" FL | \n",
"
\n",
" \n",
" 10 | \n",
" Georgia | \n",
" GA | \n",
"
\n",
" \n",
" 11 | \n",
" Hawaii | \n",
" HI | \n",
"
\n",
" \n",
" 12 | \n",
" Idaho | \n",
" ID | \n",
"
\n",
" \n",
" 13 | \n",
" Illinois | \n",
" IL | \n",
"
\n",
" \n",
" 14 | \n",
" Indiana | \n",
" IN | \n",
"
\n",
" \n",
" 15 | \n",
" Iowa | \n",
" IA | \n",
"
\n",
" \n",
" 16 | \n",
" Kansas | \n",
" KS | \n",
"
\n",
" \n",
" 17 | \n",
" Kentucky | \n",
" KY | \n",
"
\n",
" \n",
" 18 | \n",
" Louisiana | \n",
" LA | \n",
"
\n",
" \n",
" 19 | \n",
" Maine | \n",
" ME | \n",
"
\n",
" \n",
" 20 | \n",
" Montana | \n",
" MT | \n",
"
\n",
" \n",
" 21 | \n",
" Nebraska | \n",
" NE | \n",
"
\n",
" \n",
" 22 | \n",
" Nevada | \n",
" NV | \n",
"
\n",
" \n",
" 23 | \n",
" New Hampshire | \n",
" NH | \n",
"
\n",
" \n",
" 24 | \n",
" New Jersey | \n",
" NJ | \n",
"
\n",
" \n",
" 25 | \n",
" New Mexico | \n",
" NM | \n",
"
\n",
" \n",
" 26 | \n",
" New York | \n",
" NY | \n",
"
\n",
" \n",
" 27 | \n",
" North Carolina | \n",
" NC | \n",
"
\n",
" \n",
" 28 | \n",
" North Dakota | \n",
" ND | \n",
"
\n",
" \n",
" 29 | \n",
" Ohio | \n",
" OH | \n",
"
\n",
" \n",
" 30 | \n",
" Oklahoma | \n",
" OK | \n",
"
\n",
" \n",
" 31 | \n",
" Oregon | \n",
" OR | \n",
"
\n",
" \n",
" 32 | \n",
" Maryland | \n",
" MD | \n",
"
\n",
" \n",
" 33 | \n",
" Massachusetts | \n",
" MA | \n",
"
\n",
" \n",
" 34 | \n",
" Michigan | \n",
" MI | \n",
"
\n",
" \n",
" 35 | \n",
" Minnesota | \n",
" MN | \n",
"
\n",
" \n",
" 36 | \n",
" Mississippi | \n",
" MS | \n",
"
\n",
" \n",
" 37 | \n",
" Missouri | \n",
" MO | \n",
"
\n",
" \n",
" 38 | \n",
" Pennsylvania | \n",
" PA | \n",
"
\n",
" \n",
" 39 | \n",
" Rhode Island | \n",
" RI | \n",
"
\n",
" \n",
" 40 | \n",
" South Carolina | \n",
" SC | \n",
"
\n",
" \n",
" 41 | \n",
" South Dakota | \n",
" SD | \n",
"
\n",
" \n",
" 42 | \n",
" Tennessee | \n",
" TN | \n",
"
\n",
" \n",
" 43 | \n",
" Texas | \n",
" TX | \n",
"
\n",
" \n",
" 44 | \n",
" Utah | \n",
" UT | \n",
"
\n",
" \n",
" 45 | \n",
" Vermont | \n",
" VT | \n",
"
\n",
" \n",
" 46 | \n",
" Virginia | \n",
" VA | \n",
"
\n",
" \n",
" 47 | \n",
" Washington | \n",
" WA | \n",
"
\n",
" \n",
" 48 | \n",
" West Virginia | \n",
" WV | \n",
"
\n",
" \n",
" 49 | \n",
" Wisconsin | \n",
" WI | \n",
"
\n",
" \n",
" 50 | \n",
" Wyoming | \n",
" WY | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" state abbreviation\n",
"0 Alabama AL\n",
"1 Alaska AK\n",
"2 Arizona AZ\n",
"3 Arkansas AR\n",
"4 California CA\n",
"5 Colorado CO\n",
"6 Connecticut CT\n",
"7 Delaware DE\n",
"8 District of Columbia DC\n",
"9 Florida FL\n",
"10 Georgia GA\n",
"11 Hawaii HI\n",
"12 Idaho ID\n",
"13 Illinois IL\n",
"14 Indiana IN\n",
"15 Iowa IA\n",
"16 Kansas KS\n",
"17 Kentucky KY\n",
"18 Louisiana LA\n",
"19 Maine ME\n",
"20 Montana MT\n",
"21 Nebraska NE\n",
"22 Nevada NV\n",
"23 New Hampshire NH\n",
"24 New Jersey NJ\n",
"25 New Mexico NM\n",
"26 New York NY\n",
"27 North Carolina NC\n",
"28 North Dakota ND\n",
"29 Ohio OH\n",
"30 Oklahoma OK\n",
"31 Oregon OR\n",
"32 Maryland MD\n",
"33 Massachusetts MA\n",
"34 Michigan MI\n",
"35 Minnesota MN\n",
"36 Mississippi MS\n",
"37 Missouri MO\n",
"38 Pennsylvania PA\n",
"39 Rhode Island RI\n",
"40 South Carolina SC\n",
"41 South Dakota SD\n",
"42 Tennessee TN\n",
"43 Texas TX\n",
"44 Utah UT\n",
"45 Vermont VT\n",
"46 Virginia VA\n",
"47 Washington WA\n",
"48 West Virginia WV\n",
"49 Wisconsin WI\n",
"50 Wyoming WY"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 美国各州 缩写\n",
"abbrevs = pd.read_csv('./state-abbrevs.csv')\n",
"abbrevs"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(51, 2)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"abbrevs.shape"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" state/region | \n",
" ages | \n",
" year | \n",
" population | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" AL | \n",
" under18 | \n",
" 2012 | \n",
" 1117489.0 | \n",
"
\n",
" \n",
" 1 | \n",
" AL | \n",
" total | \n",
" 2012 | \n",
" 4817528.0 | \n",
"
\n",
" \n",
" 2 | \n",
" AL | \n",
" under18 | \n",
" 2010 | \n",
" 1130966.0 | \n",
"
\n",
" \n",
" 3 | \n",
" AL | \n",
" total | \n",
" 2010 | \n",
" 4785570.0 | \n",
"
\n",
" \n",
" 4 | \n",
" AL | \n",
" under18 | \n",
" 2011 | \n",
" 1125763.0 | \n",
"
\n",
" \n",
" 5 | \n",
" AL | \n",
" total | \n",
" 2011 | \n",
" 4801627.0 | \n",
"
\n",
" \n",
" 6 | \n",
" AL | \n",
" total | \n",
" 2009 | \n",
" 4757938.0 | \n",
"
\n",
" \n",
" 7 | \n",
" AL | \n",
" under18 | \n",
" 2009 | \n",
" 1134192.0 | \n",
"
\n",
" \n",
" 8 | \n",
" AL | \n",
" under18 | \n",
" 2013 | \n",
" 1111481.0 | \n",
"
\n",
" \n",
" 9 | \n",
" AL | \n",
" total | \n",
" 2013 | \n",
" 4833722.0 | \n",
"
\n",
" \n",
" 10 | \n",
" AL | \n",
" total | \n",
" 2007 | \n",
" 4672840.0 | \n",
"
\n",
" \n",
" 11 | \n",
" AL | \n",
" under18 | \n",
" 2007 | \n",
" 1132296.0 | \n",
"
\n",
" \n",
" 12 | \n",
" AL | \n",
" total | \n",
" 2008 | \n",
" 4718206.0 | \n",
"
\n",
" \n",
" 13 | \n",
" AL | \n",
" under18 | \n",
" 2008 | \n",
" 1134927.0 | \n",
"
\n",
" \n",
" 14 | \n",
" AL | \n",
" total | \n",
" 2005 | \n",
" 4569805.0 | \n",
"
\n",
" \n",
" 15 | \n",
" AL | \n",
" under18 | \n",
" 2005 | \n",
" 1117229.0 | \n",
"
\n",
" \n",
" 16 | \n",
" AL | \n",
" total | \n",
" 2006 | \n",
" 4628981.0 | \n",
"
\n",
" \n",
" 17 | \n",
" AL | \n",
" under18 | \n",
" 2006 | \n",
" 1126798.0 | \n",
"
\n",
" \n",
" 18 | \n",
" AL | \n",
" total | \n",
" 2004 | \n",
" 4530729.0 | \n",
"
\n",
" \n",
" 19 | \n",
" AL | \n",
" under18 | \n",
" 2004 | \n",
" 1113662.0 | \n",
"
\n",
" \n",
" 20 | \n",
" AL | \n",
" total | \n",
" 2003 | \n",
" 4503491.0 | \n",
"
\n",
" \n",
" 21 | \n",
" AL | \n",
" under18 | \n",
" 2003 | \n",
" 1113083.0 | \n",
"
\n",
" \n",
" 22 | \n",
" AL | \n",
" total | \n",
" 2001 | \n",
" 4467634.0 | \n",
"
\n",
" \n",
" 23 | \n",
" AL | \n",
" under18 | \n",
" 2001 | \n",
" 1120409.0 | \n",
"
\n",
" \n",
" 24 | \n",
" AL | \n",
" total | \n",
" 2002 | \n",
" 4480089.0 | \n",
"
\n",
" \n",
" 25 | \n",
" AL | \n",
" under18 | \n",
" 2002 | \n",
" 1116590.0 | \n",
"
\n",
" \n",
" 26 | \n",
" AL | \n",
" under18 | \n",
" 1999 | \n",
" 1121287.0 | \n",
"
\n",
" \n",
" 27 | \n",
" AL | \n",
" total | \n",
" 1999 | \n",
" 4430141.0 | \n",
"
\n",
" \n",
" 28 | \n",
" AL | \n",
" total | \n",
" 2000 | \n",
" 4452173.0 | \n",
"
\n",
" \n",
" 29 | \n",
" AL | \n",
" under18 | \n",
" 2000 | \n",
" 1122273.0 | \n",
"
\n",
" \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
"
\n",
" \n",
" 2514 | \n",
" USA | \n",
" under18 | \n",
" 1999 | \n",
" 71946051.0 | \n",
"
\n",
" \n",
" 2515 | \n",
" USA | \n",
" total | \n",
" 2000 | \n",
" 282162411.0 | \n",
"
\n",
" \n",
" 2516 | \n",
" USA | \n",
" under18 | \n",
" 2000 | \n",
" 72376189.0 | \n",
"
\n",
" \n",
" 2517 | \n",
" USA | \n",
" total | \n",
" 1999 | \n",
" 279040181.0 | \n",
"
\n",
" \n",
" 2518 | \n",
" USA | \n",
" total | \n",
" 2001 | \n",
" 284968955.0 | \n",
"
\n",
" \n",
" 2519 | \n",
" USA | \n",
" under18 | \n",
" 2001 | \n",
" 72671175.0 | \n",
"
\n",
" \n",
" 2520 | \n",
" USA | \n",
" total | \n",
" 2002 | \n",
" 287625193.0 | \n",
"
\n",
" \n",
" 2521 | \n",
" USA | \n",
" under18 | \n",
" 2002 | \n",
" 72936457.0 | \n",
"
\n",
" \n",
" 2522 | \n",
" USA | \n",
" total | \n",
" 2003 | \n",
" 290107933.0 | \n",
"
\n",
" \n",
" 2523 | \n",
" USA | \n",
" under18 | \n",
" 2003 | \n",
" 73100758.0 | \n",
"
\n",
" \n",
" 2524 | \n",
" USA | \n",
" total | \n",
" 2004 | \n",
" 292805298.0 | \n",
"
\n",
" \n",
" 2525 | \n",
" USA | \n",
" under18 | \n",
" 2004 | \n",
" 73297735.0 | \n",
"
\n",
" \n",
" 2526 | \n",
" USA | \n",
" total | \n",
" 2005 | \n",
" 295516599.0 | \n",
"
\n",
" \n",
" 2527 | \n",
" USA | \n",
" under18 | \n",
" 2005 | \n",
" 73523669.0 | \n",
"
\n",
" \n",
" 2528 | \n",
" USA | \n",
" total | \n",
" 2006 | \n",
" 298379912.0 | \n",
"
\n",
" \n",
" 2529 | \n",
" USA | \n",
" under18 | \n",
" 2006 | \n",
" 73757714.0 | \n",
"
\n",
" \n",
" 2530 | \n",
" USA | \n",
" total | \n",
" 2007 | \n",
" 301231207.0 | \n",
"
\n",
" \n",
" 2531 | \n",
" USA | \n",
" under18 | \n",
" 2007 | \n",
" 74019405.0 | \n",
"
\n",
" \n",
" 2532 | \n",
" USA | \n",
" total | \n",
" 2008 | \n",
" 304093966.0 | \n",
"
\n",
" \n",
" 2533 | \n",
" USA | \n",
" under18 | \n",
" 2008 | \n",
" 74104602.0 | \n",
"
\n",
" \n",
" 2534 | \n",
" USA | \n",
" under18 | \n",
" 2013 | \n",
" 73585872.0 | \n",
"
\n",
" \n",
" 2535 | \n",
" USA | \n",
" total | \n",
" 2013 | \n",
" 316128839.0 | \n",
"
\n",
" \n",
" 2536 | \n",
" USA | \n",
" total | \n",
" 2009 | \n",
" 306771529.0 | \n",
"
\n",
" \n",
" 2537 | \n",
" USA | \n",
" under18 | \n",
" 2009 | \n",
" 74134167.0 | \n",
"
\n",
" \n",
" 2538 | \n",
" USA | \n",
" under18 | \n",
" 2010 | \n",
" 74119556.0 | \n",
"
\n",
" \n",
" 2539 | \n",
" USA | \n",
" total | \n",
" 2010 | \n",
" 309326295.0 | \n",
"
\n",
" \n",
" 2540 | \n",
" USA | \n",
" under18 | \n",
" 2011 | \n",
" 73902222.0 | \n",
"
\n",
" \n",
" 2541 | \n",
" USA | \n",
" total | \n",
" 2011 | \n",
" 311582564.0 | \n",
"
\n",
" \n",
" 2542 | \n",
" USA | \n",
" under18 | \n",
" 2012 | \n",
" 73708179.0 | \n",
"
\n",
" \n",
" 2543 | \n",
" USA | \n",
" total | \n",
" 2012 | \n",
" 313873685.0 | \n",
"
\n",
" \n",
"
\n",
"
2544 rows × 4 columns
\n",
"
"
],
"text/plain": [
" state/region ages year population\n",
"0 AL under18 2012 1117489.0\n",
"1 AL total 2012 4817528.0\n",
"2 AL under18 2010 1130966.0\n",
"3 AL total 2010 4785570.0\n",
"4 AL under18 2011 1125763.0\n",
"5 AL total 2011 4801627.0\n",
"6 AL total 2009 4757938.0\n",
"7 AL under18 2009 1134192.0\n",
"8 AL under18 2013 1111481.0\n",
"9 AL total 2013 4833722.0\n",
"10 AL total 2007 4672840.0\n",
"11 AL under18 2007 1132296.0\n",
"12 AL total 2008 4718206.0\n",
"13 AL under18 2008 1134927.0\n",
"14 AL total 2005 4569805.0\n",
"15 AL under18 2005 1117229.0\n",
"16 AL total 2006 4628981.0\n",
"17 AL under18 2006 1126798.0\n",
"18 AL total 2004 4530729.0\n",
"19 AL under18 2004 1113662.0\n",
"20 AL total 2003 4503491.0\n",
"21 AL under18 2003 1113083.0\n",
"22 AL total 2001 4467634.0\n",
"23 AL under18 2001 1120409.0\n",
"24 AL total 2002 4480089.0\n",
"25 AL under18 2002 1116590.0\n",
"26 AL under18 1999 1121287.0\n",
"27 AL total 1999 4430141.0\n",
"28 AL total 2000 4452173.0\n",
"29 AL under18 2000 1122273.0\n",
"... ... ... ... ...\n",
"2514 USA under18 1999 71946051.0\n",
"2515 USA total 2000 282162411.0\n",
"2516 USA under18 2000 72376189.0\n",
"2517 USA total 1999 279040181.0\n",
"2518 USA total 2001 284968955.0\n",
"2519 USA under18 2001 72671175.0\n",
"2520 USA total 2002 287625193.0\n",
"2521 USA under18 2002 72936457.0\n",
"2522 USA total 2003 290107933.0\n",
"2523 USA under18 2003 73100758.0\n",
"2524 USA total 2004 292805298.0\n",
"2525 USA under18 2004 73297735.0\n",
"2526 USA total 2005 295516599.0\n",
"2527 USA under18 2005 73523669.0\n",
"2528 USA total 2006 298379912.0\n",
"2529 USA under18 2006 73757714.0\n",
"2530 USA total 2007 301231207.0\n",
"2531 USA under18 2007 74019405.0\n",
"2532 USA total 2008 304093966.0\n",
"2533 USA under18 2008 74104602.0\n",
"2534 USA under18 2013 73585872.0\n",
"2535 USA total 2013 316128839.0\n",
"2536 USA total 2009 306771529.0\n",
"2537 USA under18 2009 74134167.0\n",
"2538 USA under18 2010 74119556.0\n",
"2539 USA total 2010 309326295.0\n",
"2540 USA under18 2011 73902222.0\n",
"2541 USA total 2011 311582564.0\n",
"2542 USA under18 2012 73708179.0\n",
"2543 USA total 2012 313873685.0\n",
"\n",
"[2544 rows x 4 columns]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 美国的人口数据\n",
"pop = pd.read_csv('./state-population.csv')\n",
"pop"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(2544, 4)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop.shape"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" state/region | \n",
" ages | \n",
" year | \n",
" population | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" AL | \n",
" under18 | \n",
" 2012 | \n",
" 1117489.0 | \n",
"
\n",
" \n",
" 1 | \n",
" AL | \n",
" total | \n",
" 2012 | \n",
" 4817528.0 | \n",
"
\n",
" \n",
" 2 | \n",
" AL | \n",
" under18 | \n",
" 2010 | \n",
" 1130966.0 | \n",
"
\n",
" \n",
" 3 | \n",
" AL | \n",
" total | \n",
" 2010 | \n",
" 4785570.0 | \n",
"
\n",
" \n",
" 4 | \n",
" AL | \n",
" under18 | \n",
" 2011 | \n",
" 1125763.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" state/region ages year population\n",
"0 AL under18 2012 1117489.0\n",
"1 AL total 2012 4817528.0\n",
"2 AL under18 2010 1130966.0\n",
"3 AL total 2010 4785570.0\n",
"4 AL under18 2011 1125763.0"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop.head()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" state | \n",
" abbreviation | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Alabama | \n",
" AL | \n",
"
\n",
" \n",
" 1 | \n",
" Alaska | \n",
" AK | \n",
"
\n",
" \n",
" 2 | \n",
" Arizona | \n",
" AZ | \n",
"
\n",
" \n",
" 3 | \n",
" Arkansas | \n",
" AR | \n",
"
\n",
" \n",
" 4 | \n",
" California | \n",
" CA | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" state abbreviation\n",
"0 Alabama AL\n",
"1 Alaska AK\n",
"2 Arizona AZ\n",
"3 Arkansas AR\n",
"4 California CA"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"abbrevs.head()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"(2544, 4)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(51, 2)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"display(pop.shape,abbrevs.shape)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"(2544, 6)"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 级联时,数据变少了96个,哪些数据变少\n",
"# inner内连接,outer叫做外连接\n",
"pop2 = pop.merge(abbrevs,how = 'outer',left_on='state/region',right_on='abbreviation')\n",
"pop2.shape"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"state/region False\n",
"ages False\n",
"year False\n",
"population True\n",
"state True\n",
"abbreviation True\n",
"dtype: bool"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 前三列没有空值\n",
"pop2.isnull().any()"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" state/region | \n",
" ages | \n",
" year | \n",
" population | \n",
" state | \n",
" abbreviation | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" AL | \n",
" under18 | \n",
" 2012 | \n",
" 1117489.0 | \n",
" Alabama | \n",
" AL | \n",
"
\n",
" \n",
" 1 | \n",
" AL | \n",
" total | \n",
" 2012 | \n",
" 4817528.0 | \n",
" Alabama | \n",
" AL | \n",
"
\n",
" \n",
" 2 | \n",
" AL | \n",
" under18 | \n",
" 2010 | \n",
" 1130966.0 | \n",
" Alabama | \n",
" AL | \n",
"
\n",
" \n",
" 3 | \n",
" AL | \n",
" total | \n",
" 2010 | \n",
" 4785570.0 | \n",
" Alabama | \n",
" AL | \n",
"
\n",
" \n",
" 4 | \n",
" AL | \n",
" under18 | \n",
" 2011 | \n",
" 1125763.0 | \n",
" Alabama | \n",
" AL | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" state/region ages year population state abbreviation\n",
"0 AL under18 2012 1117489.0 Alabama AL\n",
"1 AL total 2012 4817528.0 Alabama AL\n",
"2 AL under18 2010 1130966.0 Alabama AL\n",
"3 AL total 2010 4785570.0 Alabama AL\n",
"4 AL under18 2011 1125763.0 Alabama AL"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop2.head()"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"# 删除一列\n",
"pop2.drop(labels = 'abbreviation',axis = 1,inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" state/region | \n",
" ages | \n",
" year | \n",
" population | \n",
" state | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" AL | \n",
" under18 | \n",
" 2012 | \n",
" 1117489.0 | \n",
" Alabama | \n",
"
\n",
" \n",
" 1 | \n",
" AL | \n",
" total | \n",
" 2012 | \n",
" 4817528.0 | \n",
" Alabama | \n",
"
\n",
" \n",
" 2 | \n",
" AL | \n",
" under18 | \n",
" 2010 | \n",
" 1130966.0 | \n",
" Alabama | \n",
"
\n",
" \n",
" 3 | \n",
" AL | \n",
" total | \n",
" 2010 | \n",
" 4785570.0 | \n",
" Alabama | \n",
"
\n",
" \n",
" 4 | \n",
" AL | \n",
" under18 | \n",
" 2011 | \n",
" 1125763.0 | \n",
" Alabama | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" state/region ages year population state\n",
"0 AL under18 2012 1117489.0 Alabama\n",
"1 AL total 2012 4817528.0 Alabama\n",
"2 AL under18 2010 1130966.0 Alabama\n",
"3 AL total 2010 4785570.0 Alabama\n",
"4 AL under18 2011 1125763.0 Alabama"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop2.head()"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"state/region False\n",
"ages False\n",
"year False\n",
"population True\n",
"state True\n",
"dtype: bool"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop2.isnull().any()"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"0 False\n",
"1 False\n",
"2 False\n",
"3 False\n",
"4 False\n",
"5 False\n",
"6 False\n",
"7 False\n",
"8 False\n",
"9 False\n",
"10 False\n",
"11 False\n",
"12 False\n",
"13 False\n",
"14 False\n",
"15 False\n",
"16 False\n",
"17 False\n",
"18 False\n",
"19 False\n",
"20 False\n",
"21 False\n",
"22 False\n",
"23 False\n",
"24 False\n",
"25 False\n",
"26 False\n",
"27 False\n",
"28 False\n",
"29 False\n",
" ... \n",
"2514 True\n",
"2515 True\n",
"2516 True\n",
"2517 True\n",
"2518 True\n",
"2519 True\n",
"2520 True\n",
"2521 True\n",
"2522 True\n",
"2523 True\n",
"2524 True\n",
"2525 True\n",
"2526 True\n",
"2527 True\n",
"2528 True\n",
"2529 True\n",
"2530 True\n",
"2531 True\n",
"2532 True\n",
"2533 True\n",
"2534 True\n",
"2535 True\n",
"2536 True\n",
"2537 True\n",
"2538 True\n",
"2539 True\n",
"2540 True\n",
"2541 True\n",
"2542 True\n",
"2543 True\n",
"Name: state, Length: 2544, dtype: bool"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 定位为空的数据\n",
"cond = pop2['state'].isnull()\n",
"cond"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array(['PR', 'USA'], dtype=object)"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 只有当state为空,返回,为空时True\n",
"# 去重操作,非重复值\n",
"pop2[cond]['state/region'].unique()"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"(51, 2)"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"abbrevs.shape"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(52, 2)"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"areas.shape"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" state | \n",
" area (sq. mi) | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Alabama | \n",
" 52423 | \n",
"
\n",
" \n",
" 1 | \n",
" Alaska | \n",
" 656425 | \n",
"
\n",
" \n",
" 2 | \n",
" Arizona | \n",
" 114006 | \n",
"
\n",
" \n",
" 3 | \n",
" Arkansas | \n",
" 53182 | \n",
"
\n",
" \n",
" 4 | \n",
" California | \n",
" 163707 | \n",
"
\n",
" \n",
" 5 | \n",
" Colorado | \n",
" 104100 | \n",
"
\n",
" \n",
" 6 | \n",
" Connecticut | \n",
" 5544 | \n",
"
\n",
" \n",
" 7 | \n",
" Delaware | \n",
" 1954 | \n",
"
\n",
" \n",
" 8 | \n",
" Florida | \n",
" 65758 | \n",
"
\n",
" \n",
" 9 | \n",
" Georgia | \n",
" 59441 | \n",
"
\n",
" \n",
" 10 | \n",
" Hawaii | \n",
" 10932 | \n",
"
\n",
" \n",
" 11 | \n",
" Idaho | \n",
" 83574 | \n",
"
\n",
" \n",
" 12 | \n",
" Illinois | \n",
" 57918 | \n",
"
\n",
" \n",
" 13 | \n",
" Indiana | \n",
" 36420 | \n",
"
\n",
" \n",
" 14 | \n",
" Iowa | \n",
" 56276 | \n",
"
\n",
" \n",
" 15 | \n",
" Kansas | \n",
" 82282 | \n",
"
\n",
" \n",
" 16 | \n",
" Kentucky | \n",
" 40411 | \n",
"
\n",
" \n",
" 17 | \n",
" Louisiana | \n",
" 51843 | \n",
"
\n",
" \n",
" 18 | \n",
" Maine | \n",
" 35387 | \n",
"
\n",
" \n",
" 19 | \n",
" Maryland | \n",
" 12407 | \n",
"
\n",
" \n",
" 20 | \n",
" Massachusetts | \n",
" 10555 | \n",
"
\n",
" \n",
" 21 | \n",
" Michigan | \n",
" 96810 | \n",
"
\n",
" \n",
" 22 | \n",
" Minnesota | \n",
" 86943 | \n",
"
\n",
" \n",
" 23 | \n",
" Mississippi | \n",
" 48434 | \n",
"
\n",
" \n",
" 24 | \n",
" Missouri | \n",
" 69709 | \n",
"
\n",
" \n",
" 25 | \n",
" Montana | \n",
" 147046 | \n",
"
\n",
" \n",
" 26 | \n",
" Nebraska | \n",
" 77358 | \n",
"
\n",
" \n",
" 27 | \n",
" Nevada | \n",
" 110567 | \n",
"
\n",
" \n",
" 28 | \n",
" New Hampshire | \n",
" 9351 | \n",
"
\n",
" \n",
" 29 | \n",
" New Jersey | \n",
" 8722 | \n",
"
\n",
" \n",
" 30 | \n",
" New Mexico | \n",
" 121593 | \n",
"
\n",
" \n",
" 31 | \n",
" New York | \n",
" 54475 | \n",
"
\n",
" \n",
" 32 | \n",
" North Carolina | \n",
" 53821 | \n",
"
\n",
" \n",
" 33 | \n",
" North Dakota | \n",
" 70704 | \n",
"
\n",
" \n",
" 34 | \n",
" Ohio | \n",
" 44828 | \n",
"
\n",
" \n",
" 35 | \n",
" Oklahoma | \n",
" 69903 | \n",
"
\n",
" \n",
" 36 | \n",
" Oregon | \n",
" 98386 | \n",
"
\n",
" \n",
" 37 | \n",
" Pennsylvania | \n",
" 46058 | \n",
"
\n",
" \n",
" 38 | \n",
" Rhode Island | \n",
" 1545 | \n",
"
\n",
" \n",
" 39 | \n",
" South Carolina | \n",
" 32007 | \n",
"
\n",
" \n",
" 40 | \n",
" South Dakota | \n",
" 77121 | \n",
"
\n",
" \n",
" 41 | \n",
" Tennessee | \n",
" 42146 | \n",
"
\n",
" \n",
" 42 | \n",
" Texas | \n",
" 268601 | \n",
"
\n",
" \n",
" 43 | \n",
" Utah | \n",
" 84904 | \n",
"
\n",
" \n",
" 44 | \n",
" Vermont | \n",
" 9615 | \n",
"
\n",
" \n",
" 45 | \n",
" Virginia | \n",
" 42769 | \n",
"
\n",
" \n",
" 46 | \n",
" Washington | \n",
" 71303 | \n",
"
\n",
" \n",
" 47 | \n",
" West Virginia | \n",
" 24231 | \n",
"
\n",
" \n",
" 48 | \n",
" Wisconsin | \n",
" 65503 | \n",
"
\n",
" \n",
" 49 | \n",
" Wyoming | \n",
" 97818 | \n",
"
\n",
" \n",
" 50 | \n",
" District of Columbia | \n",
" 68 | \n",
"
\n",
" \n",
" 51 | \n",
" Puerto Rico | \n",
" 3515 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" state area (sq. mi)\n",
"0 Alabama 52423\n",
"1 Alaska 656425\n",
"2 Arizona 114006\n",
"3 Arkansas 53182\n",
"4 California 163707\n",
"5 Colorado 104100\n",
"6 Connecticut 5544\n",
"7 Delaware 1954\n",
"8 Florida 65758\n",
"9 Georgia 59441\n",
"10 Hawaii 10932\n",
"11 Idaho 83574\n",
"12 Illinois 57918\n",
"13 Indiana 36420\n",
"14 Iowa 56276\n",
"15 Kansas 82282\n",
"16 Kentucky 40411\n",
"17 Louisiana 51843\n",
"18 Maine 35387\n",
"19 Maryland 12407\n",
"20 Massachusetts 10555\n",
"21 Michigan 96810\n",
"22 Minnesota 86943\n",
"23 Mississippi 48434\n",
"24 Missouri 69709\n",
"25 Montana 147046\n",
"26 Nebraska 77358\n",
"27 Nevada 110567\n",
"28 New Hampshire 9351\n",
"29 New Jersey 8722\n",
"30 New Mexico 121593\n",
"31 New York 54475\n",
"32 North Carolina 53821\n",
"33 North Dakota 70704\n",
"34 Ohio 44828\n",
"35 Oklahoma 69903\n",
"36 Oregon 98386\n",
"37 Pennsylvania 46058\n",
"38 Rhode Island 1545\n",
"39 South Carolina 32007\n",
"40 South Dakota 77121\n",
"41 Tennessee 42146\n",
"42 Texas 268601\n",
"43 Utah 84904\n",
"44 Vermont 9615\n",
"45 Virginia 42769\n",
"46 Washington 71303\n",
"47 West Virginia 24231\n",
"48 Wisconsin 65503\n",
"49 Wyoming 97818\n",
"50 District of Columbia 68\n",
"51 Puerto Rico 3515"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"areas"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"0 False\n",
"1 False\n",
"2 False\n",
"3 False\n",
"4 False\n",
"5 False\n",
"6 False\n",
"7 False\n",
"8 False\n",
"9 False\n",
"10 False\n",
"11 False\n",
"12 False\n",
"13 False\n",
"14 False\n",
"15 False\n",
"16 False\n",
"17 False\n",
"18 False\n",
"19 False\n",
"20 False\n",
"21 False\n",
"22 False\n",
"23 False\n",
"24 False\n",
"25 False\n",
"26 False\n",
"27 False\n",
"28 False\n",
"29 False\n",
" ... \n",
"2514 False\n",
"2515 False\n",
"2516 False\n",
"2517 False\n",
"2518 False\n",
"2519 False\n",
"2520 False\n",
"2521 False\n",
"2522 False\n",
"2523 False\n",
"2524 False\n",
"2525 False\n",
"2526 False\n",
"2527 False\n",
"2528 False\n",
"2529 False\n",
"2530 False\n",
"2531 False\n",
"2532 False\n",
"2533 False\n",
"2534 False\n",
"2535 False\n",
"2536 False\n",
"2537 False\n",
"2538 False\n",
"2539 False\n",
"2540 False\n",
"2541 False\n",
"2542 False\n",
"2543 False\n",
"Name: state/region, Length: 2544, dtype: bool"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cond = pop2['state/region'] == 'PR'\n",
"cond"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"d:\\python36\\lib\\site-packages\\ipykernel_launcher.py:1: SettingWithCopyWarning: \n",
"A value is trying to be set on a copy of a slice from a DataFrame\n",
"\n",
"See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n",
" \"\"\"Entry point for launching an IPython kernel.\n"
]
}
],
"source": [
"pop2['state'][cond] = 'Puerto Rico'"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"d:\\python36\\lib\\site-packages\\ipykernel_launcher.py:2: SettingWithCopyWarning: \n",
"A value is trying to be set on a copy of a slice from a DataFrame\n",
"\n",
"See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n",
" \n"
]
}
],
"source": [
"cond = pop2['state/region'] == 'USA'\n",
"pop2['state'][cond] = 'United State'"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"state/region False\n",
"ages False\n",
"year False\n",
"population True\n",
"state False\n",
"dtype: bool"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop2.isnull().any()"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"text/plain": [
"(20, 5)"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cond = pop2['population'].isnull()\n",
"pop2[cond].shape"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(2544, 5)"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop2.shape"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
"# 将难于进行补全的空数据进行删除\n",
"pop2.dropna(inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(2524, 5)"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop2.shape"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"state/region False\n",
"ages False\n",
"year False\n",
"population False\n",
"state False\n",
"dtype: bool"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop2.isnull().any()"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"state/region True\n",
"ages True\n",
"year True\n",
"population True\n",
"state True\n",
"dtype: bool"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop2.notnull().all()"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" state | \n",
" area (sq. mi) | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Alabama | \n",
" 52423 | \n",
"
\n",
" \n",
" 1 | \n",
" Alaska | \n",
" 656425 | \n",
"
\n",
" \n",
" 2 | \n",
" Arizona | \n",
" 114006 | \n",
"
\n",
" \n",
" 3 | \n",
" Arkansas | \n",
" 53182 | \n",
"
\n",
" \n",
" 4 | \n",
" California | \n",
" 163707 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" state area (sq. mi)\n",
"0 Alabama 52423\n",
"1 Alaska 656425\n",
"2 Arizona 114006\n",
"3 Arkansas 53182\n",
"4 California 163707"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"areas.head()"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" state/region | \n",
" ages | \n",
" year | \n",
" population | \n",
" state | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" AL | \n",
" under18 | \n",
" 2012 | \n",
" 1117489.0 | \n",
" Alabama | \n",
"
\n",
" \n",
" 1 | \n",
" AL | \n",
" total | \n",
" 2012 | \n",
" 4817528.0 | \n",
" Alabama | \n",
"
\n",
" \n",
" 2 | \n",
" AL | \n",
" under18 | \n",
" 2010 | \n",
" 1130966.0 | \n",
" Alabama | \n",
"
\n",
" \n",
" 3 | \n",
" AL | \n",
" total | \n",
" 2010 | \n",
" 4785570.0 | \n",
" Alabama | \n",
"
\n",
" \n",
" 4 | \n",
" AL | \n",
" under18 | \n",
" 2011 | \n",
" 1125763.0 | \n",
" Alabama | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" state/region ages year population state\n",
"0 AL under18 2012 1117489.0 Alabama\n",
"1 AL total 2012 4817528.0 Alabama\n",
"2 AL under18 2010 1130966.0 Alabama\n",
"3 AL total 2010 4785570.0 Alabama\n",
"4 AL under18 2011 1125763.0 Alabama"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop2.head()"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(2524, 6)"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop3 = pop2.merge(areas,how = 'outer')\n",
"pop3.shape"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" state/region | \n",
" ages | \n",
" year | \n",
" population | \n",
" state | \n",
" area (sq. mi) | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" AL | \n",
" under18 | \n",
" 2012 | \n",
" 1117489.0 | \n",
" Alabama | \n",
" 52423.0 | \n",
"
\n",
" \n",
" 1 | \n",
" AL | \n",
" total | \n",
" 2012 | \n",
" 4817528.0 | \n",
" Alabama | \n",
" 52423.0 | \n",
"
\n",
" \n",
" 2 | \n",
" AL | \n",
" under18 | \n",
" 2010 | \n",
" 1130966.0 | \n",
" Alabama | \n",
" 52423.0 | \n",
"
\n",
" \n",
" 3 | \n",
" AL | \n",
" total | \n",
" 2010 | \n",
" 4785570.0 | \n",
" Alabama | \n",
" 52423.0 | \n",
"
\n",
" \n",
" 4 | \n",
" AL | \n",
" under18 | \n",
" 2011 | \n",
" 1125763.0 | \n",
" Alabama | \n",
" 52423.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" state/region ages year population state area (sq. mi)\n",
"0 AL under18 2012 1117489.0 Alabama 52423.0\n",
"1 AL total 2012 4817528.0 Alabama 52423.0\n",
"2 AL under18 2010 1130966.0 Alabama 52423.0\n",
"3 AL total 2010 4785570.0 Alabama 52423.0\n",
"4 AL under18 2011 1125763.0 Alabama 52423.0"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop3.head()"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"state/region False\n",
"ages False\n",
"year False\n",
"population False\n",
"state False\n",
"area (sq. mi) True\n",
"dtype: bool"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop3.isnull().any()"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" state/region | \n",
" ages | \n",
" year | \n",
" population | \n",
" state | \n",
" area (sq. mi) | \n",
"
\n",
" \n",
" \n",
" \n",
" 2476 | \n",
" USA | \n",
" under18 | \n",
" 1990 | \n",
" 64218512.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2477 | \n",
" USA | \n",
" total | \n",
" 1990 | \n",
" 249622814.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2478 | \n",
" USA | \n",
" total | \n",
" 1991 | \n",
" 252980942.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2479 | \n",
" USA | \n",
" under18 | \n",
" 1991 | \n",
" 65313018.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2480 | \n",
" USA | \n",
" under18 | \n",
" 1992 | \n",
" 66509177.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2481 | \n",
" USA | \n",
" total | \n",
" 1992 | \n",
" 256514231.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2482 | \n",
" USA | \n",
" total | \n",
" 1993 | \n",
" 259918595.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2483 | \n",
" USA | \n",
" under18 | \n",
" 1993 | \n",
" 67594938.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2484 | \n",
" USA | \n",
" under18 | \n",
" 1994 | \n",
" 68640936.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2485 | \n",
" USA | \n",
" total | \n",
" 1994 | \n",
" 263125826.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2486 | \n",
" USA | \n",
" under18 | \n",
" 1995 | \n",
" 69473140.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2487 | \n",
" USA | \n",
" under18 | \n",
" 1996 | \n",
" 70233512.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2488 | \n",
" USA | \n",
" total | \n",
" 1995 | \n",
" 266278403.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2489 | \n",
" USA | \n",
" total | \n",
" 1996 | \n",
" 269394291.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2490 | \n",
" USA | \n",
" total | \n",
" 1997 | \n",
" 272646932.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2491 | \n",
" USA | \n",
" under18 | \n",
" 1997 | \n",
" 70920738.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2492 | \n",
" USA | \n",
" under18 | \n",
" 1998 | \n",
" 71431406.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2493 | \n",
" USA | \n",
" total | \n",
" 1998 | \n",
" 275854116.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2494 | \n",
" USA | \n",
" under18 | \n",
" 1999 | \n",
" 71946051.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2495 | \n",
" USA | \n",
" total | \n",
" 2000 | \n",
" 282162411.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2496 | \n",
" USA | \n",
" under18 | \n",
" 2000 | \n",
" 72376189.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2497 | \n",
" USA | \n",
" total | \n",
" 1999 | \n",
" 279040181.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2498 | \n",
" USA | \n",
" total | \n",
" 2001 | \n",
" 284968955.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2499 | \n",
" USA | \n",
" under18 | \n",
" 2001 | \n",
" 72671175.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2500 | \n",
" USA | \n",
" total | \n",
" 2002 | \n",
" 287625193.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2501 | \n",
" USA | \n",
" under18 | \n",
" 2002 | \n",
" 72936457.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2502 | \n",
" USA | \n",
" total | \n",
" 2003 | \n",
" 290107933.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2503 | \n",
" USA | \n",
" under18 | \n",
" 2003 | \n",
" 73100758.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2504 | \n",
" USA | \n",
" total | \n",
" 2004 | \n",
" 292805298.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2505 | \n",
" USA | \n",
" under18 | \n",
" 2004 | \n",
" 73297735.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2506 | \n",
" USA | \n",
" total | \n",
" 2005 | \n",
" 295516599.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2507 | \n",
" USA | \n",
" under18 | \n",
" 2005 | \n",
" 73523669.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2508 | \n",
" USA | \n",
" total | \n",
" 2006 | \n",
" 298379912.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2509 | \n",
" USA | \n",
" under18 | \n",
" 2006 | \n",
" 73757714.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2510 | \n",
" USA | \n",
" total | \n",
" 2007 | \n",
" 301231207.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2511 | \n",
" USA | \n",
" under18 | \n",
" 2007 | \n",
" 74019405.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2512 | \n",
" USA | \n",
" total | \n",
" 2008 | \n",
" 304093966.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2513 | \n",
" USA | \n",
" under18 | \n",
" 2008 | \n",
" 74104602.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2514 | \n",
" USA | \n",
" under18 | \n",
" 2013 | \n",
" 73585872.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2515 | \n",
" USA | \n",
" total | \n",
" 2013 | \n",
" 316128839.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2516 | \n",
" USA | \n",
" total | \n",
" 2009 | \n",
" 306771529.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2517 | \n",
" USA | \n",
" under18 | \n",
" 2009 | \n",
" 74134167.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2518 | \n",
" USA | \n",
" under18 | \n",
" 2010 | \n",
" 74119556.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2519 | \n",
" USA | \n",
" total | \n",
" 2010 | \n",
" 309326295.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2520 | \n",
" USA | \n",
" under18 | \n",
" 2011 | \n",
" 73902222.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2521 | \n",
" USA | \n",
" total | \n",
" 2011 | \n",
" 311582564.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2522 | \n",
" USA | \n",
" under18 | \n",
" 2012 | \n",
" 73708179.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
" 2523 | \n",
" USA | \n",
" total | \n",
" 2012 | \n",
" 313873685.0 | \n",
" United State | \n",
" NaN | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" state/region ages year population state area (sq. mi)\n",
"2476 USA under18 1990 64218512.0 United State NaN\n",
"2477 USA total 1990 249622814.0 United State NaN\n",
"2478 USA total 1991 252980942.0 United State NaN\n",
"2479 USA under18 1991 65313018.0 United State NaN\n",
"2480 USA under18 1992 66509177.0 United State NaN\n",
"2481 USA total 1992 256514231.0 United State NaN\n",
"2482 USA total 1993 259918595.0 United State NaN\n",
"2483 USA under18 1993 67594938.0 United State NaN\n",
"2484 USA under18 1994 68640936.0 United State NaN\n",
"2485 USA total 1994 263125826.0 United State NaN\n",
"2486 USA under18 1995 69473140.0 United State NaN\n",
"2487 USA under18 1996 70233512.0 United State NaN\n",
"2488 USA total 1995 266278403.0 United State NaN\n",
"2489 USA total 1996 269394291.0 United State NaN\n",
"2490 USA total 1997 272646932.0 United State NaN\n",
"2491 USA under18 1997 70920738.0 United State NaN\n",
"2492 USA under18 1998 71431406.0 United State NaN\n",
"2493 USA total 1998 275854116.0 United State NaN\n",
"2494 USA under18 1999 71946051.0 United State NaN\n",
"2495 USA total 2000 282162411.0 United State NaN\n",
"2496 USA under18 2000 72376189.0 United State NaN\n",
"2497 USA total 1999 279040181.0 United State NaN\n",
"2498 USA total 2001 284968955.0 United State NaN\n",
"2499 USA under18 2001 72671175.0 United State NaN\n",
"2500 USA total 2002 287625193.0 United State NaN\n",
"2501 USA under18 2002 72936457.0 United State NaN\n",
"2502 USA total 2003 290107933.0 United State NaN\n",
"2503 USA under18 2003 73100758.0 United State NaN\n",
"2504 USA total 2004 292805298.0 United State NaN\n",
"2505 USA under18 2004 73297735.0 United State NaN\n",
"2506 USA total 2005 295516599.0 United State NaN\n",
"2507 USA under18 2005 73523669.0 United State NaN\n",
"2508 USA total 2006 298379912.0 United State NaN\n",
"2509 USA under18 2006 73757714.0 United State NaN\n",
"2510 USA total 2007 301231207.0 United State NaN\n",
"2511 USA under18 2007 74019405.0 United State NaN\n",
"2512 USA total 2008 304093966.0 United State NaN\n",
"2513 USA under18 2008 74104602.0 United State NaN\n",
"2514 USA under18 2013 73585872.0 United State NaN\n",
"2515 USA total 2013 316128839.0 United State NaN\n",
"2516 USA total 2009 306771529.0 United State NaN\n",
"2517 USA under18 2009 74134167.0 United State NaN\n",
"2518 USA under18 2010 74119556.0 United State NaN\n",
"2519 USA total 2010 309326295.0 United State NaN\n",
"2520 USA under18 2011 73902222.0 United State NaN\n",
"2521 USA total 2011 311582564.0 United State NaN\n",
"2522 USA under18 2012 73708179.0 United State NaN\n",
"2523 USA total 2012 313873685.0 United State NaN"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cond = pop3['area (sq. mi)'].isnull()\n",
"pop3[cond]"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3790399"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = areas['area (sq. mi)'].sum()\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"d:\\python36\\lib\\site-packages\\ipykernel_launcher.py:3: SettingWithCopyWarning: \n",
"A value is trying to be set on a copy of a slice from a DataFrame\n",
"\n",
"See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n",
" This is separate from the ipykernel package so we can avoid doing imports until\n"
]
}
],
"source": [
"cond = pop3['state'] == \"United State\"\n",
"\n",
"pop3['area (sq. mi)'][cond] = a"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"state/region True\n",
"ages True\n",
"year True\n",
"population True\n",
"state True\n",
"area (sq. mi) True\n",
"dtype: bool"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop3.notnull().all()"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" state/region | \n",
" ages | \n",
" year | \n",
" population | \n",
" state | \n",
" area (sq. mi) | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" AL | \n",
" under18 | \n",
" 2012 | \n",
" 1117489.0 | \n",
" Alabama | \n",
" 52423.0 | \n",
"
\n",
" \n",
" 1 | \n",
" AL | \n",
" total | \n",
" 2012 | \n",
" 4817528.0 | \n",
" Alabama | \n",
" 52423.0 | \n",
"
\n",
" \n",
" 2 | \n",
" AL | \n",
" under18 | \n",
" 2010 | \n",
" 1130966.0 | \n",
" Alabama | \n",
" 52423.0 | \n",
"
\n",
" \n",
" 3 | \n",
" AL | \n",
" total | \n",
" 2010 | \n",
" 4785570.0 | \n",
" Alabama | \n",
" 52423.0 | \n",
"
\n",
" \n",
" 4 | \n",
" AL | \n",
" under18 | \n",
" 2011 | \n",
" 1125763.0 | \n",
" Alabama | \n",
" 52423.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" state/region ages year population state area (sq. mi)\n",
"0 AL under18 2012 1117489.0 Alabama 52423.0\n",
"1 AL total 2012 4817528.0 Alabama 52423.0\n",
"2 AL under18 2010 1130966.0 Alabama 52423.0\n",
"3 AL total 2010 4785570.0 Alabama 52423.0\n",
"4 AL under18 2011 1125763.0 Alabama 52423.0"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop3.head()"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"0 21.3\n",
"1 91.9\n",
"2 21.6\n",
"3 91.3\n",
"4 21.5\n",
"5 91.6\n",
"6 90.8\n",
"7 21.6\n",
"8 21.2\n",
"9 92.2\n",
"10 89.1\n",
"11 21.6\n",
"12 90.0\n",
"13 21.6\n",
"14 87.2\n",
"15 21.3\n",
"16 88.3\n",
"17 21.5\n",
"18 86.4\n",
"19 21.2\n",
"20 85.9\n",
"21 21.2\n",
"22 85.2\n",
"23 21.4\n",
"24 85.5\n",
"25 21.3\n",
"26 21.4\n",
"27 84.5\n",
"28 84.9\n",
"29 21.4\n",
" ... \n",
"2494 19.0\n",
"2495 74.4\n",
"2496 19.1\n",
"2497 73.6\n",
"2498 75.2\n",
"2499 19.2\n",
"2500 75.9\n",
"2501 19.2\n",
"2502 76.5\n",
"2503 19.3\n",
"2504 77.2\n",
"2505 19.3\n",
"2506 78.0\n",
"2507 19.4\n",
"2508 78.7\n",
"2509 19.5\n",
"2510 79.5\n",
"2511 19.5\n",
"2512 80.2\n",
"2513 19.6\n",
"2514 19.4\n",
"2515 83.4\n",
"2516 80.9\n",
"2517 19.6\n",
"2518 19.6\n",
"2519 81.6\n",
"2520 19.5\n",
"2521 82.2\n",
"2522 19.4\n",
"2523 82.8\n",
"Length: 2524, dtype: float64"
]
},
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop_density = (pop3['population']/pop3['area (sq. mi)']).round(1)\n",
"pop_density"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 0 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 21.3 | \n",
"
\n",
" \n",
" 1 | \n",
" 91.9 | \n",
"
\n",
" \n",
" 2 | \n",
" 21.6 | \n",
"
\n",
" \n",
" 3 | \n",
" 91.3 | \n",
"
\n",
" \n",
" 4 | \n",
" 21.5 | \n",
"
\n",
" \n",
" 5 | \n",
" 91.6 | \n",
"
\n",
" \n",
" 6 | \n",
" 90.8 | \n",
"
\n",
" \n",
" 7 | \n",
" 21.6 | \n",
"
\n",
" \n",
" 8 | \n",
" 21.2 | \n",
"
\n",
" \n",
" 9 | \n",
" 92.2 | \n",
"
\n",
" \n",
" 10 | \n",
" 89.1 | \n",
"
\n",
" \n",
" 11 | \n",
" 21.6 | \n",
"
\n",
" \n",
" 12 | \n",
" 90.0 | \n",
"
\n",
" \n",
" 13 | \n",
" 21.6 | \n",
"
\n",
" \n",
" 14 | \n",
" 87.2 | \n",
"
\n",
" \n",
" 15 | \n",
" 21.3 | \n",
"
\n",
" \n",
" 16 | \n",
" 88.3 | \n",
"
\n",
" \n",
" 17 | \n",
" 21.5 | \n",
"
\n",
" \n",
" 18 | \n",
" 86.4 | \n",
"
\n",
" \n",
" 19 | \n",
" 21.2 | \n",
"
\n",
" \n",
" 20 | \n",
" 85.9 | \n",
"
\n",
" \n",
" 21 | \n",
" 21.2 | \n",
"
\n",
" \n",
" 22 | \n",
" 85.2 | \n",
"
\n",
" \n",
" 23 | \n",
" 21.4 | \n",
"
\n",
" \n",
" 24 | \n",
" 85.5 | \n",
"
\n",
" \n",
" 25 | \n",
" 21.3 | \n",
"
\n",
" \n",
" 26 | \n",
" 21.4 | \n",
"
\n",
" \n",
" 27 | \n",
" 84.5 | \n",
"
\n",
" \n",
" 28 | \n",
" 84.9 | \n",
"
\n",
" \n",
" 29 | \n",
" 21.4 | \n",
"
\n",
" \n",
" ... | \n",
" ... | \n",
"
\n",
" \n",
" 2494 | \n",
" 19.0 | \n",
"
\n",
" \n",
" 2495 | \n",
" 74.4 | \n",
"
\n",
" \n",
" 2496 | \n",
" 19.1 | \n",
"
\n",
" \n",
" 2497 | \n",
" 73.6 | \n",
"
\n",
" \n",
" 2498 | \n",
" 75.2 | \n",
"
\n",
" \n",
" 2499 | \n",
" 19.2 | \n",
"
\n",
" \n",
" 2500 | \n",
" 75.9 | \n",
"
\n",
" \n",
" 2501 | \n",
" 19.2 | \n",
"
\n",
" \n",
" 2502 | \n",
" 76.5 | \n",
"
\n",
" \n",
" 2503 | \n",
" 19.3 | \n",
"
\n",
" \n",
" 2504 | \n",
" 77.2 | \n",
"
\n",
" \n",
" 2505 | \n",
" 19.3 | \n",
"
\n",
" \n",
" 2506 | \n",
" 78.0 | \n",
"
\n",
" \n",
" 2507 | \n",
" 19.4 | \n",
"
\n",
" \n",
" 2508 | \n",
" 78.7 | \n",
"
\n",
" \n",
" 2509 | \n",
" 19.5 | \n",
"
\n",
" \n",
" 2510 | \n",
" 79.5 | \n",
"
\n",
" \n",
" 2511 | \n",
" 19.5 | \n",
"
\n",
" \n",
" 2512 | \n",
" 80.2 | \n",
"
\n",
" \n",
" 2513 | \n",
" 19.6 | \n",
"
\n",
" \n",
" 2514 | \n",
" 19.4 | \n",
"
\n",
" \n",
" 2515 | \n",
" 83.4 | \n",
"
\n",
" \n",
" 2516 | \n",
" 80.9 | \n",
"
\n",
" \n",
" 2517 | \n",
" 19.6 | \n",
"
\n",
" \n",
" 2518 | \n",
" 19.6 | \n",
"
\n",
" \n",
" 2519 | \n",
" 81.6 | \n",
"
\n",
" \n",
" 2520 | \n",
" 19.5 | \n",
"
\n",
" \n",
" 2521 | \n",
" 82.2 | \n",
"
\n",
" \n",
" 2522 | \n",
" 19.4 | \n",
"
\n",
" \n",
" 2523 | \n",
" 82.8 | \n",
"
\n",
" \n",
"
\n",
"
2524 rows × 1 columns
\n",
"
"
],
"text/plain": [
" 0\n",
"0 21.3\n",
"1 91.9\n",
"2 21.6\n",
"3 91.3\n",
"4 21.5\n",
"5 91.6\n",
"6 90.8\n",
"7 21.6\n",
"8 21.2\n",
"9 92.2\n",
"10 89.1\n",
"11 21.6\n",
"12 90.0\n",
"13 21.6\n",
"14 87.2\n",
"15 21.3\n",
"16 88.3\n",
"17 21.5\n",
"18 86.4\n",
"19 21.2\n",
"20 85.9\n",
"21 21.2\n",
"22 85.2\n",
"23 21.4\n",
"24 85.5\n",
"25 21.3\n",
"26 21.4\n",
"27 84.5\n",
"28 84.9\n",
"29 21.4\n",
"... ...\n",
"2494 19.0\n",
"2495 74.4\n",
"2496 19.1\n",
"2497 73.6\n",
"2498 75.2\n",
"2499 19.2\n",
"2500 75.9\n",
"2501 19.2\n",
"2502 76.5\n",
"2503 19.3\n",
"2504 77.2\n",
"2505 19.3\n",
"2506 78.0\n",
"2507 19.4\n",
"2508 78.7\n",
"2509 19.5\n",
"2510 79.5\n",
"2511 19.5\n",
"2512 80.2\n",
"2513 19.6\n",
"2514 19.4\n",
"2515 83.4\n",
"2516 80.9\n",
"2517 19.6\n",
"2518 19.6\n",
"2519 81.6\n",
"2520 19.5\n",
"2521 82.2\n",
"2522 19.4\n",
"2523 82.8\n",
"\n",
"[2524 rows x 1 columns]"
]
},
"execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop_density = DataFrame(pop_density)\n",
"pop_density"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" pop_density | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 21.3 | \n",
"
\n",
" \n",
" 1 | \n",
" 91.9 | \n",
"
\n",
" \n",
" 2 | \n",
" 21.6 | \n",
"
\n",
" \n",
" 3 | \n",
" 91.3 | \n",
"
\n",
" \n",
" 4 | \n",
" 21.5 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" pop_density\n",
"0 21.3\n",
"1 91.9\n",
"2 21.6\n",
"3 91.3\n",
"4 21.5"
]
},
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop_density.columns = ['pop_density']\n",
"pop_density.head()"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" state/region | \n",
" ages | \n",
" year | \n",
" population | \n",
" state | \n",
" area (sq. mi) | \n",
" pop_density | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" AL | \n",
" under18 | \n",
" 2012 | \n",
" 1117489.0 | \n",
" Alabama | \n",
" 52423.0 | \n",
" 21.3 | \n",
"
\n",
" \n",
" 1 | \n",
" AL | \n",
" total | \n",
" 2012 | \n",
" 4817528.0 | \n",
" Alabama | \n",
" 52423.0 | \n",
" 91.9 | \n",
"
\n",
" \n",
" 2 | \n",
" AL | \n",
" under18 | \n",
" 2010 | \n",
" 1130966.0 | \n",
" Alabama | \n",
" 52423.0 | \n",
" 21.6 | \n",
"
\n",
" \n",
" 3 | \n",
" AL | \n",
" total | \n",
" 2010 | \n",
" 4785570.0 | \n",
" Alabama | \n",
" 52423.0 | \n",
" 91.3 | \n",
"
\n",
" \n",
" 4 | \n",
" AL | \n",
" under18 | \n",
" 2011 | \n",
" 1125763.0 | \n",
" Alabama | \n",
" 52423.0 | \n",
" 21.5 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" state/region ages year population state area (sq. mi) pop_density\n",
"0 AL under18 2012 1117489.0 Alabama 52423.0 21.3\n",
"1 AL total 2012 4817528.0 Alabama 52423.0 91.9\n",
"2 AL under18 2010 1130966.0 Alabama 52423.0 21.6\n",
"3 AL total 2010 4785570.0 Alabama 52423.0 91.3\n",
"4 AL under18 2011 1125763.0 Alabama 52423.0 21.5"
]
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop4 = pop3.merge(pop_density,left_index=True,right_index=True)\n",
"pop4.head()"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"array([2012, 2010, 2011, 2009, 2013, 2007, 2008, 2005, 2006, 2004, 2003,\n",
" 2001, 2002, 1999, 2000, 1998, 1997, 1996, 1995, 1994, 1993, 1992,\n",
" 1991, 1990], dtype=int64)"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop4['year'].unique()"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array(['under18', 'total'], dtype=object)"
]
},
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop4['ages'].unique()"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" state/region | \n",
" ages | \n",
" year | \n",
" population | \n",
" state | \n",
" area (sq. mi) | \n",
" pop_density | \n",
"
\n",
" \n",
" \n",
" \n",
" 1 | \n",
" AL | \n",
" total | \n",
" 2012 | \n",
" 4817528.0 | \n",
" Alabama | \n",
" 52423.0 | \n",
" 91.9 | \n",
"
\n",
" \n",
" 95 | \n",
" AK | \n",
" total | \n",
" 2012 | \n",
" 730307.0 | \n",
" Alaska | \n",
" 656425.0 | \n",
" 1.1 | \n",
"
\n",
" \n",
" 97 | \n",
" AZ | \n",
" total | \n",
" 2012 | \n",
" 6551149.0 | \n",
" Arizona | \n",
" 114006.0 | \n",
" 57.5 | \n",
"
\n",
" \n",
" 191 | \n",
" AR | \n",
" total | \n",
" 2012 | \n",
" 2949828.0 | \n",
" Arkansas | \n",
" 53182.0 | \n",
" 55.5 | \n",
"
\n",
" \n",
" 193 | \n",
" CA | \n",
" total | \n",
" 2012 | \n",
" 37999878.0 | \n",
" California | \n",
" 163707.0 | \n",
" 232.1 | \n",
"
\n",
" \n",
" 287 | \n",
" CO | \n",
" total | \n",
" 2012 | \n",
" 5189458.0 | \n",
" Colorado | \n",
" 104100.0 | \n",
" 49.9 | \n",
"
\n",
" \n",
" 289 | \n",
" CT | \n",
" total | \n",
" 2012 | \n",
" 3591765.0 | \n",
" Connecticut | \n",
" 5544.0 | \n",
" 647.9 | \n",
"
\n",
" \n",
" 383 | \n",
" DE | \n",
" total | \n",
" 2012 | \n",
" 917053.0 | \n",
" Delaware | \n",
" 1954.0 | \n",
" 469.3 | \n",
"
\n",
" \n",
" 385 | \n",
" DC | \n",
" total | \n",
" 2012 | \n",
" 633427.0 | \n",
" District of Columbia | \n",
" 68.0 | \n",
" 9315.1 | \n",
"
\n",
" \n",
" 479 | \n",
" FL | \n",
" total | \n",
" 2012 | \n",
" 19320749.0 | \n",
" Florida | \n",
" 65758.0 | \n",
" 293.8 | \n",
"
\n",
" \n",
" 480 | \n",
" GA | \n",
" total | \n",
" 2012 | \n",
" 9915646.0 | \n",
" Georgia | \n",
" 59441.0 | \n",
" 166.8 | \n",
"
\n",
" \n",
" 575 | \n",
" HI | \n",
" total | \n",
" 2012 | \n",
" 1390090.0 | \n",
" Hawaii | \n",
" 10932.0 | \n",
" 127.2 | \n",
"
\n",
" \n",
" 576 | \n",
" ID | \n",
" total | \n",
" 2012 | \n",
" 1595590.0 | \n",
" Idaho | \n",
" 83574.0 | \n",
" 19.1 | \n",
"
\n",
" \n",
" 671 | \n",
" IL | \n",
" total | \n",
" 2012 | \n",
" 12868192.0 | \n",
" Illinois | \n",
" 57918.0 | \n",
" 222.2 | \n",
"
\n",
" \n",
" 672 | \n",
" IN | \n",
" total | \n",
" 2012 | \n",
" 6537782.0 | \n",
" Indiana | \n",
" 36420.0 | \n",
" 179.5 | \n",
"
\n",
" \n",
" 767 | \n",
" IA | \n",
" total | \n",
" 2012 | \n",
" 3075039.0 | \n",
" Iowa | \n",
" 56276.0 | \n",
" 54.6 | \n",
"
\n",
" \n",
" 768 | \n",
" KS | \n",
" total | \n",
" 2012 | \n",
" 2885398.0 | \n",
" Kansas | \n",
" 82282.0 | \n",
" 35.1 | \n",
"
\n",
" \n",
" 863 | \n",
" KY | \n",
" total | \n",
" 2012 | \n",
" 4379730.0 | \n",
" Kentucky | \n",
" 40411.0 | \n",
" 108.4 | \n",
"
\n",
" \n",
" 864 | \n",
" LA | \n",
" total | \n",
" 2012 | \n",
" 4602134.0 | \n",
" Louisiana | \n",
" 51843.0 | \n",
" 88.8 | \n",
"
\n",
" \n",
" 959 | \n",
" ME | \n",
" total | \n",
" 2012 | \n",
" 1328501.0 | \n",
" Maine | \n",
" 35387.0 | \n",
" 37.5 | \n",
"
\n",
" \n",
" 960 | \n",
" MD | \n",
" total | \n",
" 2012 | \n",
" 5884868.0 | \n",
" Maryland | \n",
" 12407.0 | \n",
" 474.3 | \n",
"
\n",
" \n",
" 1055 | \n",
" MA | \n",
" total | \n",
" 2012 | \n",
" 6645303.0 | \n",
" Massachusetts | \n",
" 10555.0 | \n",
" 629.6 | \n",
"
\n",
" \n",
" 1056 | \n",
" MI | \n",
" total | \n",
" 2012 | \n",
" 9882519.0 | \n",
" Michigan | \n",
" 96810.0 | \n",
" 102.1 | \n",
"
\n",
" \n",
" 1151 | \n",
" MN | \n",
" total | \n",
" 2012 | \n",
" 5379646.0 | \n",
" Minnesota | \n",
" 86943.0 | \n",
" 61.9 | \n",
"
\n",
" \n",
" 1152 | \n",
" MS | \n",
" total | \n",
" 2012 | \n",
" 2986450.0 | \n",
" Mississippi | \n",
" 48434.0 | \n",
" 61.7 | \n",
"
\n",
" \n",
" 1247 | \n",
" MO | \n",
" total | \n",
" 2012 | \n",
" 6024522.0 | \n",
" Missouri | \n",
" 69709.0 | \n",
" 86.4 | \n",
"
\n",
" \n",
" 1248 | \n",
" MT | \n",
" total | \n",
" 2012 | \n",
" 1005494.0 | \n",
" Montana | \n",
" 147046.0 | \n",
" 6.8 | \n",
"
\n",
" \n",
" 1343 | \n",
" NE | \n",
" total | \n",
" 2012 | \n",
" 1855350.0 | \n",
" Nebraska | \n",
" 77358.0 | \n",
" 24.0 | \n",
"
\n",
" \n",
" 1344 | \n",
" NV | \n",
" total | \n",
" 2012 | \n",
" 2754354.0 | \n",
" Nevada | \n",
" 110567.0 | \n",
" 24.9 | \n",
"
\n",
" \n",
" 1439 | \n",
" NH | \n",
" total | \n",
" 2012 | \n",
" 1321617.0 | \n",
" New Hampshire | \n",
" 9351.0 | \n",
" 141.3 | \n",
"
\n",
" \n",
" 1440 | \n",
" NJ | \n",
" total | \n",
" 2012 | \n",
" 8867749.0 | \n",
" New Jersey | \n",
" 8722.0 | \n",
" 1016.7 | \n",
"
\n",
" \n",
" 1535 | \n",
" NM | \n",
" total | \n",
" 2012 | \n",
" 2083540.0 | \n",
" New Mexico | \n",
" 121593.0 | \n",
" 17.1 | \n",
"
\n",
" \n",
" 1536 | \n",
" NY | \n",
" total | \n",
" 2012 | \n",
" 19576125.0 | \n",
" New York | \n",
" 54475.0 | \n",
" 359.4 | \n",
"
\n",
" \n",
" 1631 | \n",
" NC | \n",
" total | \n",
" 2012 | \n",
" 9748364.0 | \n",
" North Carolina | \n",
" 53821.0 | \n",
" 181.1 | \n",
"
\n",
" \n",
" 1632 | \n",
" ND | \n",
" total | \n",
" 2012 | \n",
" 701345.0 | \n",
" North Dakota | \n",
" 70704.0 | \n",
" 9.9 | \n",
"
\n",
" \n",
" 1727 | \n",
" OH | \n",
" total | \n",
" 2012 | \n",
" 11553031.0 | \n",
" Ohio | \n",
" 44828.0 | \n",
" 257.7 | \n",
"
\n",
" \n",
" 1728 | \n",
" OK | \n",
" total | \n",
" 2012 | \n",
" 3815780.0 | \n",
" Oklahoma | \n",
" 69903.0 | \n",
" 54.6 | \n",
"
\n",
" \n",
" 1823 | \n",
" OR | \n",
" total | \n",
" 2012 | \n",
" 3899801.0 | \n",
" Oregon | \n",
" 98386.0 | \n",
" 39.6 | \n",
"
\n",
" \n",
" 1824 | \n",
" PA | \n",
" total | \n",
" 2012 | \n",
" 12764475.0 | \n",
" Pennsylvania | \n",
" 46058.0 | \n",
" 277.1 | \n",
"
\n",
" \n",
" 1919 | \n",
" RI | \n",
" total | \n",
" 2012 | \n",
" 1050304.0 | \n",
" Rhode Island | \n",
" 1545.0 | \n",
" 679.8 | \n",
"
\n",
" \n",
" 1920 | \n",
" SC | \n",
" total | \n",
" 2012 | \n",
" 4723417.0 | \n",
" South Carolina | \n",
" 32007.0 | \n",
" 147.6 | \n",
"
\n",
" \n",
" 2015 | \n",
" SD | \n",
" total | \n",
" 2012 | \n",
" 834047.0 | \n",
" South Dakota | \n",
" 77121.0 | \n",
" 10.8 | \n",
"
\n",
" \n",
" 2016 | \n",
" TN | \n",
" total | \n",
" 2012 | \n",
" 6454914.0 | \n",
" Tennessee | \n",
" 42146.0 | \n",
" 153.2 | \n",
"
\n",
" \n",
" 2111 | \n",
" TX | \n",
" total | \n",
" 2012 | \n",
" 26060796.0 | \n",
" Texas | \n",
" 268601.0 | \n",
" 97.0 | \n",
"
\n",
" \n",
" 2112 | \n",
" UT | \n",
" total | \n",
" 2012 | \n",
" 2854871.0 | \n",
" Utah | \n",
" 84904.0 | \n",
" 33.6 | \n",
"
\n",
" \n",
" 2207 | \n",
" VT | \n",
" total | \n",
" 2012 | \n",
" 625953.0 | \n",
" Vermont | \n",
" 9615.0 | \n",
" 65.1 | \n",
"
\n",
" \n",
" 2208 | \n",
" VA | \n",
" total | \n",
" 2012 | \n",
" 8186628.0 | \n",
" Virginia | \n",
" 42769.0 | \n",
" 191.4 | \n",
"
\n",
" \n",
" 2303 | \n",
" WA | \n",
" total | \n",
" 2012 | \n",
" 6895318.0 | \n",
" Washington | \n",
" 71303.0 | \n",
" 96.7 | \n",
"
\n",
" \n",
" 2304 | \n",
" WV | \n",
" total | \n",
" 2012 | \n",
" 1856680.0 | \n",
" West Virginia | \n",
" 24231.0 | \n",
" 76.6 | \n",
"
\n",
" \n",
" 2399 | \n",
" WI | \n",
" total | \n",
" 2012 | \n",
" 5724554.0 | \n",
" Wisconsin | \n",
" 65503.0 | \n",
" 87.4 | \n",
"
\n",
" \n",
" 2400 | \n",
" WY | \n",
" total | \n",
" 2012 | \n",
" 576626.0 | \n",
" Wyoming | \n",
" 97818.0 | \n",
" 5.9 | \n",
"
\n",
" \n",
" 2475 | \n",
" PR | \n",
" total | \n",
" 2012 | \n",
" 3651545.0 | \n",
" Puerto Rico | \n",
" 3515.0 | \n",
" 1038.8 | \n",
"
\n",
" \n",
" 2523 | \n",
" USA | \n",
" total | \n",
" 2012 | \n",
" 313873685.0 | \n",
" United State | \n",
" 3790399.0 | \n",
" 82.8 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" state/region ages year population state area (sq. mi) pop_density\n",
"1 AL total 2012 4817528.0 Alabama 52423.0 91.9\n",
"95 AK total 2012 730307.0 Alaska 656425.0 1.1\n",
"97 AZ total 2012 6551149.0 Arizona 114006.0 57.5\n",
"191 AR total 2012 2949828.0 Arkansas 53182.0 55.5\n",
"193 CA total 2012 37999878.0 California 163707.0 232.1\n",
"287 CO total 2012 5189458.0 Colorado 104100.0 49.9\n",
"289 CT total 2012 3591765.0 Connecticut 5544.0 647.9\n",
"383 DE total 2012 917053.0 Delaware 1954.0 469.3\n",
"385 DC total 2012 633427.0 District of Columbia 68.0 9315.1\n",
"479 FL total 2012 19320749.0 Florida 65758.0 293.8\n",
"480 GA total 2012 9915646.0 Georgia 59441.0 166.8\n",
"575 HI total 2012 1390090.0 Hawaii 10932.0 127.2\n",
"576 ID total 2012 1595590.0 Idaho 83574.0 19.1\n",
"671 IL total 2012 12868192.0 Illinois 57918.0 222.2\n",
"672 IN total 2012 6537782.0 Indiana 36420.0 179.5\n",
"767 IA total 2012 3075039.0 Iowa 56276.0 54.6\n",
"768 KS total 2012 2885398.0 Kansas 82282.0 35.1\n",
"863 KY total 2012 4379730.0 Kentucky 40411.0 108.4\n",
"864 LA total 2012 4602134.0 Louisiana 51843.0 88.8\n",
"959 ME total 2012 1328501.0 Maine 35387.0 37.5\n",
"960 MD total 2012 5884868.0 Maryland 12407.0 474.3\n",
"1055 MA total 2012 6645303.0 Massachusetts 10555.0 629.6\n",
"1056 MI total 2012 9882519.0 Michigan 96810.0 102.1\n",
"1151 MN total 2012 5379646.0 Minnesota 86943.0 61.9\n",
"1152 MS total 2012 2986450.0 Mississippi 48434.0 61.7\n",
"1247 MO total 2012 6024522.0 Missouri 69709.0 86.4\n",
"1248 MT total 2012 1005494.0 Montana 147046.0 6.8\n",
"1343 NE total 2012 1855350.0 Nebraska 77358.0 24.0\n",
"1344 NV total 2012 2754354.0 Nevada 110567.0 24.9\n",
"1439 NH total 2012 1321617.0 New Hampshire 9351.0 141.3\n",
"1440 NJ total 2012 8867749.0 New Jersey 8722.0 1016.7\n",
"1535 NM total 2012 2083540.0 New Mexico 121593.0 17.1\n",
"1536 NY total 2012 19576125.0 New York 54475.0 359.4\n",
"1631 NC total 2012 9748364.0 North Carolina 53821.0 181.1\n",
"1632 ND total 2012 701345.0 North Dakota 70704.0 9.9\n",
"1727 OH total 2012 11553031.0 Ohio 44828.0 257.7\n",
"1728 OK total 2012 3815780.0 Oklahoma 69903.0 54.6\n",
"1823 OR total 2012 3899801.0 Oregon 98386.0 39.6\n",
"1824 PA total 2012 12764475.0 Pennsylvania 46058.0 277.1\n",
"1919 RI total 2012 1050304.0 Rhode Island 1545.0 679.8\n",
"1920 SC total 2012 4723417.0 South Carolina 32007.0 147.6\n",
"2015 SD total 2012 834047.0 South Dakota 77121.0 10.8\n",
"2016 TN total 2012 6454914.0 Tennessee 42146.0 153.2\n",
"2111 TX total 2012 26060796.0 Texas 268601.0 97.0\n",
"2112 UT total 2012 2854871.0 Utah 84904.0 33.6\n",
"2207 VT total 2012 625953.0 Vermont 9615.0 65.1\n",
"2208 VA total 2012 8186628.0 Virginia 42769.0 191.4\n",
"2303 WA total 2012 6895318.0 Washington 71303.0 96.7\n",
"2304 WV total 2012 1856680.0 West Virginia 24231.0 76.6\n",
"2399 WI total 2012 5724554.0 Wisconsin 65503.0 87.4\n",
"2400 WY total 2012 576626.0 Wyoming 97818.0 5.9\n",
"2475 PR total 2012 3651545.0 Puerto Rico 3515.0 1038.8\n",
"2523 USA total 2012 313873685.0 United State 3790399.0 82.8"
]
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 查找2012年美国各州的全民人口数据\n",
"\n",
"# pandas非常强大的,可以像查询数据库一样进行数据查询\n",
"\n",
"pop5 = pop4.query(\"year == 2012 and ages == 'total'\")\n",
"pop5"
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {},
"outputs": [],
"source": [
"pop5.set_index(keys = 'state/region',inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" ages | \n",
" year | \n",
" population | \n",
" state | \n",
" area (sq. mi) | \n",
" pop_density | \n",
"
\n",
" \n",
" state/region | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" AK | \n",
" total | \n",
" 2012 | \n",
" 730307.0 | \n",
" Alaska | \n",
" 656425.0 | \n",
" 1.1 | \n",
"
\n",
" \n",
" WY | \n",
" total | \n",
" 2012 | \n",
" 576626.0 | \n",
" Wyoming | \n",
" 97818.0 | \n",
" 5.9 | \n",
"
\n",
" \n",
" MT | \n",
" total | \n",
" 2012 | \n",
" 1005494.0 | \n",
" Montana | \n",
" 147046.0 | \n",
" 6.8 | \n",
"
\n",
" \n",
" ND | \n",
" total | \n",
" 2012 | \n",
" 701345.0 | \n",
" North Dakota | \n",
" 70704.0 | \n",
" 9.9 | \n",
"
\n",
" \n",
" SD | \n",
" total | \n",
" 2012 | \n",
" 834047.0 | \n",
" South Dakota | \n",
" 77121.0 | \n",
" 10.8 | \n",
"
\n",
" \n",
" NM | \n",
" total | \n",
" 2012 | \n",
" 2083540.0 | \n",
" New Mexico | \n",
" 121593.0 | \n",
" 17.1 | \n",
"
\n",
" \n",
" ID | \n",
" total | \n",
" 2012 | \n",
" 1595590.0 | \n",
" Idaho | \n",
" 83574.0 | \n",
" 19.1 | \n",
"
\n",
" \n",
" NE | \n",
" total | \n",
" 2012 | \n",
" 1855350.0 | \n",
" Nebraska | \n",
" 77358.0 | \n",
" 24.0 | \n",
"
\n",
" \n",
" NV | \n",
" total | \n",
" 2012 | \n",
" 2754354.0 | \n",
" Nevada | \n",
" 110567.0 | \n",
" 24.9 | \n",
"
\n",
" \n",
" UT | \n",
" total | \n",
" 2012 | \n",
" 2854871.0 | \n",
" Utah | \n",
" 84904.0 | \n",
" 33.6 | \n",
"
\n",
" \n",
" KS | \n",
" total | \n",
" 2012 | \n",
" 2885398.0 | \n",
" Kansas | \n",
" 82282.0 | \n",
" 35.1 | \n",
"
\n",
" \n",
" ME | \n",
" total | \n",
" 2012 | \n",
" 1328501.0 | \n",
" Maine | \n",
" 35387.0 | \n",
" 37.5 | \n",
"
\n",
" \n",
" OR | \n",
" total | \n",
" 2012 | \n",
" 3899801.0 | \n",
" Oregon | \n",
" 98386.0 | \n",
" 39.6 | \n",
"
\n",
" \n",
" CO | \n",
" total | \n",
" 2012 | \n",
" 5189458.0 | \n",
" Colorado | \n",
" 104100.0 | \n",
" 49.9 | \n",
"
\n",
" \n",
" IA | \n",
" total | \n",
" 2012 | \n",
" 3075039.0 | \n",
" Iowa | \n",
" 56276.0 | \n",
" 54.6 | \n",
"
\n",
" \n",
" OK | \n",
" total | \n",
" 2012 | \n",
" 3815780.0 | \n",
" Oklahoma | \n",
" 69903.0 | \n",
" 54.6 | \n",
"
\n",
" \n",
" AR | \n",
" total | \n",
" 2012 | \n",
" 2949828.0 | \n",
" Arkansas | \n",
" 53182.0 | \n",
" 55.5 | \n",
"
\n",
" \n",
" AZ | \n",
" total | \n",
" 2012 | \n",
" 6551149.0 | \n",
" Arizona | \n",
" 114006.0 | \n",
" 57.5 | \n",
"
\n",
" \n",
" MS | \n",
" total | \n",
" 2012 | \n",
" 2986450.0 | \n",
" Mississippi | \n",
" 48434.0 | \n",
" 61.7 | \n",
"
\n",
" \n",
" MN | \n",
" total | \n",
" 2012 | \n",
" 5379646.0 | \n",
" Minnesota | \n",
" 86943.0 | \n",
" 61.9 | \n",
"
\n",
" \n",
" VT | \n",
" total | \n",
" 2012 | \n",
" 625953.0 | \n",
" Vermont | \n",
" 9615.0 | \n",
" 65.1 | \n",
"
\n",
" \n",
" WV | \n",
" total | \n",
" 2012 | \n",
" 1856680.0 | \n",
" West Virginia | \n",
" 24231.0 | \n",
" 76.6 | \n",
"
\n",
" \n",
" USA | \n",
" total | \n",
" 2012 | \n",
" 313873685.0 | \n",
" United State | \n",
" 3790399.0 | \n",
" 82.8 | \n",
"
\n",
" \n",
" MO | \n",
" total | \n",
" 2012 | \n",
" 6024522.0 | \n",
" Missouri | \n",
" 69709.0 | \n",
" 86.4 | \n",
"
\n",
" \n",
" WI | \n",
" total | \n",
" 2012 | \n",
" 5724554.0 | \n",
" Wisconsin | \n",
" 65503.0 | \n",
" 87.4 | \n",
"
\n",
" \n",
" LA | \n",
" total | \n",
" 2012 | \n",
" 4602134.0 | \n",
" Louisiana | \n",
" 51843.0 | \n",
" 88.8 | \n",
"
\n",
" \n",
" AL | \n",
" total | \n",
" 2012 | \n",
" 4817528.0 | \n",
" Alabama | \n",
" 52423.0 | \n",
" 91.9 | \n",
"
\n",
" \n",
" WA | \n",
" total | \n",
" 2012 | \n",
" 6895318.0 | \n",
" Washington | \n",
" 71303.0 | \n",
" 96.7 | \n",
"
\n",
" \n",
" TX | \n",
" total | \n",
" 2012 | \n",
" 26060796.0 | \n",
" Texas | \n",
" 268601.0 | \n",
" 97.0 | \n",
"
\n",
" \n",
" MI | \n",
" total | \n",
" 2012 | \n",
" 9882519.0 | \n",
" Michigan | \n",
" 96810.0 | \n",
" 102.1 | \n",
"
\n",
" \n",
" KY | \n",
" total | \n",
" 2012 | \n",
" 4379730.0 | \n",
" Kentucky | \n",
" 40411.0 | \n",
" 108.4 | \n",
"
\n",
" \n",
" HI | \n",
" total | \n",
" 2012 | \n",
" 1390090.0 | \n",
" Hawaii | \n",
" 10932.0 | \n",
" 127.2 | \n",
"
\n",
" \n",
" NH | \n",
" total | \n",
" 2012 | \n",
" 1321617.0 | \n",
" New Hampshire | \n",
" 9351.0 | \n",
" 141.3 | \n",
"
\n",
" \n",
" SC | \n",
" total | \n",
" 2012 | \n",
" 4723417.0 | \n",
" South Carolina | \n",
" 32007.0 | \n",
" 147.6 | \n",
"
\n",
" \n",
" TN | \n",
" total | \n",
" 2012 | \n",
" 6454914.0 | \n",
" Tennessee | \n",
" 42146.0 | \n",
" 153.2 | \n",
"
\n",
" \n",
" GA | \n",
" total | \n",
" 2012 | \n",
" 9915646.0 | \n",
" Georgia | \n",
" 59441.0 | \n",
" 166.8 | \n",
"
\n",
" \n",
" IN | \n",
" total | \n",
" 2012 | \n",
" 6537782.0 | \n",
" Indiana | \n",
" 36420.0 | \n",
" 179.5 | \n",
"
\n",
" \n",
" NC | \n",
" total | \n",
" 2012 | \n",
" 9748364.0 | \n",
" North Carolina | \n",
" 53821.0 | \n",
" 181.1 | \n",
"
\n",
" \n",
" VA | \n",
" total | \n",
" 2012 | \n",
" 8186628.0 | \n",
" Virginia | \n",
" 42769.0 | \n",
" 191.4 | \n",
"
\n",
" \n",
" IL | \n",
" total | \n",
" 2012 | \n",
" 12868192.0 | \n",
" Illinois | \n",
" 57918.0 | \n",
" 222.2 | \n",
"
\n",
" \n",
" CA | \n",
" total | \n",
" 2012 | \n",
" 37999878.0 | \n",
" California | \n",
" 163707.0 | \n",
" 232.1 | \n",
"
\n",
" \n",
" OH | \n",
" total | \n",
" 2012 | \n",
" 11553031.0 | \n",
" Ohio | \n",
" 44828.0 | \n",
" 257.7 | \n",
"
\n",
" \n",
" PA | \n",
" total | \n",
" 2012 | \n",
" 12764475.0 | \n",
" Pennsylvania | \n",
" 46058.0 | \n",
" 277.1 | \n",
"
\n",
" \n",
" FL | \n",
" total | \n",
" 2012 | \n",
" 19320749.0 | \n",
" Florida | \n",
" 65758.0 | \n",
" 293.8 | \n",
"
\n",
" \n",
" NY | \n",
" total | \n",
" 2012 | \n",
" 19576125.0 | \n",
" New York | \n",
" 54475.0 | \n",
" 359.4 | \n",
"
\n",
" \n",
" DE | \n",
" total | \n",
" 2012 | \n",
" 917053.0 | \n",
" Delaware | \n",
" 1954.0 | \n",
" 469.3 | \n",
"
\n",
" \n",
" MD | \n",
" total | \n",
" 2012 | \n",
" 5884868.0 | \n",
" Maryland | \n",
" 12407.0 | \n",
" 474.3 | \n",
"
\n",
" \n",
" MA | \n",
" total | \n",
" 2012 | \n",
" 6645303.0 | \n",
" Massachusetts | \n",
" 10555.0 | \n",
" 629.6 | \n",
"
\n",
" \n",
" CT | \n",
" total | \n",
" 2012 | \n",
" 3591765.0 | \n",
" Connecticut | \n",
" 5544.0 | \n",
" 647.9 | \n",
"
\n",
" \n",
" RI | \n",
" total | \n",
" 2012 | \n",
" 1050304.0 | \n",
" Rhode Island | \n",
" 1545.0 | \n",
" 679.8 | \n",
"
\n",
" \n",
" NJ | \n",
" total | \n",
" 2012 | \n",
" 8867749.0 | \n",
" New Jersey | \n",
" 8722.0 | \n",
" 1016.7 | \n",
"
\n",
" \n",
" PR | \n",
" total | \n",
" 2012 | \n",
" 3651545.0 | \n",
" Puerto Rico | \n",
" 3515.0 | \n",
" 1038.8 | \n",
"
\n",
" \n",
" DC | \n",
" total | \n",
" 2012 | \n",
" 633427.0 | \n",
" District of Columbia | \n",
" 68.0 | \n",
" 9315.1 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" ages year population state area (sq. mi) pop_density\n",
"state/region \n",
"AK total 2012 730307.0 Alaska 656425.0 1.1\n",
"WY total 2012 576626.0 Wyoming 97818.0 5.9\n",
"MT total 2012 1005494.0 Montana 147046.0 6.8\n",
"ND total 2012 701345.0 North Dakota 70704.0 9.9\n",
"SD total 2012 834047.0 South Dakota 77121.0 10.8\n",
"NM total 2012 2083540.0 New Mexico 121593.0 17.1\n",
"ID total 2012 1595590.0 Idaho 83574.0 19.1\n",
"NE total 2012 1855350.0 Nebraska 77358.0 24.0\n",
"NV total 2012 2754354.0 Nevada 110567.0 24.9\n",
"UT total 2012 2854871.0 Utah 84904.0 33.6\n",
"KS total 2012 2885398.0 Kansas 82282.0 35.1\n",
"ME total 2012 1328501.0 Maine 35387.0 37.5\n",
"OR total 2012 3899801.0 Oregon 98386.0 39.6\n",
"CO total 2012 5189458.0 Colorado 104100.0 49.9\n",
"IA total 2012 3075039.0 Iowa 56276.0 54.6\n",
"OK total 2012 3815780.0 Oklahoma 69903.0 54.6\n",
"AR total 2012 2949828.0 Arkansas 53182.0 55.5\n",
"AZ total 2012 6551149.0 Arizona 114006.0 57.5\n",
"MS total 2012 2986450.0 Mississippi 48434.0 61.7\n",
"MN total 2012 5379646.0 Minnesota 86943.0 61.9\n",
"VT total 2012 625953.0 Vermont 9615.0 65.1\n",
"WV total 2012 1856680.0 West Virginia 24231.0 76.6\n",
"USA total 2012 313873685.0 United State 3790399.0 82.8\n",
"MO total 2012 6024522.0 Missouri 69709.0 86.4\n",
"WI total 2012 5724554.0 Wisconsin 65503.0 87.4\n",
"LA total 2012 4602134.0 Louisiana 51843.0 88.8\n",
"AL total 2012 4817528.0 Alabama 52423.0 91.9\n",
"WA total 2012 6895318.0 Washington 71303.0 96.7\n",
"TX total 2012 26060796.0 Texas 268601.0 97.0\n",
"MI total 2012 9882519.0 Michigan 96810.0 102.1\n",
"KY total 2012 4379730.0 Kentucky 40411.0 108.4\n",
"HI total 2012 1390090.0 Hawaii 10932.0 127.2\n",
"NH total 2012 1321617.0 New Hampshire 9351.0 141.3\n",
"SC total 2012 4723417.0 South Carolina 32007.0 147.6\n",
"TN total 2012 6454914.0 Tennessee 42146.0 153.2\n",
"GA total 2012 9915646.0 Georgia 59441.0 166.8\n",
"IN total 2012 6537782.0 Indiana 36420.0 179.5\n",
"NC total 2012 9748364.0 North Carolina 53821.0 181.1\n",
"VA total 2012 8186628.0 Virginia 42769.0 191.4\n",
"IL total 2012 12868192.0 Illinois 57918.0 222.2\n",
"CA total 2012 37999878.0 California 163707.0 232.1\n",
"OH total 2012 11553031.0 Ohio 44828.0 257.7\n",
"PA total 2012 12764475.0 Pennsylvania 46058.0 277.1\n",
"FL total 2012 19320749.0 Florida 65758.0 293.8\n",
"NY total 2012 19576125.0 New York 54475.0 359.4\n",
"DE total 2012 917053.0 Delaware 1954.0 469.3\n",
"MD total 2012 5884868.0 Maryland 12407.0 474.3\n",
"MA total 2012 6645303.0 Massachusetts 10555.0 629.6\n",
"CT total 2012 3591765.0 Connecticut 5544.0 647.9\n",
"RI total 2012 1050304.0 Rhode Island 1545.0 679.8\n",
"NJ total 2012 8867749.0 New Jersey 8722.0 1016.7\n",
"PR total 2012 3651545.0 Puerto Rico 3515.0 1038.8\n",
"DC total 2012 633427.0 District of Columbia 68.0 9315.1"
]
},
"execution_count": 80,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop5.sort_values(by = 'pop_density')"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" ages | \n",
" year | \n",
" population | \n",
" state | \n",
" area (sq. mi) | \n",
" pop_density | \n",
"
\n",
" \n",
" state/region | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" DC | \n",
" total | \n",
" 2012 | \n",
" 633427.0 | \n",
" District of Columbia | \n",
" 68.0 | \n",
" 9315.1 | \n",
"
\n",
" \n",
" PR | \n",
" total | \n",
" 2012 | \n",
" 3651545.0 | \n",
" Puerto Rico | \n",
" 3515.0 | \n",
" 1038.8 | \n",
"
\n",
" \n",
" NJ | \n",
" total | \n",
" 2012 | \n",
" 8867749.0 | \n",
" New Jersey | \n",
" 8722.0 | \n",
" 1016.7 | \n",
"
\n",
" \n",
" RI | \n",
" total | \n",
" 2012 | \n",
" 1050304.0 | \n",
" Rhode Island | \n",
" 1545.0 | \n",
" 679.8 | \n",
"
\n",
" \n",
" CT | \n",
" total | \n",
" 2012 | \n",
" 3591765.0 | \n",
" Connecticut | \n",
" 5544.0 | \n",
" 647.9 | \n",
"
\n",
" \n",
" MA | \n",
" total | \n",
" 2012 | \n",
" 6645303.0 | \n",
" Massachusetts | \n",
" 10555.0 | \n",
" 629.6 | \n",
"
\n",
" \n",
" MD | \n",
" total | \n",
" 2012 | \n",
" 5884868.0 | \n",
" Maryland | \n",
" 12407.0 | \n",
" 474.3 | \n",
"
\n",
" \n",
" DE | \n",
" total | \n",
" 2012 | \n",
" 917053.0 | \n",
" Delaware | \n",
" 1954.0 | \n",
" 469.3 | \n",
"
\n",
" \n",
" NY | \n",
" total | \n",
" 2012 | \n",
" 19576125.0 | \n",
" New York | \n",
" 54475.0 | \n",
" 359.4 | \n",
"
\n",
" \n",
" FL | \n",
" total | \n",
" 2012 | \n",
" 19320749.0 | \n",
" Florida | \n",
" 65758.0 | \n",
" 293.8 | \n",
"
\n",
" \n",
" PA | \n",
" total | \n",
" 2012 | \n",
" 12764475.0 | \n",
" Pennsylvania | \n",
" 46058.0 | \n",
" 277.1 | \n",
"
\n",
" \n",
" OH | \n",
" total | \n",
" 2012 | \n",
" 11553031.0 | \n",
" Ohio | \n",
" 44828.0 | \n",
" 257.7 | \n",
"
\n",
" \n",
" CA | \n",
" total | \n",
" 2012 | \n",
" 37999878.0 | \n",
" California | \n",
" 163707.0 | \n",
" 232.1 | \n",
"
\n",
" \n",
" IL | \n",
" total | \n",
" 2012 | \n",
" 12868192.0 | \n",
" Illinois | \n",
" 57918.0 | \n",
" 222.2 | \n",
"
\n",
" \n",
" VA | \n",
" total | \n",
" 2012 | \n",
" 8186628.0 | \n",
" Virginia | \n",
" 42769.0 | \n",
" 191.4 | \n",
"
\n",
" \n",
" NC | \n",
" total | \n",
" 2012 | \n",
" 9748364.0 | \n",
" North Carolina | \n",
" 53821.0 | \n",
" 181.1 | \n",
"
\n",
" \n",
" IN | \n",
" total | \n",
" 2012 | \n",
" 6537782.0 | \n",
" Indiana | \n",
" 36420.0 | \n",
" 179.5 | \n",
"
\n",
" \n",
" GA | \n",
" total | \n",
" 2012 | \n",
" 9915646.0 | \n",
" Georgia | \n",
" 59441.0 | \n",
" 166.8 | \n",
"
\n",
" \n",
" TN | \n",
" total | \n",
" 2012 | \n",
" 6454914.0 | \n",
" Tennessee | \n",
" 42146.0 | \n",
" 153.2 | \n",
"
\n",
" \n",
" SC | \n",
" total | \n",
" 2012 | \n",
" 4723417.0 | \n",
" South Carolina | \n",
" 32007.0 | \n",
" 147.6 | \n",
"
\n",
" \n",
" NH | \n",
" total | \n",
" 2012 | \n",
" 1321617.0 | \n",
" New Hampshire | \n",
" 9351.0 | \n",
" 141.3 | \n",
"
\n",
" \n",
" HI | \n",
" total | \n",
" 2012 | \n",
" 1390090.0 | \n",
" Hawaii | \n",
" 10932.0 | \n",
" 127.2 | \n",
"
\n",
" \n",
" KY | \n",
" total | \n",
" 2012 | \n",
" 4379730.0 | \n",
" Kentucky | \n",
" 40411.0 | \n",
" 108.4 | \n",
"
\n",
" \n",
" MI | \n",
" total | \n",
" 2012 | \n",
" 9882519.0 | \n",
" Michigan | \n",
" 96810.0 | \n",
" 102.1 | \n",
"
\n",
" \n",
" TX | \n",
" total | \n",
" 2012 | \n",
" 26060796.0 | \n",
" Texas | \n",
" 268601.0 | \n",
" 97.0 | \n",
"
\n",
" \n",
" WA | \n",
" total | \n",
" 2012 | \n",
" 6895318.0 | \n",
" Washington | \n",
" 71303.0 | \n",
" 96.7 | \n",
"
\n",
" \n",
" AL | \n",
" total | \n",
" 2012 | \n",
" 4817528.0 | \n",
" Alabama | \n",
" 52423.0 | \n",
" 91.9 | \n",
"
\n",
" \n",
" LA | \n",
" total | \n",
" 2012 | \n",
" 4602134.0 | \n",
" Louisiana | \n",
" 51843.0 | \n",
" 88.8 | \n",
"
\n",
" \n",
" WI | \n",
" total | \n",
" 2012 | \n",
" 5724554.0 | \n",
" Wisconsin | \n",
" 65503.0 | \n",
" 87.4 | \n",
"
\n",
" \n",
" MO | \n",
" total | \n",
" 2012 | \n",
" 6024522.0 | \n",
" Missouri | \n",
" 69709.0 | \n",
" 86.4 | \n",
"
\n",
" \n",
" USA | \n",
" total | \n",
" 2012 | \n",
" 313873685.0 | \n",
" United State | \n",
" 3790399.0 | \n",
" 82.8 | \n",
"
\n",
" \n",
" WV | \n",
" total | \n",
" 2012 | \n",
" 1856680.0 | \n",
" West Virginia | \n",
" 24231.0 | \n",
" 76.6 | \n",
"
\n",
" \n",
" VT | \n",
" total | \n",
" 2012 | \n",
" 625953.0 | \n",
" Vermont | \n",
" 9615.0 | \n",
" 65.1 | \n",
"
\n",
" \n",
" MN | \n",
" total | \n",
" 2012 | \n",
" 5379646.0 | \n",
" Minnesota | \n",
" 86943.0 | \n",
" 61.9 | \n",
"
\n",
" \n",
" MS | \n",
" total | \n",
" 2012 | \n",
" 2986450.0 | \n",
" Mississippi | \n",
" 48434.0 | \n",
" 61.7 | \n",
"
\n",
" \n",
" AZ | \n",
" total | \n",
" 2012 | \n",
" 6551149.0 | \n",
" Arizona | \n",
" 114006.0 | \n",
" 57.5 | \n",
"
\n",
" \n",
" AR | \n",
" total | \n",
" 2012 | \n",
" 2949828.0 | \n",
" Arkansas | \n",
" 53182.0 | \n",
" 55.5 | \n",
"
\n",
" \n",
" OK | \n",
" total | \n",
" 2012 | \n",
" 3815780.0 | \n",
" Oklahoma | \n",
" 69903.0 | \n",
" 54.6 | \n",
"
\n",
" \n",
" IA | \n",
" total | \n",
" 2012 | \n",
" 3075039.0 | \n",
" Iowa | \n",
" 56276.0 | \n",
" 54.6 | \n",
"
\n",
" \n",
" CO | \n",
" total | \n",
" 2012 | \n",
" 5189458.0 | \n",
" Colorado | \n",
" 104100.0 | \n",
" 49.9 | \n",
"
\n",
" \n",
" OR | \n",
" total | \n",
" 2012 | \n",
" 3899801.0 | \n",
" Oregon | \n",
" 98386.0 | \n",
" 39.6 | \n",
"
\n",
" \n",
" ME | \n",
" total | \n",
" 2012 | \n",
" 1328501.0 | \n",
" Maine | \n",
" 35387.0 | \n",
" 37.5 | \n",
"
\n",
" \n",
" KS | \n",
" total | \n",
" 2012 | \n",
" 2885398.0 | \n",
" Kansas | \n",
" 82282.0 | \n",
" 35.1 | \n",
"
\n",
" \n",
" UT | \n",
" total | \n",
" 2012 | \n",
" 2854871.0 | \n",
" Utah | \n",
" 84904.0 | \n",
" 33.6 | \n",
"
\n",
" \n",
" NV | \n",
" total | \n",
" 2012 | \n",
" 2754354.0 | \n",
" Nevada | \n",
" 110567.0 | \n",
" 24.9 | \n",
"
\n",
" \n",
" NE | \n",
" total | \n",
" 2012 | \n",
" 1855350.0 | \n",
" Nebraska | \n",
" 77358.0 | \n",
" 24.0 | \n",
"
\n",
" \n",
" ID | \n",
" total | \n",
" 2012 | \n",
" 1595590.0 | \n",
" Idaho | \n",
" 83574.0 | \n",
" 19.1 | \n",
"
\n",
" \n",
" NM | \n",
" total | \n",
" 2012 | \n",
" 2083540.0 | \n",
" New Mexico | \n",
" 121593.0 | \n",
" 17.1 | \n",
"
\n",
" \n",
" SD | \n",
" total | \n",
" 2012 | \n",
" 834047.0 | \n",
" South Dakota | \n",
" 77121.0 | \n",
" 10.8 | \n",
"
\n",
" \n",
" ND | \n",
" total | \n",
" 2012 | \n",
" 701345.0 | \n",
" North Dakota | \n",
" 70704.0 | \n",
" 9.9 | \n",
"
\n",
" \n",
" MT | \n",
" total | \n",
" 2012 | \n",
" 1005494.0 | \n",
" Montana | \n",
" 147046.0 | \n",
" 6.8 | \n",
"
\n",
" \n",
" WY | \n",
" total | \n",
" 2012 | \n",
" 576626.0 | \n",
" Wyoming | \n",
" 97818.0 | \n",
" 5.9 | \n",
"
\n",
" \n",
" AK | \n",
" total | \n",
" 2012 | \n",
" 730307.0 | \n",
" Alaska | \n",
" 656425.0 | \n",
" 1.1 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" ages year population state area (sq. mi) pop_density\n",
"state/region \n",
"DC total 2012 633427.0 District of Columbia 68.0 9315.1\n",
"PR total 2012 3651545.0 Puerto Rico 3515.0 1038.8\n",
"NJ total 2012 8867749.0 New Jersey 8722.0 1016.7\n",
"RI total 2012 1050304.0 Rhode Island 1545.0 679.8\n",
"CT total 2012 3591765.0 Connecticut 5544.0 647.9\n",
"MA total 2012 6645303.0 Massachusetts 10555.0 629.6\n",
"MD total 2012 5884868.0 Maryland 12407.0 474.3\n",
"DE total 2012 917053.0 Delaware 1954.0 469.3\n",
"NY total 2012 19576125.0 New York 54475.0 359.4\n",
"FL total 2012 19320749.0 Florida 65758.0 293.8\n",
"PA total 2012 12764475.0 Pennsylvania 46058.0 277.1\n",
"OH total 2012 11553031.0 Ohio 44828.0 257.7\n",
"CA total 2012 37999878.0 California 163707.0 232.1\n",
"IL total 2012 12868192.0 Illinois 57918.0 222.2\n",
"VA total 2012 8186628.0 Virginia 42769.0 191.4\n",
"NC total 2012 9748364.0 North Carolina 53821.0 181.1\n",
"IN total 2012 6537782.0 Indiana 36420.0 179.5\n",
"GA total 2012 9915646.0 Georgia 59441.0 166.8\n",
"TN total 2012 6454914.0 Tennessee 42146.0 153.2\n",
"SC total 2012 4723417.0 South Carolina 32007.0 147.6\n",
"NH total 2012 1321617.0 New Hampshire 9351.0 141.3\n",
"HI total 2012 1390090.0 Hawaii 10932.0 127.2\n",
"KY total 2012 4379730.0 Kentucky 40411.0 108.4\n",
"MI total 2012 9882519.0 Michigan 96810.0 102.1\n",
"TX total 2012 26060796.0 Texas 268601.0 97.0\n",
"WA total 2012 6895318.0 Washington 71303.0 96.7\n",
"AL total 2012 4817528.0 Alabama 52423.0 91.9\n",
"LA total 2012 4602134.0 Louisiana 51843.0 88.8\n",
"WI total 2012 5724554.0 Wisconsin 65503.0 87.4\n",
"MO total 2012 6024522.0 Missouri 69709.0 86.4\n",
"USA total 2012 313873685.0 United State 3790399.0 82.8\n",
"WV total 2012 1856680.0 West Virginia 24231.0 76.6\n",
"VT total 2012 625953.0 Vermont 9615.0 65.1\n",
"MN total 2012 5379646.0 Minnesota 86943.0 61.9\n",
"MS total 2012 2986450.0 Mississippi 48434.0 61.7\n",
"AZ total 2012 6551149.0 Arizona 114006.0 57.5\n",
"AR total 2012 2949828.0 Arkansas 53182.0 55.5\n",
"OK total 2012 3815780.0 Oklahoma 69903.0 54.6\n",
"IA total 2012 3075039.0 Iowa 56276.0 54.6\n",
"CO total 2012 5189458.0 Colorado 104100.0 49.9\n",
"OR total 2012 3899801.0 Oregon 98386.0 39.6\n",
"ME total 2012 1328501.0 Maine 35387.0 37.5\n",
"KS total 2012 2885398.0 Kansas 82282.0 35.1\n",
"UT total 2012 2854871.0 Utah 84904.0 33.6\n",
"NV total 2012 2754354.0 Nevada 110567.0 24.9\n",
"NE total 2012 1855350.0 Nebraska 77358.0 24.0\n",
"ID total 2012 1595590.0 Idaho 83574.0 19.1\n",
"NM total 2012 2083540.0 New Mexico 121593.0 17.1\n",
"SD total 2012 834047.0 South Dakota 77121.0 10.8\n",
"ND total 2012 701345.0 North Dakota 70704.0 9.9\n",
"MT total 2012 1005494.0 Montana 147046.0 6.8\n",
"WY total 2012 576626.0 Wyoming 97818.0 5.9\n",
"AK total 2012 730307.0 Alaska 656425.0 1.1"
]
},
"execution_count": 81,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pop5.sort_values(by='pop_density',ascending=False)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}