• 需求:

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

.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
vertical-align: top;
} .dataframe thead th {
text-align: right;
}
state abbreviation
0 Alabama AL
1 Alaska AK
pop = pd.read_csv('./data/state-population.csv')
pop.head(2)

.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
vertical-align: top;
} .dataframe thead th {
text-align: right;
}
state/region ages year population
0 AL under18 2012 1117489.0
1 AL total 2012 4817528.0
area = pd.read_csv('./data/state-areas.csv')
area.head(2)

.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
vertical-align: top;
} .dataframe thead th {
text-align: right;
}
state area (sq. mi)
0 Alabama 52423
1 Alaska 656425
# 将人口数据和各州简称数据进行合并
abb_pop = pd.merge(abb,pop,left_on='abbreviation',right_on='state/region',how='outer')
abb_pop.head(2)

.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
vertical-align: top;
} .dataframe thead th {
text-align: right;
}
state abbreviation state/region ages year population
0 Alabama AL AL under18 2012 1117489.0
1 Alabama AL AL total 2012 4817528.0
# 将合并的数据中重复的abbreviation列进行删除
abb_pop.drop(labels='abbreviation',axis=1,inplace=True)
abb_pop.head(2)

.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
vertical-align: top;
} .dataframe thead th {
text-align: right;
}
state state/region ages year population
0 Alabama AL under18 2012 1117489.0
1 Alabama AL total 2012 4817528.0
# 查看存在缺失数据的列
abb_pop.isnull().any(axis=0)
state            True
state/region False
ages False
year False
population True
dtype: bool
# 找到有哪些state/region使得state的值为NaN,进行去重操作
# 1.state列中哪些值为空
abb_pop['state'].isnull()
0       False
1 False
2 False
...
2542 True
2543 True
Name: state, Length: 2544, dtype: bool
# 2.可以将step1中空对应的行数据取出(state中的空值对应的行数据)
abb_pop.loc[abb_pop['state'].isnull()]

.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
vertical-align: top;
} .dataframe thead th {
text-align: right;
}
state state/region ages year population
2448 NaN PR under18 1990 NaN
... ... ... ... ... ...
2543 NaN USA total 2012 313873685.0

96 rows × 5 columns

# 3.将对应的行数据中指定的简称列取出
abb_pop.loc[abb_pop['state'].isnull()]['state/region'].unique()
array(['PR', 'USA'], dtype=object)
# 为找到的这些state/region的state项补上正确的值,从而去除掉state这一列的所有NaN
# 1.先将USA对应的state列中的空值定位到
abb_pop['state/region'] == 'USA'
0       False
1 False
2 False
3 False
...
2541 True
2542 True
2543 True
Name: state/region, Length: 2544, dtype: bool
# 2,将布尔值作为原数据的行索引,取出USA简称对应的行数据
abb_pop.loc[abb_pop['state/region'] == 'USA']

.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
vertical-align: top;
} .dataframe thead th {
text-align: right;
}
state state/region ages year population
2496 NaN USA under18 1990 64218512.0
... ... ... ... ... ...
2542 NaN USA under18 2012 73708179.0
2543 NaN USA total 2012 313873685.0
# 3.获取符合要求行数据的行索引
indexs = abb_pop.loc[abb_pop['state/region'] == 'USA'].index
# 4.将indexs这些行中的state列的值批量赋值成united states
abb_pop.loc[indexs,'state'] = 'United Status'
# 将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'
# 合并各州面积数据areas
abb_pop_area = pd.merge(abb_pop,area,how='outer')
abb_pop_area.head(3)

.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
vertical-align: top;
} .dataframe thead th {
text-align: right;
}
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
# 我们会发现area(sq.mi)这一列有缺失数据,找出是哪些行
abb_pop_area['area (sq. mi)'].isnull()
# 将空值对应的行数据取出
indexs = abb_pop_area.loc[abb_pop_area['area (sq. mi)'].isnull()].index
indexs
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')
# 去除含有缺失数据的行
abb_pop_area.drop(labels=indexs,axis=0,inplace=True)
# 找出2010年的全民人口数据    条件查询
abb_pop_area.query('year == 2010 & ages == "total"')

.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
vertical-align: top;
} .dataframe thead th {
text-align: right;
}

...
...

state state/region ages year population area (sq. mi)
3 Alabama AL total 2010.0 4785570.0 52423.0
91 Alaska AK total 2010.0 713868.0 656425.0
101 Arizona AZ total 2010.0 6408790.0 114006.0
189 Arkansas AR total 2010.0 2922280.0 53182.0
197 California CA total 2010.0 37333601.0 163707.0
2405 Wyoming WY total 2010.0 564222.0 97818.0
# 计算各州的人口密度
abb_pop_area['midu'] = abb_pop_area['population'] / abb_pop_area['area (sq. mi)']
abb_pop_area.head(2)

.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
vertical-align: top;
} .dataframe thead th {
text-align: right;
}
state state/region ages year population area (sq. mi) midu
0 Alabama AL under18 2012.0 1117489.0 52423.0 21.316769
1 Alabama AL total 2012.0 4817528.0 52423.0 91.897221
# 排序,并找出人口密度最高的五个州   df.sort_values()
abb_pop_area.sort_values(by='midu',axis=0,ascending=False).head(5)

.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}

.dataframe tbody tr th {
vertical-align: top;
} .dataframe thead th {
text-align: right;
}
state state/region ages year population area (sq. mi) midu
391 District of Columbia DC total 2013.0 646449.0 68.0 9506.602941
385 District of Columbia DC total 2012.0 633427.0 68.0 9315.102941
387 District of Columbia DC total 2011.0 619624.0 68.0 9112.117647
431 District of Columbia DC total 1990.0 605321.0 68.0 8901.779412
389 District of Columbia DC total 2010.0 605125.0 68.0 8898.897059
abb_pop_area.groupby(by='state')['area (sq. mi)'].max().sort_values(ascending=False).head(5)
state
Alaska 656425.0
Texas 268601.0
California 163707.0
Montana 147046.0
New Mexico 121593.0
Name: area (sq. mi), dtype: float64

Pandas案例--人口密度分析的更多相关文章

  1. pandas - 案例(股票分析)

    需求: 使用tushare包获取某股票的历史行情数据. 输出该股票所有收盘比开盘上涨3%以上的日期. 输出该股票所有开盘比前日收盘跌幅超过2%的日期. 假如我从2010年1月1日开始,每月第一个交易日 ...

  2. 转 Unity企业级支持案例与分析

    Unity大中华区技术支持总监张黎明以“Unity企业级支持案例与分析”为主题进行了分享. 以下为演讲实录: 张黎明:非常感谢大家来参加今年的Unite,其实我现在看到有的朋友已经不是第一次来参加Un ...

  3. unite2017《Unity企业级支持案例与分析》

    在今天举办的Unite2017开发者大会上,Unity大中华区技术支持总监张黎明以"Unity企业级支持案例与分析"为主题进行了分享. 以下为演讲实录: 张黎明:非常感谢大家来参加 ...

  4. 2018.3.3 多线程中继承Thread 和实现Runnable接口 的比较(通过售票案例来分析)

    多线程中继承Thread 和实现Runnable接口 的比较(通过售票案例来分析) 通过Thread来实现 Test.java package com.lanqiao.demo4; public cl ...

  5. Pandas应用案例-股票分析:使用tushare包获取股票的历史行情数据进行数据分析

    目标: 使用tushare包获取股票的历史行情数据 输出该股票所有收盘比开盘上涨3%以上的日期 输出该股票所有开盘比前日收盘跌幅超过2%以上的日期 假如为我们从2010年1月1日开始,每月第一个交易日 ...

  6. UE4的AI学习(2)——官方案例实例分析

    官方给出的AI实例是实现一个跟随着玩家跑的AI,当玩家没有在AI视野里时,它会继续跑到最后看到玩家的地点,等待几秒后如果仍然看不到玩家,则跑回初始地点.官方的案例已经讲得比较详细,对于一些具体的函数调 ...

  7. 026 使用大数据对网站基本指标PV案例的分析

    案例: 使用电商网站的用户行为日志进行统计分析 一:准备 1.指标 PV:网页流浪量 UV:独立访客数 VV:访客的访问数,session次数 IP:独立的IP数 2.上传测试数据 3.查看第一条记录 ...

  8. pandas - 案例(美国各州人口普查)

    需求: 导入文件,查看原始数据 将人口数据和各州简称数据进行合并 将合并的数据中重复的abbreviation列进行删除 查看存在缺失数据的列 找到有哪些state/region使得state的值为N ...

  9. 利用Pandas和matplotlib分析我爱我家房租区间频率

    前几天利用python爬取了我爱我家的租房的一些数据,就想着能不能对房租进行一波分析,于是通过书籍和博客等查阅了相关资料,进行了房租的区间分析.不得不说,用python做区间分析比我之前用sql关键字 ...

随机推荐

  1. POJ 2104 区间第k大(主席树)

    题目链接:http://poj.org/problem?id=2104 题目大意:给定还有n个数的序列,m个操作,每个操作含有l,r,k,求区间[l,r]第k大 解题思路:线段树只能维护序列的最大值最 ...

  2. alert(1) to win 4

    function escape(s) { var url = 'javascript:console.log(' + JSON.stringify(s) + ')'; console.log(url) ...

  3. poj 3468 : A Simple Problem with Integers 【线段树 区间修改】

    题目链接 题目是对一个数组,支持两种操作 操作C:对下标从a到b的每个元素,值增加c: 操作Q:对求下标从a到b的元素值之和. #include<cstdio> #include<c ...

  4. python实现Restful服务 (基于flask)(1)

    参考:https://www.jianshu.com/p/6ac1cab17929 参考:https://www.cnblogs.com/alexyuyu/p/6243362.html 参考:http ...

  5. (arm板子tensorflow安装)armv7板子pip安装的wheel

    树莓派之类的armv7板子在,安装 numpy,scipy时经常失败,因为安装过程是下载源码包到本地编译,然后再安装的,编译过程中往往就会失败. https://www.piwheels.org/si ...

  6. python学习笔记(十三)处理时间模块

    import time time.sleep(2)#等待几秒 时间的三种表现方式: 1.格式化好的时间 2018-1-14 16:12 2.时间戳 是从unix元年到现在所有的秒数 3.时间元组 想时 ...

  7. 【2019 Multi-University Training Contest 6】

    01: 02:https://www.cnblogs.com/myx12345/p/11650764.html 03: 04: 05:https://www.cnblogs.com/myx12345/ ...

  8. 使用PHPExcel操作Excel用法实例分析

    本文实例分析了使用PHPExcel操作Excel用法.分享给大家供大家参考.具体分析如下: PHPExcel下载地址:http://www.codeplex.com/PHPExcel http://w ...

  9. [CSP-S模拟测试]:神炎皇(数学)

    题目描述 神炎皇乌利亚很喜欢数对,他想找到神奇的数对. 对于一个整数对$(a,b)$,若满足$a+b\leqslant n$且$a+b$是$ab$的因子,则称为神奇的数对.请问这样的数对共有多少呢? ...

  10. Spring Boot 的单元测试

    Spring Boot 的单元测试 引入依赖 testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-tes ...