pandas 案例分析:美国各州人口数据分析

案例分析:美国各州人口数据分析

首先导入文件,并查看数据样本

In [54]:
abbr = pd.read_csv("./usapop/state-abbrevs.csv")
abbr.head()
Out[54]:
  state abbreviation
0 Alabama AL
1 Alaska AK
2 Arizona AZ
3 Arkansas AR
4 California CA
In [55]:
areas = pd.read_csv("./usapop/state-areas.csv")
areas.head()
Out[55]:
  state area (sq. mi)
0 Alabama 52423
1 Alaska 656425
2 Arizona 114006
3 Arkansas 53182
4 California 163707
In [56]:
pop = pd.read_csv("./usapop/state-population.csv")
pop.head()
Out[56]:
  state/region ages year population
0 AL under18 2012 1117489.0
1 AL total 2012 4817528.0
2 AL under18 2010 1130966.0
3 AL total 2010 4785570.0
4 AL under18 2011 1125763.0

合并pop与abbrevs两个DataFrame,分别依据state/region列和abbreviation列来合并。

为了保留所有信息,使用外合并。

In [57]:
pop2 = pop.merge(abbr,left_on="state/region",right_on="abbreviation",how="outer")
# 用外连接(或者左链接)
pop2
Out[57]:
  state/region ages year population state abbreviation
0 AL under18 2012 1117489.0 Alabama AL
1 AL total 2012 4817528.0 Alabama AL
2 AL under18 2010 1130966.0 Alabama AL
3 AL total 2010 4785570.0 Alabama AL
4 AL under18 2011 1125763.0 Alabama AL
5 AL total 2011 4801627.0 Alabama AL
6 AL total 2009 4757938.0 Alabama AL
7 AL under18 2009 1134192.0 Alabama AL
8 AL under18 2013 1111481.0 Alabama AL
9 AL total 2013 4833722.0 Alabama AL
10 AL total 2007 4672840.0 Alabama AL
11 AL under18 2007 1132296.0 Alabama AL
12 AL total 2008 4718206.0 Alabama AL
13 AL under18 2008 1134927.0 Alabama AL
14 AL total 2005 4569805.0 Alabama AL
15 AL under18 2005 1117229.0 Alabama AL
16 AL total 2006 4628981.0 Alabama AL
17 AL under18 2006 1126798.0 Alabama AL
18 AL total 2004 4530729.0 Alabama AL
19 AL under18 2004 1113662.0 Alabama AL
20 AL total 2003 4503491.0 Alabama AL
21 AL under18 2003 1113083.0 Alabama AL
22 AL total 2001 4467634.0 Alabama AL
23 AL under18 2001 1120409.0 Alabama AL
24 AL total 2002 4480089.0 Alabama AL
25 AL under18 2002 1116590.0 Alabama AL
26 AL under18 1999 1121287.0 Alabama AL
27 AL total 1999 4430141.0 Alabama AL
28 AL total 2000 4452173.0 Alabama AL
29 AL under18 2000 1122273.0 Alabama AL
... ... ... ... ... ... ...
2514 USA under18 1999 71946051.0 NaN NaN
2515 USA total 2000 282162411.0 NaN NaN
2516 USA under18 2000 72376189.0 NaN NaN
2517 USA total 1999 279040181.0 NaN NaN
2518 USA total 2001 284968955.0 NaN NaN
2519 USA under18 2001 72671175.0 NaN NaN
2520 USA total 2002 287625193.0 NaN NaN
2521 USA under18 2002 72936457.0 NaN NaN
2522 USA total 2003 290107933.0 NaN NaN
2523 USA under18 2003 73100758.0 NaN NaN
2524 USA total 2004 292805298.0 NaN NaN
2525 USA under18 2004 73297735.0 NaN NaN
2526 USA total 2005 295516599.0 NaN NaN
2527 USA under18 2005 73523669.0 NaN NaN
2528 USA total 2006 298379912.0 NaN NaN
2529 USA under18 2006 73757714.0 NaN NaN
2530 USA total 2007 301231207.0 NaN NaN
2531 USA under18 2007 74019405.0 NaN NaN
2532 USA total 2008 304093966.0 NaN NaN
2533 USA under18 2008 74104602.0 NaN NaN
2534 USA under18 2013 73585872.0 NaN NaN
2535 USA total 2013 316128839.0 NaN NaN
2536 USA total 2009 306771529.0 NaN NaN
2537 USA under18 2009 74134167.0 NaN NaN
2538 USA under18 2010 74119556.0 NaN NaN
2539 USA total 2010 309326295.0 NaN NaN
2540 USA under18 2011 73902222.0 NaN NaN
2541 USA total 2011 311582564.0 NaN NaN
2542 USA under18 2012 73708179.0 NaN NaN
2543 USA total 2012 313873685.0 NaN NaN

2544 rows × 6 columns

去除abbreviation的那一列(axis=1)

In [58]:
pop2.drop("abbreviation",axis=1,inplace=True)
# 默认axis=0删除行
# inplace是否在原表上删除,默认是False不在原表上删除
In [59]:
pop2.head()
Out[59]:
  state/region ages year population state
0 AL under18 2012 1117489.0 Alabama
1 AL total 2012 4817528.0 Alabama
2 AL under18 2010 1130966.0 Alabama
3 AL total 2010 4785570.0 Alabama
4 AL under18 2011 1125763.0 Alabama

查看存在缺失数据的列。

使用.isnull().any(),只有某一列存在一个缺失数据,就会显示True。

In [60]:
cond = pop2.isnull().any(axis=1)
cond
Out[60]:
0       False
1       False
2       False
3       False
4       False
5       False
6       False
7       False
8       False
9       False
10      False
11      False
12      False
13      False
14      False
15      False
16      False
17      False
18      False
19      False
20      False
21      False
22      False
23      False
24      False
25      False
26      False
27      False
28      False
29      False
        ...  
2514     True
2515     True
2516     True
2517     True
2518     True
2519     True
2520     True
2521     True
2522     True
2523     True
2524     True
2525     True
2526     True
2527     True
2528     True
2529     True
2530     True
2531     True
2532     True
2533     True
2534     True
2535     True
2536     True
2537     True
2538     True
2539     True
2540     True
2541     True
2542     True
2543     True
Length: 2544, dtype: bool
In [61]:
pop2[cond]
Out[61]:
  state/region ages year population state
2448 PR under18 1990 NaN NaN
2449 PR total 1990 NaN NaN
2450 PR total 1991 NaN NaN
2451 PR under18 1991 NaN NaN
2452 PR total 1993 NaN NaN
2453 PR under18 1993 NaN NaN
2454 PR under18 1992 NaN NaN
2455 PR total 1992 NaN NaN
2456 PR under18 1994 NaN NaN
2457 PR total 1994 NaN NaN
2458 PR total 1995 NaN NaN
2459 PR under18 1995 NaN NaN
2460 PR under18 1996 NaN NaN
2461 PR total 1996 NaN NaN
2462 PR under18 1998 NaN NaN
2463 PR total 1998 NaN NaN
2464 PR total 1997 NaN NaN
2465 PR under18 1997 NaN NaN
2466 PR total 1999 NaN NaN
2467 PR under18 1999 NaN NaN
2468 PR total 2000 3810605.0 NaN
2469 PR under18 2000 1089063.0 NaN
2470 PR total 2001 3818774.0 NaN
2471 PR under18 2001 1077566.0 NaN
2472 PR total 2002 3823701.0 NaN
2473 PR under18 2002 1065051.0 NaN
2474 PR total 2004 3826878.0 NaN
2475 PR under18 2004 1035919.0 NaN
2476 PR total 2003 3826095.0 NaN
2477 PR under18 2003 1050615.0 NaN
... ... ... ... ... ...
2514 USA under18 1999 71946051.0 NaN
2515 USA total 2000 282162411.0 NaN
2516 USA under18 2000 72376189.0 NaN
2517 USA total 1999 279040181.0 NaN
2518 USA total 2001 284968955.0 NaN
2519 USA under18 2001 72671175.0 NaN
2520 USA total 2002 287625193.0 NaN
2521 USA under18 2002 72936457.0 NaN
2522 USA total 2003 290107933.0 NaN
2523 USA under18 2003 73100758.0 NaN
2524 USA total 2004 292805298.0 NaN
2525 USA under18 2004 73297735.0 NaN
2526 USA total 2005 295516599.0 NaN
2527 USA under18 2005 73523669.0 NaN
2528 USA total 2006 298379912.0 NaN
2529 USA under18 2006 73757714.0 NaN
2530 USA total 2007 301231207.0 NaN
2531 USA under18 2007 74019405.0 NaN
2532 USA total 2008 304093966.0 NaN
2533 USA under18 2008 74104602.0 NaN
2534 USA under18 2013 73585872.0 NaN
2535 USA total 2013 316128839.0 NaN
2536 USA total 2009 306771529.0 NaN
2537 USA under18 2009 74134167.0 NaN
2538 USA under18 2010 74119556.0 NaN
2539 USA total 2010 309326295.0 NaN
2540 USA under18 2011 73902222.0 NaN
2541 USA total 2011 311582564.0 NaN
2542 USA under18 2012 73708179.0 NaN
2543 USA total 2012 313873685.0 NaN

96 rows × 5 columns

In [ ]:

查看缺失数据

根据数据是否缺失情况显示数据,如果缺失为True,那么显示

In [62]:
cond_state = pop2["state"].isnull()

找到有哪些state/region使得state的值为NaN,使用unique()查看非重复值

In [63]:
pop2[cond_state]["state/region"].unique()
Out[63]:
array(['PR', 'USA'], dtype=object)

为找到的这些state/region的state项补上正确的值,从而去除掉state这一列的所有NaN!

记住这样清除缺失数据NaN的方法!

In [64]:
cond_pr = pop2["state/region"] == "PR"
cond_usa = pop2["state/region"] == "USA"
In [65]:
pop2["state"][cond_pr] = "Puerto Rico"
pop2["state"][cond_usa] = "United State"
d:\Anaconda\lib\site-packages\ipykernel_launcher.py:1: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  """Entry point for launching an IPython kernel.
d:\Anaconda\lib\site-packages\ipykernel_launcher.py:2: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  
In [66]:
pop2.head()
Out[66]:
  state/region ages year population state
0 AL under18 2012 1117489.0 Alabama
1 AL total 2012 4817528.0 Alabama
2 AL under18 2010 1130966.0 Alabama
3 AL total 2010 4785570.0 Alabama
4 AL under18 2011 1125763.0 Alabama
In [67]:
pop2.isnull().any()
Out[67]:
state/region    False
ages            False
year            False
population       True
state           False
dtype: bool

合并各州面积数据areas,使用左合并。

思考一下为什么使用外合并?

In [68]:
areas.head()
Out[68]:
  state area (sq. mi)
0 Alabama 52423
1 Alaska 656425
2 Arizona 114006
3 Arkansas 53182
4 California 163707
In [69]:
pop3 = pop2.merge(areas,how="outer")
pop3.head()
Out[69]:
  state/region ages year population state area (sq. mi)
0 AL under18 2012 1117489.0 Alabama 52423.0
1 AL total 2012 4817528.0 Alabama 52423.0
2 AL under18 2010 1130966.0 Alabama 52423.0
3 AL total 2010 4785570.0 Alabama 52423.0
4 AL under18 2011 1125763.0 Alabama 52423.0
In [ ]:

继续寻找存在缺失数据的列

In [70]:
pop3.isnull().any()
Out[70]:
state/region     False
ages             False
year             False
population        True
state            False
area (sq. mi)     True
dtype: bool
In [71]:
cond_area = pop3["area (sq. mi)"].isnull()
In [72]:
pop3[cond_area]
Out[72]:
  state/region ages year population state area (sq. mi)
2496 USA under18 1990 64218512.0 United State NaN
2497 USA total 1990 249622814.0 United State NaN
2498 USA total 1991 252980942.0 United State NaN
2499 USA under18 1991 65313018.0 United State NaN
2500 USA under18 1992 66509177.0 United State NaN
2501 USA total 1992 256514231.0 United State NaN
2502 USA total 1993 259918595.0 United State NaN
2503 USA under18 1993 67594938.0 United State NaN
2504 USA under18 1994 68640936.0 United State NaN
2505 USA total 1994 263125826.0 United State NaN
2506 USA under18 1995 69473140.0 United State NaN
2507 USA under18 1996 70233512.0 United State NaN
2508 USA total 1995 266278403.0 United State NaN
2509 USA total 1996 269394291.0 United State NaN
2510 USA total 1997 272646932.0 United State NaN
2511 USA under18 1997 70920738.0 United State NaN
2512 USA under18 1998 71431406.0 United State NaN
2513 USA total 1998 275854116.0 United State NaN
2514 USA under18 1999 71946051.0 United State NaN
2515 USA total 2000 282162411.0 United State NaN
2516 USA under18 2000 72376189.0 United State NaN
2517 USA total 1999 279040181.0 United State NaN
2518 USA total 2001 284968955.0 United State NaN
2519 USA under18 2001 72671175.0 United State NaN
2520 USA total 2002 287625193.0 United State NaN
2521 USA under18 2002 72936457.0 United State NaN
2522 USA total 2003 290107933.0 United State NaN
2523 USA under18 2003 73100758.0 United State NaN
2524 USA total 2004 292805298.0 United State NaN
2525 USA under18 2004 73297735.0 United State NaN
2526 USA total 2005 295516599.0 United State NaN
2527 USA under18 2005 73523669.0 United State NaN
2528 USA total 2006 298379912.0 United State NaN
2529 USA under18 2006 73757714.0 United State NaN
2530 USA total 2007 301231207.0 United State NaN
2531 USA under18 2007 74019405.0 United State NaN
2532 USA total 2008 304093966.0 United State NaN
2533 USA under18 2008 74104602.0 United State NaN
2534 USA under18 2013 73585872.0 United State NaN
2535 USA total 2013 316128839.0 United State NaN
2536 USA total 2009 306771529.0 United State NaN
2537 USA under18 2009 74134167.0 United State NaN
2538 USA under18 2010 74119556.0 United State NaN
2539 USA total 2010 309326295.0 United State NaN
2540 USA under18 2011 73902222.0 United State NaN
2541 USA total 2011 311582564.0 United State NaN
2542 USA under18 2012 73708179.0 United State NaN
2543 USA total 2012 313873685.0 United State NaN

我们会发现area(sq.mi)这一列有缺失数据,为了找出是哪一行,我们需要找出是哪个state没有数据

In [73]:
# usa面积缺失,就可以吧所有的州面积求和
usa_area = areas["area (sq. mi)"].sum()
In [74]:
# 把美国的面积赋值到pop3中
pop3["area (sq. mi)"][cond_area] = usa_area
d:\Anaconda\lib\site-packages\ipykernel_launcher.py:2: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  
In [75]:
pop3
Out[75]:
  state/region ages year population state area (sq. mi)
0 AL under18 2012 1117489.0 Alabama 52423.0
1 AL total 2012 4817528.0 Alabama 52423.0
2 AL under18 2010 1130966.0 Alabama 52423.0
3 AL total 2010 4785570.0 Alabama 52423.0
4 AL under18 2011 1125763.0 Alabama 52423.0
5 AL total 2011 4801627.0 Alabama 52423.0
6 AL total 2009 4757938.0 Alabama 52423.0
7 AL under18 2009 1134192.0 Alabama 52423.0
8 AL under18 2013 1111481.0 Alabama 52423.0
9 AL total 2013 4833722.0 Alabama 52423.0
10 AL total 2007 4672840.0 Alabama 52423.0
11 AL under18 2007 1132296.0 Alabama 52423.0
12 AL total 2008 4718206.0 Alabama 52423.0
13 AL under18 2008 1134927.0 Alabama 52423.0
14 AL total 2005 4569805.0 Alabama 52423.0
15 AL under18 2005 1117229.0 Alabama 52423.0
16 AL total 2006 4628981.0 Alabama 52423.0
17 AL under18 2006 1126798.0 Alabama 52423.0
18 AL total 2004 4530729.0 Alabama 52423.0
19 AL under18 2004 1113662.0 Alabama 52423.0
20 AL total 2003 4503491.0 Alabama 52423.0
21 AL under18 2003 1113083.0 Alabama 52423.0
22 AL total 2001 4467634.0 Alabama 52423.0
23 AL under18 2001 1120409.0 Alabama 52423.0
24 AL total 2002 4480089.0 Alabama 52423.0
25 AL under18 2002 1116590.0 Alabama 52423.0
26 AL under18 1999 1121287.0 Alabama 52423.0
27 AL total 1999 4430141.0 Alabama 52423.0
28 AL total 2000 4452173.0 Alabama 52423.0
29 AL under18 2000 1122273.0 Alabama 52423.0
... ... ... ... ... ... ...
2514 USA under18 1999 71946051.0 United State 3790399.0
2515 USA total 2000 282162411.0 United State 3790399.0
2516 USA under18 2000 72376189.0 United State 3790399.0
2517 USA total 1999 279040181.0 United State 3790399.0
2518 USA total 2001 284968955.0 United State 3790399.0
2519 USA under18 2001 72671175.0 United State 3790399.0
2520 USA total 2002 287625193.0 United State 3790399.0
2521 USA under18 2002 72936457.0 United State 3790399.0
2522 USA total 2003 290107933.0 United State 3790399.0
2523 USA under18 2003 73100758.0 United State 3790399.0
2524 USA total 2004 292805298.0 United State 3790399.0
2525 USA under18 2004 73297735.0 United State 3790399.0
2526 USA total 2005 295516599.0 United State 3790399.0
2527 USA under18 2005 73523669.0 United State 3790399.0
2528 USA total 2006 298379912.0 United State 3790399.0
2529 USA under18 2006 73757714.0 United State 3790399.0
2530 USA total 2007 301231207.0 United State 3790399.0
2531 USA under18 2007 74019405.0 United State 3790399.0
2532 USA total 2008 304093966.0 United State 3790399.0
2533 USA under18 2008 74104602.0 United State 3790399.0
2534 USA under18 2013 73585872.0 United State 3790399.0
2535 USA total 2013 316128839.0 United State 3790399.0
2536 USA total 2009 306771529.0 United State 3790399.0
2537 USA under18 2009 74134167.0 United State 3790399.0
2538 USA under18 2010 74119556.0 United State 3790399.0
2539 USA total 2010 309326295.0 United State 3790399.0
2540 USA under18 2011 73902222.0 United State 3790399.0
2541 USA total 2011 311582564.0 United State 3790399.0
2542 USA under18 2012 73708179.0 United State 3790399.0
2543 USA total 2012 313873685.0 United State 3790399.0

2544 rows × 6 columns

去除含有缺失数据的行

In [76]:
pop3.isnull().any()
Out[76]:
state/region     False
ages             False
year             False
population        True
state            False
area (sq. mi)    False
dtype: bool

查看数据是否缺失

In [77]:
pop3.dropna(inplace=True)
In [78]:
pop3.isnull().any()
Out[78]:
state/region     False
ages             False
year             False
population       False
state            False
area (sq. mi)    False
dtype: bool

找出2010年的全民人口数据,df.query(查询语句)

In [79]:
pop3.head()
Out[79]:
  state/region ages year population state area (sq. mi)
0 AL under18 2012 1117489.0 Alabama 52423.0
1 AL total 2012 4817528.0 Alabama 52423.0
2 AL under18 2010 1130966.0 Alabama 52423.0
3 AL total 2010 4785570.0 Alabama 52423.0
4 AL under18 2011 1125763.0 Alabama 52423.0
In [80]:
pop3.query("year==2010 & ages=='total' & state=='United State'")
Out[80]:
  state/region ages year population state area (sq. mi)
2539 USA total 2010 309326295.0 United State 3790399.0
In [81]:
pop_2010 = pop3.query("year==2010 & ages=='total'")
pop_2010
Out[81]:
  state/region ages year population state area (sq. mi)
3 AL total 2010 4785570.0 Alabama 52423.0
91 AK total 2010 713868.0 Alaska 656425.0
101 AZ total 2010 6408790.0 Arizona 114006.0
189 AR total 2010 2922280.0 Arkansas 53182.0
197 CA total 2010 37333601.0 California 163707.0
283 CO total 2010 5048196.0 Colorado 104100.0
293 CT total 2010 3579210.0 Connecticut 5544.0
379 DE total 2010 899711.0 Delaware 1954.0
389 DC total 2010 605125.0 District of Columbia 68.0
475 FL total 2010 18846054.0 Florida 65758.0
485 GA total 2010 9713248.0 Georgia 59441.0
570 HI total 2010 1363731.0 Hawaii 10932.0
581 ID total 2010 1570718.0 Idaho 83574.0
666 IL total 2010 12839695.0 Illinois 57918.0
677 IN total 2010 6489965.0 Indiana 36420.0
762 IA total 2010 3050314.0 Iowa 56276.0
773 KS total 2010 2858910.0 Kansas 82282.0
858 KY total 2010 4347698.0 Kentucky 40411.0
869 LA total 2010 4545392.0 Louisiana 51843.0
954 ME total 2010 1327366.0 Maine 35387.0
965 MD total 2010 5787193.0 Maryland 12407.0
1050 MA total 2010 6563263.0 Massachusetts 10555.0
1061 MI total 2010 9876149.0 Michigan 96810.0
1146 MN total 2010 5310337.0 Minnesota 86943.0
1157 MS total 2010 2970047.0 Mississippi 48434.0
1242 MO total 2010 5996063.0 Missouri 69709.0
1253 MT total 2010 990527.0 Montana 147046.0
1338 NE total 2010 1829838.0 Nebraska 77358.0
1349 NV total 2010 2703230.0 Nevada 110567.0
1434 NH total 2010 1316614.0 New Hampshire 9351.0
1445 NJ total 2010 8802707.0 New Jersey 8722.0
1530 NM total 2010 2064982.0 New Mexico 121593.0
1541 NY total 2010 19398228.0 New York 54475.0
1626 NC total 2010 9559533.0 North Carolina 53821.0
1637 ND total 2010 674344.0 North Dakota 70704.0
1722 OH total 2010 11545435.0 Ohio 44828.0
1733 OK total 2010 3759263.0 Oklahoma 69903.0
1818 OR total 2010 3837208.0 Oregon 98386.0
1829 PA total 2010 12710472.0 Pennsylvania 46058.0
1914 RI total 2010 1052669.0 Rhode Island 1545.0
1925 SC total 2010 4636361.0 South Carolina 32007.0
2010 SD total 2010 816211.0 South Dakota 77121.0
2021 TN total 2010 6356683.0 Tennessee 42146.0
2106 TX total 2010 25245178.0 Texas 268601.0
2117 UT total 2010 2774424.0 Utah 84904.0
2202 VT total 2010 625793.0 Vermont 9615.0
2213 VA total 2010 8024417.0 Virginia 42769.0
2298 WA total 2010 6742256.0 Washington 71303.0
2309 WV total 2010 1854146.0 West Virginia 24231.0
2394 WI total 2010 5689060.0 Wisconsin 65503.0
2405 WY total 2010 564222.0 Wyoming 97818.0
2490 PR total 2010 3721208.0 Puerto Rico 3515.0
2539 USA total 2010 309326295.0 United State 3790399.0

对查询结果进行处理,以state列作为新的行索引:set_index

In [82]:
pop_2010.set_index("state",inplace=True)
In [83]:
pop_2010.shape
Out[83]:
(53, 5)

计算人口密度。注意是Series/Series,其结果还是一个Series。

In [84]:
pop_dense = pop_2010["population"] / pop_2010["area (sq. mi)"]
pop_dense
Out[84]:
state
Alabama                   91.287603
Alaska                     1.087509
Arizona                   56.214497
Arkansas                  54.948667
California               228.051342
Colorado                  48.493718
Connecticut              645.600649
Delaware                 460.445752
District of Columbia    8898.897059
Florida                  286.597129
Georgia                  163.409902
Hawaii                   124.746707
Idaho                     18.794338
Illinois                 221.687472
Indiana                  178.197831
Iowa                      54.202751
Kansas                    34.745266
Kentucky                 107.586994
Louisiana                 87.676099
Maine                     37.509990
Maryland                 466.445797
Massachusetts            621.815538
Michigan                 102.015794
Minnesota                 61.078373
Mississippi               61.321530
Missouri                  86.015622
Montana                    6.736171
Nebraska                  23.654153
Nevada                    24.448796
New Hampshire            140.799273
New Jersey              1009.253268
New Mexico                16.982737
New York                 356.094135
North Carolina           177.617157
North Dakota               9.537565
Ohio                     257.549634
Oklahoma                  53.778278
Oregon                    39.001565
Pennsylvania             275.966651
Rhode Island             681.339159
South Carolina           144.854594
South Dakota              10.583512
Tennessee                150.825298
Texas                     93.987655
Utah                      32.677188
Vermont                   65.085075
Virginia                 187.622273
Washington                94.557817
West Virginia             76.519582
Wisconsin                 86.851900
Wyoming                    5.768079
Puerto Rico             1058.665149
United State              81.607845
dtype: float64

排序,并找出人口密度最高的五个州sort_values()

In [85]:
pop_dense.sort_values(inplace=True)
pop_dense
Out[85]:
state
Alaska                     1.087509
Wyoming                    5.768079
Montana                    6.736171
North Dakota               9.537565
South Dakota              10.583512
New Mexico                16.982737
Idaho                     18.794338
Nebraska                  23.654153
Nevada                    24.448796
Utah                      32.677188
Kansas                    34.745266
Maine                     37.509990
Oregon                    39.001565
Colorado                  48.493718
Oklahoma                  53.778278
Iowa                      54.202751
Arkansas                  54.948667
Arizona                   56.214497
Minnesota                 61.078373
Mississippi               61.321530
Vermont                   65.085075
West Virginia             76.519582
United State              81.607845
Missouri                  86.015622
Wisconsin                 86.851900
Louisiana                 87.676099
Alabama                   91.287603
Texas                     93.987655
Washington                94.557817
Michigan                 102.015794
Kentucky                 107.586994
Hawaii                   124.746707
New Hampshire            140.799273
South Carolina           144.854594
Tennessee                150.825298
Georgia                  163.409902
North Carolina           177.617157
Indiana                  178.197831
Virginia                 187.622273
Illinois                 221.687472
California               228.051342
Ohio                     257.549634
Pennsylvania             275.966651
Florida                  286.597129
New York                 356.094135
Delaware                 460.445752
Maryland                 466.445797
Massachusetts            621.815538
Connecticut              645.600649
Rhode Island             681.339159
New Jersey              1009.253268
Puerto Rico             1058.665149
District of Columbia    8898.897059
dtype: float64
In [86]:
pop_dense.tail()
Out[86]:
state
Connecticut              645.600649
Rhode Island             681.339159
New Jersey              1009.253268
Puerto Rico             1058.665149
District of Columbia    8898.897059
dtype: float64

找出人口密度最低的五个州

In [87]:
pop_dense.head()
Out[87]:
state
Alaska           1.087509
Wyoming          5.768079
Montana          6.736171
North Dakota     9.537565
South Dakota    10.583512
dtype: float64

要点总结:

  • 统一用loc()索引
  • 善于使用.isnull().any()找到存在NaN的列
  • 善于使用.unique()确定该列中哪些key是我们需要的
  • 一般使用外合并、左合并,目的只有一个:宁愿该列是NaN也不要丢弃其他列的信息

你可能感兴趣的:(数据分析)