案例分析:美國各州人口數據分析
- 需求:
- 導入文件,查看原始數據
- 將人口數據和各州簡稱數據進行合併
- 將合併的數據中重複的abbreviation列進行刪除
- 查看存在缺失數據的列
- 找到有哪些state/region使得state的值爲NaN,進行去重操做
- 爲找到的這些state/region的state項補上正確的值,從而去除掉state這一列的全部NaN
- 合併各州面積數據areas
- 咱們會發現area(sq.mi)這一列有缺失數據,找出是哪些行
- 去除含有缺失數據的行
- 找出2010年的全民人口數據
- 計算各州的人口密度
- 排序,並找出人口密度最高的五個州 df.sort_values()
# 1.導入文件,查看原始數據
import numpy as np
from pandas import DataFrame,Series
import pandas as pd
abb = pd.read_csv('./data/state-abbrevs.csv')
pop = pd.read_csv('./data/state-population.csv')
area = pd.read_csv('./data/state-areas.csv')
# 查看的數據
abb.head(1)
state abbreviation
0 Alabama AL
pop.head(1)
state/region ages year population
0 AL under18 2012 1117489.0
# 2 將人口數據和各州簡稱數據進行合併
abb_pop = pd.merge(abb,pop,left_on='abbreviation',right_on='state/region',how='outer')
abb_pop.head(3)
state abbreviation state/region ages year population
0 Alabama AL AL under18 2012 1117489.0
1 Alabama AL AL total 2012 4817528.0
2 Alabama AL AL under18 2010 1130966.0
# 3 將合併的數據中重複的abbreviation列進行刪除
abb_pop.drop(labels='abbreviation',axis=1,inplace=True)
# 4 查看存在缺失數據的列
abb_pop.isnull().any(axis=0)
state True
state/region False
ages False
year False
population True
dtype: bool
# 5 找到有哪些state/region使得state的值爲NaN,進行去重操做
# 找到哪些簡稱 的全稱爲空 (就是先找到state中的空值 ,經過state在找到state/region)
# 把簡稱找到之後 進行去重
# 找全稱爲空,用該數據找到簡稱,而後去重
abb_pop.head(5)
state state/region ages year population
0 Alabama AL under18 2012 1117489.0
1 Alabama AL total 2012 4817528.0
2 Alabama AL under18 2010 1130966.0
3 Alabama AL total 2010 4785570.0
4 Alabama AL under18 2011 1125763.0
# 5.1.找出state中的空值
abb_pop['state'].isnull()
# 5.2.將布爾值做爲元數據的行索引:定位到全部state爲空對應的行數據
abb_pop.loc[abb_pop['state'].isnull()]
# 5.3.將空對應的行數據中的簡稱這一列的數據取出進行去重操做
abb_pop.loc[abb_pop['state'].isnull()]['state/region'].unique()
# array([], dtype=object)
# 6 爲找到的這些state/region的state項補上正確的值,從而去除掉state這一列的全部NaN
# 6.1.找出USA對應state列中的空值
# 返回的是bool值
abb_pop['state/region'] == 'USA'
# 6.2.取出USA對應的行數據
abb_pop.loc[abb_pop['state/region'] == 'USA']
indexs = abb_pop.loc[abb_pop['state/region'] == 'USA'].index
indexs
Int64Index([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')
# 6.3.將USA對應的空值覆蓋成對應的值
abb_pop.loc[indexs,'state'] = 'United States'
# 6.4 找到PR所對應的行數據
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'] = 'ppprrr'
area.head()
state area (sq. mi)
0 Alabama 52423
1 Alaska 656425
2 Arizona 114006
3 Arkansas 53182
4 California 163707
# 7 合併各州面積數據areas
abb_pop_area = pd.merge(abb_pop,area,how='outer')
abb_pop_area.head()
state state/region ages year population area (sq. mi)
0 Alabama AL under18 2012.0 1117489.0 52423.0
1 Alabama AL total 2012.0 4817528.0 52423.0
2 Alabama AL under18 2010.0 1130966.0 52423.0
3 Alabama AL total 2010.0 4785570.0 52423.0
4 Alabama AL under18 2011.0 1125763.0 52423.0
# 8 咱們會發現area(sq.mi)這一列有缺失數據,找出是哪些行
# 9 去除含有缺失數據的行
abb_pop_area['area (sq. mi)'].isnull()
abb_pop_area.loc[abb_pop_area['area (sq. mi)'].isnull()]
# 獲取行索引
indexs = abb_pop_area.loc[abb_pop_area['area (sq. mi)'].isnull()].index
abb_pop_area.drop(labels=indexs,axis=0,inplace=True)
# 10 找出2010年的全民人口數據
# query 作條件查詢
df_2010 = abb_pop_area.query('year == 2010 & ages == "total"')
df_2010
# 11 計算各州的人口密度
abb_pop_area['midu'] = abb_pop_area['population'] / abb_pop_area['area (sq. mi)']
abb_pop_area.head(1)
state state/region ages year population area (sq. mi) midu
0 Alabama AL under18 2012.0 1117489.0 52423.0 21.316769
# 12 排序,並找出人口密度最高的五個州 df.sort_values()
abb_pop_area.sort_values(by='midu',axis=0,ascending=False)