• 需求:
    • 导入文件,查看原始数据
    • 将人口数据和各州简称数据进行合并
    • 将合并的数据中重复的abbreviation列进行删除
    • 查看存在缺失数据的列
    • 找到有哪些state/region使得state的值为NaN,进行去重操作
    • 为找到的这些state/region的state项补上正确的值,从而去除掉state这一列的所有NaN
    • 合并各州面积数据areas
    • 我们会发现area(sq.mi)这一列有缺失数据,找出是哪些行
    • 去除含有缺失数据的行
    • 找出2010年的全民人口数据
    • 计算各州的人口密度
    • 排序,并找出人口密度最高的五个州 df.sort_values()
In [1]:
import numpy as np
from pandas import DataFrame,Series
import pandas as pd
In [3]:
abb = pd.read_csv('./data/state-abbrevs.csv')
pop = pd.read_csv('./data/state-population.csv')
area = pd.read_csv('./data/state-areas.csv')
In [8]:
#将人口数据和各州简称数据进行合并
display(abb.head(1),pop.head(1))
abb_pop = pd.merge(abb,pop,left_on='abbreviation',right_on='state/region',how='outer')
abb_pop.head()
 
0AlabamaAL
 
0ALunder1820121117489.0
Out[8]:
0AlabamaALALunder1820121117489.0
1AlabamaALALtotal20124817528.0
2AlabamaALALunder1820101130966.0
3AlabamaALALtotal20104785570.0
4AlabamaALALunder1820111125763.0
In [9]:
#将合并的数据中重复的abbreviation列进行删除
abb_pop.drop(labels='abbreviation',axis=1,inplace=True)
abb_pop.head()
Out[9]:
0AlabamaALunder1820121117489.0
1AlabamaALtotal20124817528.0
2AlabamaALunder1820101130966.0
3AlabamaALtotal20104785570.0
4AlabamaALunder1820111125763.0
In [10]:
#查看存在缺失数据的列
abb_pop.isnull().any(axis=0)
Out[10]:
state            True
state/region    False
ages            False
year            False
population       True
dtype: bool
In [11]:
#找到有哪些state/region使得state的值为NaN,进行去重操作
In [13]:
#1.检测state列中的空值
abb_pop['state'].isnull()
Out[13]:
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
Name: state, Length: 2544, dtype: bool
In [15]:
#2.将1的返回值作用的state_region这一列中
abb_pop['state/region'][abb_pop['state'].isnull()]
Out[15]:
2448     PR
2449     PR
2450     PR
2451     PR
2452     PR
2453     PR
2454     PR
2455     PR
2456     PR
2457     PR
2458     PR
2459     PR
2460     PR
2461     PR
2462     PR
2463     PR
2464     PR
2465     PR
2466     PR
2467     PR
2468     PR
2469     PR
2470     PR
2471     PR
2472     PR
2473     PR
2474     PR
2475     PR
2476     PR
2477     PR
       ...
2514    USA
2515    USA
2516    USA
2517    USA
2518    USA
2519    USA
2520    USA
2521    USA
2522    USA
2523    USA
2524    USA
2525    USA
2526    USA
2527    USA
2528    USA
2529    USA
2530    USA
2531    USA
2532    USA
2533    USA
2534    USA
2535    USA
2536    USA
2537    USA
2538    USA
2539    USA
2540    USA
2541    USA
2542    USA
2543    USA
Name: state/region, Length: 96, dtype: object
In [29]:
#3.去重
abb_pop['state/region'][abb_pop['state'].isnull()].unique()
Out[29]:
array([], dtype=object)
In [19]:
#为找到的这些state/region的state项补上正确的值,从而去除掉state这一列的所有NaN
abb_pop['state/region'] == 'USA'
Out[19]:
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
Name: state/region, Length: 2544, dtype: bool
In [23]:
indexs = abb_pop['state'][abb_pop['state/region'] == 'USA'].index
In [24]:
abb_pop.loc[indexs,'state'] = 'United State'
In [27]:
pr_index = abb_pop['state'][abb_pop['state/region'] == 'PR'].index
In [28]:
abb_pop.loc[pr_index,'state'] = 'PPPRRR'

合并各州面积数据areas 我们会发现area(sq.mi)这一列有缺失数据,找出是哪些行 去除含有缺失数据的行 找出2010年的全民人口数据 计算各州的人口密度 排序,并找出人口密度最高的五个州 df.sort_values()

In [31]:
#合并各州面积数据areas
abb_pop_area = pd.merge(abb_pop,area,how='outer')
abb_pop_area.head()
Out[31]:
0AlabamaALunder182012.01117489.052423.0
1AlabamaALtotal2012.04817528.052423.0
2AlabamaALunder182010.01130966.052423.0
3AlabamaALtotal2010.04785570.052423.0
4AlabamaALunder182011.01125763.052423.0
In [32]:
#我们会发现area(sq.mi)这一列有缺失数据,找出是哪些行
abb_pop_area['area (sq. mi)'].isnull()
Out[32]:
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
        ...
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
2544    False
Name: area (sq. mi), Length: 2545, dtype: bool
In [35]:
a_index = abb_pop_area.loc[abb_pop_area['area (sq. mi)'].isnull()].index
In [36]:
#去除含有缺失数据的行
abb_pop_area.drop(labels=a_index,axis=0,inplace=True)
In [38]:
#找出2010年的全民人口数据
abb_pop_area.query('year == 2010 & ages == "total"')
Out[38]:
3AlabamaALtotal2010.04785570.052423.0
91AlaskaAKtotal2010.0713868.0656425.0
101ArizonaAZtotal2010.06408790.0114006.0
189ArkansasARtotal2010.02922280.053182.0
197CaliforniaCAtotal2010.037333601.0163707.0
283ColoradoCOtotal2010.05048196.0104100.0
293ConnecticutCTtotal2010.03579210.05544.0
379DelawareDEtotal2010.0899711.01954.0
389District of ColumbiaDCtotal2010.0605125.068.0
475FloridaFLtotal2010.018846054.065758.0
485GeorgiaGAtotal2010.09713248.059441.0
570HawaiiHItotal2010.01363731.010932.0
581IdahoIDtotal2010.01570718.083574.0
666IllinoisILtotal2010.012839695.057918.0
677IndianaINtotal2010.06489965.036420.0
762IowaIAtotal2010.03050314.056276.0
773KansasKStotal2010.02858910.082282.0
858KentuckyKYtotal2010.04347698.040411.0
869LouisianaLAtotal2010.04545392.051843.0
954MaineMEtotal2010.01327366.035387.0
965MontanaMTtotal2010.0990527.0147046.0
1050NebraskaNEtotal2010.01829838.077358.0
1061NevadaNVtotal2010.02703230.0110567.0
1146New HampshireNHtotal2010.01316614.09351.0
1157New JerseyNJtotal2010.08802707.08722.0
1242New MexicoNMtotal2010.02064982.0121593.0
1253New YorkNYtotal2010.019398228.054475.0
1338North CarolinaNCtotal2010.09559533.053821.0
1349North DakotaNDtotal2010.0674344.070704.0
1434OhioOHtotal2010.011545435.044828.0
1445OklahomaOKtotal2010.03759263.069903.0
1530OregonORtotal2010.03837208.098386.0
1541MarylandMDtotal2010.05787193.012407.0
1626MassachusettsMAtotal2010.06563263.010555.0
1637MichiganMItotal2010.09876149.096810.0
1722MinnesotaMNtotal2010.05310337.086943.0
1733MississippiMStotal2010.02970047.048434.0
1818MissouriMOtotal2010.05996063.069709.0
1829PennsylvaniaPAtotal2010.012710472.046058.0
1914Rhode IslandRItotal2010.01052669.01545.0
1925South CarolinaSCtotal2010.04636361.032007.0
2010South DakotaSDtotal2010.0816211.077121.0
2021TennesseeTNtotal2010.06356683.042146.0
2106TexasTXtotal2010.025245178.0268601.0
2117UtahUTtotal2010.02774424.084904.0
2202VermontVTtotal2010.0625793.09615.0
2213VirginiaVAtotal2010.08024417.042769.0
2298WashingtonWAtotal2010.06742256.071303.0
2309West VirginiaWVtotal2010.01854146.024231.0
2394WisconsinWItotal2010.05689060.065503.0
2405WyomingWYtotal2010.0564222.097818.0
In [40]:
#计算各州的人口密度
abb_pop_area['midu'] = abb_pop_area['population'] / abb_pop_area['area (sq. mi)']
abb_pop_area.head()
Out[40]:
0AlabamaALunder182012.01117489.052423.021.316769
1AlabamaALtotal2012.04817528.052423.091.897221
2AlabamaALunder182010.01130966.052423.021.573851
3AlabamaALtotal2010.04785570.052423.091.287603
4AlabamaALunder182011.01125763.052423.021.474601
In [43]:
#排序,并找出人口密度最高的五个州 df.sort_values()
abb_pop_area.sort_values(by='midu',axis=0,ascending=False).head()
Out[43]:
391District of ColumbiaDCtotal2013.0646449.068.09506.602941
385District of ColumbiaDCtotal2012.0633427.068.09315.102941
387District of ColumbiaDCtotal2011.0619624.068.09112.117647
431District of ColumbiaDCtotal1990.0605321.068.08901.779412
389District of ColumbiaDCtotal2010.0605125.068.08898.897059
02-12 13:30