人口数据分析案例

需求:

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_pop = pd.merge(abb, pop, left_on='abbreviation', right_on='state/region', how='outer')

为了保证数据的完整性,这里使用了outer, 默认情况下是inner

 

#将合并的数据中重复的abbreviation列进行删除

注意,这里是删除列

abb_pop.drop(labels='abbreviation', axis=1,,inplace=True)

在drop中,axis=1表示是列,一般情况下axis=1表示为行, inplace=True则是将结果作用到原数据中。

 

查看存在缺失数据的列

这里有两种方法:

方法一:

abb_pop.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 2544 entries, 0 to 2543
Data columns (total 5 columns):
state           2448 non-null object
state/region    2544 non-null object
ages            2544 non-null object
year            2544 non-null int64
population      2524 non-null float64
dtypes: float64(1), int64(1), object(3)
memory usage: 119.2+ KB
可以看到总共有2544行数据,但其中state, population的数据要少,说明有空数据

方法二:

abb_pop.isnull().any()
state            True
state/region    False
ages            False
year            False
population       True
dtype: bool

isnull(),如果为空,则返回True,否则返回False,可以看到,同样state, population表有空数据。

 

# 找到有哪些state/region(简称)使得state(全称)的值为NaN,进行去重操作

这里理解起来比较绕,这里分开理解:

state列有NaN, 找到它对应的state/region,然后对它进行去重

# 取出state列,判断是否为NaN,返回的结果是True,或者False
abb_pop['state'].isnull()

# 将True,False作为索引,即返回state为空的行数据
abb_pop[abb_pop['state'].isnull()]

# 取出state/region列
abb_pop[abb_pop['state'].isnull()]['state/region]

# 去重
abb_pop[abb_pop['state'].isnull()]['state/region].unique()

它返回的结果是:

array(['PR', 'USA'], dtype=object)

说明,PR,USA对应的state的数据为Nan

 

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

从上一步的结果中可以看到,state中的空值有两种情况分别为:PR, USA,也就是说我们要分别赋正确的值,而要分开这两种情况只能分别判断

abb_pop['state/region']

# 判断是否为PR
abb_pop['state/region'] == 'PR' # 返回为True,则说明数据为PR

# 以True,False为索引取出行数据
abb_pop.loc[abb_pop['state/region'] == 'PR']
# 或者下面也行:因为True,False根本不是原来的索引
abb_pop[abb_pop['state/region'] == 'PR']

# 获取对应的索引
index_pr = abb_pop.loc[abb_pop['state/region'] == 'PR'].index

# 得到的index_pr为
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],
           dtype='int64')

现在我们得到了pr对应的行索引,要做的就是对这些行的state进行赋值

# 这里用loc是对的,因为index_pr是abb_pop的索引
abb_pop.loc[index_pr, 'state']= 'Puerto Rico'

再次查看为空的state/region

# 可以看到只有usa了
abb_pop.loc[abb_pop['state'].isnull()]['state/region'].unique()

同样的道理,处理USA的行,将USA对应的全称的空值覆盖为United states

# 取出USA的行,先判断,拿到index
abb_pop['state/region'] == 'USA'

#根据index取出对应行数据
abb_pop.loc[abb_pop['state/region'] == 'USA']

#取出这些行的索引
index_usa = abb_pop.loc[abb_pop['state/region'] == 'USA'].index

对应索引值列表如下:
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')

现在,对这些行的值进行填充:

abb_pop.loc[index_usa, 'state'] = 'United states'

再次查看原表:已经没有空值了

abb_pop.loc[abb_pop['state'].isnull()]['state/region'].unique()
array([], dtype=object)

 

# 合并各州面积数据areas

abb_pop_area = pd.merge(abb_pop, area, on='state',how='outer')

只有有相同数据时才能合并,这里通过state合并,使用outer是为了保持数据完整

 

# 我们会发现area(sq.mi)这一列有缺失数据,找出是哪些行,去除含有缺失数据的行

 

# 判断空值的,将True,False作为索引
area_null_index = abb_pop_area['area (sq. mi)'].isnull()

# 取出对应的行,再取它的索引
drop_index = abb_pop_area[area_null_index].index
# abb_pop_area.loc[area_null_index].index

# 去除这些行
abb_pop_area.drop(labels=drop_index,axis=0, inplace=True)

 

# 2010年的全民人口数据

abb_pop_area.query('ages=="total" & year=2010')

 

# 计算各州人口密度

abb_pop_area['population']/abb_pop_area['area (sq. mi)']
# 赋值施回表中
abb_pop_area['density']=abb_pop_area['population']/abb_pop_area['area (sq. mi)']

 

 

 

 

 

 

 

 

 

 

 

上一篇:Sqlite3 插入数据 Sqlite3.OperationalError: near "%": Syntax error

下一篇:Pandas高级操作