人口分析

本篇介绍使用数据分析进行人口分析。

  • 需求:
    • 导入文件,查看原始数据
    • 将人口数据和各州简称数据进行合并
    • 将合并的数据中重复的abbreviation列进行删除
    • 查看存在缺失数据的列
    • 找到有哪些state/region使得state的值为NaN,进行去重操作
    • 为找到的这些state/region的state项补上正确的值,从而去除掉state这一列的所有NaN
    • 合并各州面积数据areas
    • 我们会发现area(sq.mi)这一列有缺失数据,找出是哪些行
    • 去除含有缺失数据的行
    • 找出2010年的全民人口数据
    • 计算各州的人口密度
    • 排序,并找出人口密度最高的五个州 df.sort_values()
1
2
3
import numpy as np
from pandas import DataFrame,Series
import pandas as pd
1
2
abb = pd.read_csv('./data/state-abbrevs.csv')
abb.head(2)

打印:

stateabbreviation
0AlabamaAL
1AlaskaAK
1
2
pop = pd.read_csv('./data/state-population.csv')
pop.head(2)

打印:

state/regionagesyearpopulation
0ALunder1820121117489.0
1ALtotal20124817528.0
1
2
area = pd.read_csv('./data/state-areas.csv')
area.head(2)

打印:

statearea (sq. mi)
0Alabama52423
1Alaska656425
1
2
3
# 将人口数据和各州简称数据进行合并
abb_pop = pd.merge(abb,pop,left_on='abbreviation',right_on='state/region',how='outer')
abb_pop.head(2)

打印:

stateabbreviationstate/regionagesyearpopulation
0AlabamaALALunder1820121117489.0
1AlabamaALALtotal20124817528.0
1
2
3
# 将合并的数据中重复的abbreviation列进行删除
abb_pop.drop(labels='abbreviation',axis=1,inplace=True)
abb_pop.head(2)

打印:

statestate/regionagesyearpopulation
0AlabamaALunder1820121117489.0
1AlabamaALtotal20124817528.0
1
2
# 查看存在缺失数据的列
abb_pop.isnull().any(axis=0)

打印:

1
2
3
4
5
6
state            True
state/region False
ages False
year False
population True
dtype: bool
1
2
3
# 找到有哪些state/region使得state的值为NaN,进行去重操作
# 1.state列中哪些值为空
abb_pop['state'].isnull()

打印:

1
2
3
4
5
6
7
0       False
1 False
2 False
...
2542 True
2543 True
Name: state, Length: 2544, dtype: bool
1
2
# 2.可以将step1中空对应的行数据取出(state中的空值对应的行数据)
abb_pop.loc[abb_pop['state'].isnull()]

打印:

statestate/regionagesyearpopulation
2448NaNPRunder181990NaN
..................
2543NaNUSAtotal2012313873685.0

96 rows × 5 columns

1
2
# 3.将对应的行数据中指定的简称列取出
abb_pop.loc[abb_pop['state'].isnull()]['state/region'].unique()

打印:

1
array(['PR', 'USA'], dtype=object)
1
2
3
# 为找到的这些state/region的state项补上正确的值,从而去除掉state这一列的所有NaN
# 1.先将USA对应的state列中的空值定位到
abb_pop['state/region'] == 'USA'

打印:

1
2
3
4
5
6
7
8
9
0       False
1 False
2 False
3 False
...
2541 True
2542 True
2543 True
Name: state/region, Length: 2544, dtype: bool
1
2
# 2,将布尔值作为原数据的行索引,取出USA简称对应的行数据
abb_pop.loc[abb_pop['state/region'] == 'USA']

打印:

statestate/regionagesyearpopulation
2496NaNUSAunder18199064218512.0
..................
2542NaNUSAunder18201273708179.0
2543NaNUSAtotal2012313873685.0
1
2
# 3.获取符合要求行数据的行索引
indexs = abb_pop.loc[abb_pop['state/region'] == 'USA'].index
1
2
# 4.将indexs这些行中的state列的值批量赋值成united states
abb_pop.loc[indexs,'state'] = 'United Status'
1
2
3
4
5
# 将PR对应的state列中的空批量赋值成 PUERTO RICO
abb_pop['state/region'] == 'PR'
abb_pop.loc[abb_pop['state/region'] == 'PR']
indexs = abb_pop.loc[abb_pop['state/region'] == 'PR'].index
abb_pop.loc[indexs,'state'] = 'PUERTO RICO'
1
2
3
# 合并各州面积数据areas
abb_pop_area = pd.merge(abb_pop,area,how='outer')
abb_pop_area.head(3)

打印:

statestate/regionagesyearpopulationarea (sq. mi)
0AlabamaALunder182012.01117489.052423.0
1AlabamaALtotal2012.04817528.052423.0
2AlabamaALunder182010.01130966.052423.0
1
2
3
4
5
# 我们会发现area(sq.mi)这一列有缺失数据,找出是哪些行
abb_pop_area['area (sq. mi)'].isnull()
# 将空值对应的行数据取出
indexs = abb_pop_area.loc[abb_pop_area['area (sq. mi)'].isnull()].index
indexs

打印:

1
2
3
4
5
6
7
8
9
10
Int64Index([2448, 2449, 2450, 2451, 2452, 2453, 2454, 2455, 2456, 2457, 2458,
2459, 2460, 2461, 2462, 2463, 2464, 2465, 2466, 2467, 2468, 2469,
2470, 2471, 2472, 2473, 2474, 2475, 2476, 2477, 2478, 2479, 2480,
2481, 2482, 2483, 2484, 2485, 2486, 2487, 2488, 2489, 2490, 2491,
2492, 2493, 2494, 2495, 2496, 2497, 2498, 2499, 2500, 2501, 2502,
2503, 2504, 2505, 2506, 2507, 2508, 2509, 2510, 2511, 2512, 2513,
2514, 2515, 2516, 2517, 2518, 2519, 2520, 2521, 2522, 2523, 2524,
2525, 2526, 2527, 2528, 2529, 2530, 2531, 2532, 2533, 2534, 2535,
2536, 2537, 2538, 2539, 2540, 2541, 2542, 2543],
dtype='int64')
1
2
# 去除含有缺失数据的行
abb_pop_area.drop(labels=indexs,axis=0,inplace=True)
1
2
# 找出2010年的全民人口数据    条件查询
abb_pop_area.query('year == 2010 & ages == "total"')

打印:

statestate/regionagesyearpopulationarea (sq. mi)
3AlabamaALtotal2010.04785570.052423.0
91AlaskaAKtotal2010.0713868.0656425.0
101ArizonaAZtotal2010.06408790.0114006.0
189ArkansasARtotal2010.02922280.053182.0
197CaliforniaCAtotal2010.037333601.0163707.0
2405WyomingWYtotal2010.0564222.097818.0
1
2
3
# 计算各州的人口密度
abb_pop_area['midu'] = abb_pop_area['population'] / abb_pop_area['area (sq. mi)']
abb_pop_area.head(2)

打印:

statestate/regionagesyearpopulationarea (sq. mi)midu
0AlabamaALunder182012.01117489.052423.021.316769
1AlabamaALtotal2012.04817528.052423.091.897221
1
2
# 排序,并找出人口密度最高的五个州   df.sort_values()
abb_pop_area.sort_values(by='midu',axis=0,ascending=False).head(5)

打印:

statestate/regionagesyearpopulationarea (sq. mi)midu
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
1
abb_pop_area.groupby(by='state')['area (sq. mi)'].max().sort_values(ascending=False).head(5)

打印:

1
2
3
4
5
6
7
state
Alaska 656425.0
Texas 268601.0
California 163707.0
Montana 147046.0
New Mexico 121593.0
Name: area (sq. mi), dtype: float64
-------------The End-------------

本文标题:人口分析

文章作者:Naqin

发布时间:2019年10月21日 - 10:10

最后更新:2019年11月05日 - 01:11

原始链接:https://chennq.top/数据分析/20191021-data_analysis_3.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

Naqin wechat
欢迎看官加我微信!
坚持原创技术分享,您的支持将鼓励我继续创作!
0%