diff --git a/Day01-15/Day01/初识Python.md b/Day01-15/01.初识Python.md similarity index 90% rename from Day01-15/Day01/初识Python.md rename to Day01-15/01.初识Python.md index 0dd4ff2..e08946e 100644 --- a/Day01-15/Day01/初识Python.md +++ b/Day01-15/01.初识Python.md @@ -1,4 +1,4 @@ -## Day01 - 初识Python +## 初识Python ### Python简介 @@ -159,53 +159,51 @@ IDLE是安装Python环境时自带的集成开发工具,如下图所示。但 IPython是一种基于Python的交互式解释器。相较于原生的Python Shell,IPython提供了更为强大的编辑和交互功能。可以通过Python的包管理工具pip安装IPython和Jupyter,具体的操作如下所示。 ```Shell -pip install ipython jupyter +pip install ipython ``` 或者 ```Shell -python -m pip install ipython jupyter +python -m pip install ipython ``` 安装成功后,可以通过下面的ipython命令启动IPython,如下图所示。 ![](./res/python-ipython.png) -当然我们也可以通过Jupyter运行名为notebook的项目在浏览器窗口中进行交互式操作。 +当然我们也可以通过安装Jupyter并运行名为notebook的程序在浏览器窗口中进行交互式代码编写操作。 ```Shell +pip install jupyter jupyter notebook ``` ![](./res/python-jupyter-2.png) -#### anaconda - 一站式的数据科学神器 -Anaconda指的是一个开源的Python发行版本,其包含了conda、Python等180多个科学包及其依赖项。 -因为包含了大量的科学包,Anaconda 的下载文件比较大(约 531 MB),如果只需要某些包,或者需要节省带宽或存储空间,也可以使用Miniconda这个较小的发行版(仅包含conda和 Python)。 -对于学习数据科学的人来说,anaconda是绝对的神器,安装简便,而且anaconda支持安装相关软件【例如前文提到的ipython,jupyter notebook,甚至有R等其他数据科学软件 】 -[一个相当有价值的介绍](https://www.jianshu.com/p/169403f7e40c) -现在唯一的问题在于清华镜像服务已经关闭,跨国下载会比较慢 - #### Sublime - 文本编辑神器 ![](./res/python-sublime.png) - 首先可以通过[官方网站](https://www.sublimetext.com/)下载安装程序安装Sublime 3或Sublime 2。 -- 安装包管理工具。通过快捷键Ctrl+`或者在View菜单中选择Show Console打开控制台,输入下面的代码。 +- 安装包管理工具。 + 1. 通过快捷键Ctrl+`或者在View菜单中选择Show Console打开控制台,输入下面的代码。 - Sublime 3 ```Python import urllib.request,os;pf='Package Control.sublime-package';ipp=sublime.installed_packages_path();urllib.request.install_opener(urllib.request.build_opener(urllib.request.ProxyHandler()));open(os.path.join(ipp,pf),'wb').write(urllib.request.urlopen('http://sublime.wbond.net/'+pf.replace(' ','%20')).read()) ``` - - Sublime 2 ```Python import urllib2,os;pf='Package Control.sublime-package';ipp=sublime.installed_packages_path();os.makedirs(ipp)ifnotos.path.exists(ipp)elseNone;urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler()));open(os.path.join(ipp,pf),'wb').write(urllib2.urlopen('http://sublime.wbond.net/'+pf.replace(' ','%20')).read());print('Please restart Sublime Text to finish installation') ``` + 2. 手动安装浏览器输入 https://sublime.wbond.net/Package%20Control.sublime-package 下载这个文件 + 下载好以后,打开sublime text,选择菜单Preferences->Browse Packages... 打开安装目录 + 此时会进入到一个叫做Packages的目录下,点击进入上一层目录Sublime Text3,在此目录下有一个文件夹叫做Installed Packages,把刚才下载的文件放到这里就可以了。然后重启sublime text3,观察Preferences菜单最下边是否有Package Settings 和Package Control两个选项,如果有,则代表安装成功了。 + - 安装插件。通过Preference菜单的Package Control或快捷键Ctrl+Shift+P打开命令面板,在面板中输入Install Package就可以找到安装插件的工具,然后再查找需要的插件。我们推荐大家安装以下几个插件: @@ -217,7 +215,7 @@ Anaconda指的是一个开源的Python发行版本,其包含了conda、Python #### PyCharm - Python开发神器 -PyCharm的安装、配置和使用我们在后面会进行介绍。 +PyCharm的安装、配置和使用在[《玩转PyCharm》](../玩转PyCharm.md)进行了介绍,有兴趣的读者可以选择阅读。 ![](./res/python-pycharm.png) diff --git a/Day01-15/Day02/语言元素.md b/Day01-15/02.语言元素.md similarity index 92% rename from Day01-15/Day02/语言元素.md rename to Day01-15/02.语言元素.md index 4a0937c..329637f 100644 --- a/Day01-15/Day02/语言元素.md +++ b/Day01-15/02.语言元素.md @@ -1,4 +1,4 @@ -## Day02 - 语言元素 +## 语言元素 #### 指令和程序 @@ -6,7 +6,7 @@ ### 变量和类型 -在程序设计中,变量是一种存储数据的载体。计算机中的变量是实际存在的数据或者说是存储器中存储数据的一块内存空间,变量的值可以被读取和修改,这是所有计算和控制的基础。计算机能处理的数据有很多中类型,除了数值之外还可以处理文本、图形、音频、视频等各种各样的数据,那么不同的数据就需要定义不同的存储类型。Python中的数据类型很多,而且也允许我们自定义新的数据类型(这一点在后面会讲到),我们先介绍几种常用的数据类型。 +在程序设计中,变量是一种存储数据的载体。计算机中的变量是实际存在的数据或者说是存储器中存储数据的一块内存空间,变量的值可以被读取和修改,这是所有计算和控制的基础。计算机能处理的数据有很多种类型,除了数值之外还可以处理文本、图形、音频、视频等各种各样的数据,那么不同的数据就需要定义不同的存储类型。Python中的数据类型很多,而且也允许我们自定义新的数据类型(这一点在后面会讲到),我们先介绍几种常用的数据类型。 - 整型:Python中可以处理任意大小的整数(Python 2.x中有int和long两种类型的整数,但这种区分对Python来说意义不大,因此在Python 3.x中整数只有int这一种了),而且支持二进制(如`0b100`,换算成十进制是4)、八进制(如`0o100`,换算成十进制是64)、十进制(`100`)和十六进制(`0x100`,换算成十进制是256)的表示法。 - 浮点型:浮点数也就是小数,之所以称为浮点数,是因为按照科学记数法表示时,一个浮点数的小数点位置是可变的,浮点数除了数学写法(如`123.456`)之外还支持科学计数法(如`1.23456e2`)。 @@ -99,11 +99,11 @@ print(type(e)) 在对变量类型进行转换时可以使用Python的内置函数(准确的说下面列出的并不是真正意义上的函数,而是后面我们要讲到的创建对象的构造方法)。 -- int():将一个数值或字符串转换成整数,可以指定进制。 -- float():将一个字符串转换成浮点数。 -- str():将指定的对象转换成字符串形式,可以指定编码。 -- chr():将整数转换成该编码对应的字符串(一个字符)。 -- ord():将字符串(一个字符)转换成对应的编码(整数)。 +- `int()`:将一个数值或字符串转换成整数,可以指定进制。 +- `float()`:将一个字符串转换成浮点数。 +- `str()`:将指定的对象转换成字符串形式,可以指定编码。 +- `chr()`:将整数转换成该编码对应的字符串(一个字符)。 +- `ord()`:将字符串(一个字符)转换成对应的编码(整数)。 ### 运算符 @@ -118,15 +118,15 @@ Python支持多种运算符,下表大致按照优先级从高到低的顺序 | `+` `-` | 加,减 | | `>>` `<<` | 右移,左移 | | `&` | 按位与 | -| `^` `|` | 按位异或,按位或 | +| `^` `\|` | 按位异或,按位或 | | `<=` `<` `>` `>=` | 小于等于,小于,大于,大于等于 | | `==` `!=` | 等于,不等于 | | `is` `is not` | 身份运算符 | | `in` `not in` | 成员运算符 | | `not` `or` `and` | 逻辑运算符 | -| `=` `+=` `-=` `*=` `/=` `%=` `//=` `**=` `&=` `|=` `^=` `>>=` `<<=` | (复合)赋值运算符 | +| `=` `+=` `-=` `*=` `/=` `%=` `//=` `**=` `&=` `\|=` `^=` `>>=` `<<=` | (复合)赋值运算符 | ->**说明:**在实际开发中,如果搞不清楚优先级可以使用括号来确保运算的执行顺序。 +>**说明:** 在实际开发中,如果搞不清楚优先级可以使用括号来确保运算的执行顺序。 下面的例子演示了运算符的使用。 diff --git a/Day01-15/Day03/分支结构.md b/Day01-15/03.分支结构.md similarity index 85% rename from Day01-15/Day03/分支结构.md rename to Day01-15/03.分支结构.md index 017363e..099f25a 100644 --- a/Day01-15/Day03/分支结构.md +++ b/Day01-15/03.分支结构.md @@ -1,4 +1,4 @@ -## Day03 - 分支结构 +## 分支结构 ### 分支结构的应用场景 @@ -31,7 +31,7 @@ else: 当然如果要构造出更多的分支,可以使用`if…elif…else…`结构,例如下面的分段函数求值。 -$$f(x)=\begin{cases} 3x-5&\text{(x>1)}\\x+2&\text{(-1}\leq\text{x}\leq\text{1)}\\5x+3&\text {(x<-1)}\end{cases}$$ +![$$f(x)=\begin{cases} 3x-5&\text{(x>1)}\\x+2&\text{(-1}\leq\text{x}\leq\text{1)}\\5x+3&\text {(x<-1)}\end{cases}$$](./res/formula_1.png) ```Python """ @@ -79,7 +79,7 @@ else: print('f(%.2f) = %.2f' % (x, y)) ``` -> **说明:**大家可以自己感受一下这两种写法到底是哪一种更好。在之前我们提到的Python之禅中有这么一句话“Flat is better than nested.”,之所以提出这个观点是因为嵌套结构的嵌套层次多了之后会严重的影响代码的可读性,如果可以使用扁平化的结构就不要去用嵌套,因此之前的写法是更好的做法。 +> **说明:** 大家可以自己感受一下这两种写法到底是哪一种更好。在之前我们提到的Python之禅中有这么一句话“Flat is better than nested.”,之所以提出这个观点是因为嵌套结构的嵌套层次多了之后会严重的影响代码的可读性,如果可以使用扁平化的结构就不要去用嵌套,因此之前的写法是更好的做法。 ### 练习 @@ -130,7 +130,7 @@ else: result = '讲冷笑话' print(result) ``` -> **说明:**上面的代码中使用了random模块的randint函数生成指定范围的随机数来模拟掷骰子。 +> **说明:** 上面的代码中使用了random模块的randint函数生成指定范围的随机数来模拟掷骰子。 #### 练习3:百分制成绩转等级制 @@ -184,7 +184,7 @@ if a + b > c and a + c > b and b + c > a: else: print('不能构成三角形') ``` -> **说明:**上面的代码中使用了`math`模块的`sqrt`函数来计算平方根。用边长计算三角形面积的公式叫做[海伦公式](https://zh.wikipedia.org/zh-hans/海伦公式)。 +> **说明:** 上面的代码中使用了`math`模块的`sqrt`函数来计算平方根。用边长计算三角形面积的公式叫做[海伦公式](https://zh.wikipedia.org/zh-hans/海伦公式)。 #### 练习5:个人所得税计算器。 @@ -227,4 +227,4 @@ tax = abs(diff * rate - deduction) print('个人所得税: ¥%.2f元' % tax) print('实际到手收入: ¥%.2f元' % (diff + 3500 - tax)) ``` ->**说明:**上面的代码中使用了Python内置的`abs()`函数取绝对值来处理`-0`的问题。 +>**说明:** 上面的代码中使用了Python内置的`abs()`函数取绝对值来处理`-0`的问题。 diff --git a/Day01-15/Day04/循环结构.md b/Day01-15/04.循环结构.md similarity index 91% rename from Day01-15/Day04/循环结构.md rename to Day01-15/04.循环结构.md index c3adf99..4c0eda9 100644 --- a/Day01-15/Day04/循环结构.md +++ b/Day01-15/04.循环结构.md @@ -1,4 +1,4 @@ -## Day04 - 循环结构 +## 循环结构 ### 循环结构的应用场景 @@ -6,7 +6,7 @@ ### for-in循环 -如果明确的知道循环执行的次数或者是要对一个容器进行迭代(后面会讲到),那么我们推荐使用`for-in`循环,例如下面代码中计算$\sum_{n=1}^{100}n$。 +如果明确的知道循环执行的次数或者是要对一个容器进行迭代(后面会讲到),那么我们推荐使用`for-in`循环,例如下面代码中计算![$\sum_{n=1}^{100}n$](./res/formula_2.png)。 ```Python """ @@ -94,7 +94,7 @@ if counter > 7: print('你的智商余额明显不足') ``` -> **说明:**上面的代码中使用了`break`关键字来提前终止循环,需要注意的是`break`只能终止它所在的那个循环,这一点在使用嵌套的循环结构(下面会讲到)需要引起注意。除了`break`之外,还有另一个关键字是`continue`,它可以用来放弃本次循环后续的代码直接让循环进入下一轮。 +> **说明:** 上面的代码中使用了`break`关键字来提前终止循环,需要注意的是`break`只能终止它所在的那个循环,这一点在使用嵌套的循环结构(下面会讲到)需要引起注意。除了`break`之外,还有另一个关键字是`continue`,它可以用来放弃本次循环后续的代码直接让循环进入下一轮。 和分支结构一样,循环结构也是可以嵌套的,也就是说在循环中还可以构造循环结构。下面的例子演示了如何通过嵌套的循环来输出一个九九乘法表。 diff --git a/Day01-15/Day05/总结和练习.md b/Day01-15/05.构造程序逻辑.md similarity index 90% rename from Day01-15/Day05/总结和练习.md rename to Day01-15/05.构造程序逻辑.md index 86baa19..755a99f 100644 --- a/Day01-15/Day05/总结和练习.md +++ b/Day01-15/05.构造程序逻辑.md @@ -1,4 +1,4 @@ -## 练习 +## 构造程序逻辑 ### 练习清单 diff --git a/Day01-15/Day06/函数和模块的使用.md b/Day01-15/06.函数和模块的使用.md similarity index 93% rename from Day01-15/Day06/函数和模块的使用.md rename to Day01-15/06.函数和模块的使用.md index 202b9e7..5d65cab 100644 --- a/Day01-15/Day06/函数和模块的使用.md +++ b/Day01-15/06.函数和模块的使用.md @@ -2,11 +2,11 @@ 在讲解本章节的内容之前,我们先来研究一道数学题,请说出下面的方程有多少组正整数解。 -$$x_1 + x_2 + x_3 + x_4 = 8$$ +![$$x_1 + x_2 + x_3 + x_4 = 8$$](./res/formula_3.png) 事实上,上面的问题等同于将8个苹果分成四组每组至少一个苹果有多少种方案。想到这一点问题的答案就呼之欲出了。 -$$C_M^N =\frac{M!}{N!(M-N)!}, \text{(M=7, N=3)} $$ +![$$C_M^N =\frac{M!}{N!(M-N)!}, \text{(M=7, N=3)} $$](./res/formula_4.png) 可以用Python的程序来计算出这个值,代码如下所示。 @@ -59,7 +59,7 @@ n = int(input('n = ')) print(factorial(m) // factorial(n) // factorial(m - n)) ``` -> **说明:**Python的math模块中其实已经有一个factorial函数了,事实上要计算阶乘可以直接使用这个现成的函数而不用自己定义。下面例子中的某些函数其实Python中也是内置了,我们这里是为了讲解函数的定义和使用才把它们又实现了一遍,实际开发中不建议做这种低级的重复性的工作。 +> **说明:** Python的math模块中其实已经有一个factorial函数了,事实上要计算阶乘可以直接使用这个现成的函数而不用自己定义。下面例子中的某些函数其实Python中也是内置了,我们这里是为了讲解函数的定义和使用才把它们又实现了一遍,实际开发中不建议做这种低级的重复性的工作。 ### 函数的参数 @@ -337,7 +337,7 @@ if __name__ == '__main__': 在实际开发中,我们应该尽量减少对全局变量的使用,因为全局变量的作用域和影响过于广泛,可能会发生意料之外的修改和使用,除此之外全局变量比局部变量拥有更长的生命周期,可能导致对象占用的内存长时间无法被[垃圾回收](https://zh.wikipedia.org/wiki/%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6_(%E8%A8%88%E7%AE%97%E6%A9%9F%E7%A7%91%E5%AD%B8))。事实上,减少对全局变量的使用,也是降低代码之间耦合度的一个重要举措,同时也是对[迪米特法则](https://zh.wikipedia.org/zh-hans/%E5%BE%97%E5%A2%A8%E5%BF%92%E8%80%B3%E5%AE%9A%E5%BE%8B)的践行。减少全局变量的使用就意味着我们应该尽量让变量的作用域在函数的内部,但是如果我们希望将一个局部变量的生命周期延长,使其在函数调用结束后依然可以访问,这时候就需要使用[闭包](https://zh.wikipedia.org/wiki/%E9%97%AD%E5%8C%85_(%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%A7%91%E5%AD%A6)),这个我们在后续的内容中进行讲解。 -> **说明**:很多人经常会将“闭包”一词和[“匿名函数”](https://zh.wikipedia.org/wiki/%E5%8C%BF%E5%90%8D%E5%87%BD%E6%95%B0)混为一谈,但实际上它们是不同的概念,如果想提前了解这个概念,推荐看看[维基百科](https://zh.wikipedia.org/wiki/)或者[知乎](https://www.zhihu.com/)上对这个概念的讨论。 +> **说明:** 很多人经常会将“闭包”一词和[“匿名函数”](https://zh.wikipedia.org/wiki/%E5%8C%BF%E5%90%8D%E5%87%BD%E6%95%B0)混为一谈,但实际上它们是不同的概念,如果想提前了解这个概念,推荐看看[维基百科](https://zh.wikipedia.org/wiki/)或者[知乎](https://www.zhihu.com/)上对这个概念的讨论。 说了那么多,其实结论很简单,从现在开始我们可以将Python代码按照下面的格式进行书写,这一点点的改进其实就是在我们理解了函数和作用域的基础上跨出的巨大的一步。 diff --git a/Day01-15/Day07/字符串和常用数据结构.md b/Day01-15/07.字符串和常用数据结构.md similarity index 94% rename from Day01-15/Day07/字符串和常用数据结构.md rename to Day01-15/07.字符串和常用数据结构.md index 5dff114..9aa6030 100644 --- a/Day01-15/Day07/字符串和常用数据结构.md +++ b/Day01-15/07.字符串和常用数据结构.md @@ -2,7 +2,7 @@ ### 使用字符串 -第二次世界大战促使了现代电子计算机的诞生,当初的想法很简单,就是用计算机来计算导弹的弹道,因此在计算机刚刚诞生的那个年代,计算机处理的信息主要是数值,而世界上的第一台电子计算机ENIAC每秒钟能够完成约5000次浮点运算。随着时间的推移,虽然对数值运算仍然是计算机日常工作中最为重要的事情之一,但是今天的计算机处理得更多的数据都是以文本信息的方式存在的,而Python表示文本信息的方式我们在很早以前就说过了,那就是字符串类型。所谓**字符串**,就是由零个或多个字符组成的有限序列,一般记为[$${\displaystyle s=a_{1}a_{2}\dots a_{n}(0\leq n \leq \infty)}$$](https://wikimedia.org/api/rest_v1/media/math/render/svg/e29bf631b090323edd6889f810e6cff29538b161)。 +第二次世界大战促使了现代电子计算机的诞生,当初的想法很简单,就是用计算机来计算导弹的弹道,因此在计算机刚刚诞生的那个年代,计算机处理的信息主要是数值,而世界上的第一台电子计算机ENIAC每秒钟能够完成约5000次浮点运算。随着时间的推移,虽然对数值运算仍然是计算机日常工作中最为重要的事情之一,但是今天的计算机处理得更多的数据都是以文本信息的方式存在的,而Python表示文本信息的方式我们在很早以前就说过了,那就是字符串类型。所谓**字符串**,就是由零个或多个字符组成的有限序列,一般记为![$${\displaystyle s=a_{1}a_{2}\dots a_{n}(0\leq n \leq \infty)}$$](./res/formula_5.png)。 我们可以通过下面的代码来了解字符串的使用。 @@ -183,11 +183,11 @@ if __name__ == '__main__': 除了上面提到的生成器语法,Python中还有另外一种定义生成器的方式,就是通过`yield`关键字将一个普通函数改造成生成器函数。下面的代码演示了如何实现一个生成[斐波拉切数列](https://zh.wikipedia.org/wiki/%E6%96%90%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B0%E5%88%97)的生成器。所谓斐波拉切数列可以通过下面[递归](https://zh.wikipedia.org/wiki/%E9%80%92%E5%BD%92)的方法来进行定义: -$${\displaystyle F_{0}=0}$$ +![$${\displaystyle F_{0}=0}$$](./res/formula_6.png) -$${\displaystyle F_{1}=1}$$ +![$${\displaystyle F_{1}=1}$$](./res/formula_7.png) -$${\displaystyle F_{n}=F_{n-1}+F_{n-2}}({n}\geq{2})$$ +![$${\displaystyle F_{n}=F_{n-1}+F_{n-2}}({n}\geq{2})$$](./res/formula_8.png) ![](./res/fibonacci-blocks.png) @@ -307,7 +307,7 @@ if __name__ == '__main__': main() ``` -> **说明**:Python中允许通过一些特殊的方法来为某种类型或数据结构自定义运算符(后面的章节中会讲到),上面的代码中我们对集合进行运算的时候可以调用集合对象的方法,也可以直接使用对应的运算符,例如`&`运算符跟intersection方法的作用就是一样的,但是使用运算符让代码更加直观。 +> **说明:** Python中允许通过一些特殊的方法来为某种类型或数据结构自定义运算符(后面的章节中会讲到),上面的代码中我们对集合进行运算的时候可以调用集合对象的方法,也可以直接使用对应的运算符,例如`&`运算符跟intersection方法的作用就是一样的,但是使用运算符让代码更加直观。 ### 使用字典 @@ -530,7 +530,7 @@ if __name__ == '__main__': main() ``` -> **说明**:上面使用random模块的sample函数来实现从列表中选择不重复的n个元素。 +> **说明:** 上面使用random模块的sample函数来实现从列表中选择不重复的n个元素。 #### 综合案例2:[约瑟夫环问题](https://zh.wikipedia.org/wiki/%E7%BA%A6%E7%91%9F%E5%A4%AB%E6%96%AF%E9%97%AE%E9%A2%98) @@ -609,4 +609,4 @@ if __name__ == '__main__': main() ``` ->**说明**:最后这个案例来自[《Python编程快速上手:让繁琐工作自动化》](https://item.jd.com/11943853.html)一书(这本书对有编程基础想迅速使用Python将日常工作自动化的人来说还是不错的选择),对代码做了一点点的调整。 \ No newline at end of file +>**说明:** 最后这个案例来自[《Python编程快速上手:让繁琐工作自动化》](https://item.jd.com/11943853.html)一书(这本书对有编程基础想迅速使用Python将日常工作自动化的人来说还是不错的选择),对代码做了一点点的调整。 \ No newline at end of file diff --git a/Day01-15/Day08/面向对象编程基础.md b/Day01-15/08.面向对象编程基础.md similarity index 91% rename from Day01-15/Day08/面向对象编程基础.md rename to Day01-15/08.面向对象编程基础.md index b4f4856..9769ffb 100644 --- a/Day01-15/Day08/面向对象编程基础.md +++ b/Day01-15/08.面向对象编程基础.md @@ -8,13 +8,13 @@ ![](./res/oop-zhihu.png) -> **说明**:以上的内容来自于网络,不代表作者本人的观点和看法,与作者本人立场无关,相关责任不由作者承担。 +> **说明:** 以上的内容来自于网络,不代表作者本人的观点和看法,与作者本人立场无关,相关责任不由作者承担。 之前我们说过“程序是指令的集合”,我们在程序中书写的语句在执行时会变成一条或多条指令然后由CPU去执行。当然为了简化程序的设计,我们引入了函数的概念,把相对独立且经常重复使用的代码放置到函数中,在需要使用这些功能的时候只要调用函数即可;如果一个函数的功能过于复杂和臃肿,我们又可以进一步将函数继续切分为子函数来降低系统的复杂性。但是说了这么多,不知道大家是否发现,所谓编程就是程序员按照计算机的工作方式控制计算机完成各种任务。但是,计算机的工作方式与正常人类的思维模式是不同的,如果编程就必须得抛弃人类正常的思维方式去迎合计算机,编程的乐趣就少了很多,“每个人都应该学习编程”这样的豪言壮语就只能说说而已。当然,这些还不是最重要的,最重要的是当我们需要开发一个复杂的系统时,代码的复杂性会让开发和维护工作都变得举步维艰,所以在上世纪60年代末期,“[软件危机](https://zh.wikipedia.org/wiki/%E8%BD%AF%E4%BB%B6%E5%8D%B1%E6%9C%BA)”、“[软件工程](https://zh.wikipedia.org/wiki/%E8%BD%AF%E4%BB%B6%E5%B7%A5%E7%A8%8B)”等一系列的概念开始在行业中出现。 当然,程序员圈子内的人都知道,现实中并没有解决上面所说的这些问题的“[银弹](https://zh.wikipedia.org/wiki/%E6%B2%A1%E6%9C%89%E9%93%B6%E5%BC%B9)”,真正让软件开发者看到希望的是上世纪70年代诞生的[Smalltalk](https://zh.wikipedia.org/wiki/Smalltalk)编程语言中引入的面向对象的编程思想(面向对象编程的雏形可以追溯到更早期的[Simula](https://zh.wikipedia.org/wiki/Simula)语言)。按照这种编程理念,程序中的数据和操作数据的函数是一个逻辑上的整体,我们称之为“对象”,而我们解决问题的方式就是创建出需要的对象并向对象发出各种各样的消息,多个对象的协同工作最终可以让我们构造出复杂的系统来解决现实中的问题。 -> **说明**:当然面向对象也不是解决软件开发中所有问题的最后的“银弹”,所以今天的高级程序设计语言几乎都提供了对多种编程范式的支持,Python也不例外。 +> **说明:** 当然面向对象也不是解决软件开发中所有问题的最后的“银弹”,所以今天的高级程序设计语言几乎都提供了对多种编程范式的支持,Python也不例外。 ### 类和对象 @@ -39,15 +39,15 @@ class Student(object): print('%s正在学习%s.' % (self.name, course_name)) # PEP 8要求标识符的名字用全小写多个单词用下划线连接 - # 但是很多程序员和公司更倾向于使用驼峰命名法(驼峰标识) - def watch_av(self): + # 但是部分程序员和公司更倾向于使用驼峰命名法(驼峰标识) + def watch_movie(self): if self.age < 18: print('%s只能观看《熊出没》.' % self.name) else: - print('%s正在观看岛国爱情动作片.' % self.name + print('%s正在观看岛国爱情大电影.' % self.name) ``` -> **说明**:写在类中的函数,我们通常称之为(对象的)方法,这些方法就是对象可以接收的消息。 +> **说明:** 写在类中的函数,我们通常称之为(对象的)方法,这些方法就是对象可以接收的消息。 ### 创建和使用对象 @@ -60,10 +60,10 @@ def main(): # 给对象发study消息 stu1.study('Python程序设计') # 给对象发watch_av消息 - stu1.watch_av() + stu1.watch_movie() stu2 = Student('王大锤', 15) stu2.study('思想品德') - stu2.watch_av() + stu2.watch_movie() if __name__ == '__main__': @@ -237,4 +237,4 @@ if __name__ == '__main__': main() ``` -> **说明**:本章中的插图来自于Grady Booch等著作的[《面向对象分析与设计》](https://item.jd.com/20476561918.html)一书,该书是讲解面向对象编程的经典著作,有兴趣的读者可以购买和阅读这本书来了解更多的面向对象的相关知识。 \ No newline at end of file +> **说明:** 本章中的插图来自于Grady Booch等著作的[《面向对象分析与设计》](https://item.jd.com/20476561918.html)一书,该书是讲解面向对象编程的经典著作,有兴趣的读者可以购买和阅读这本书来了解更多的面向对象的相关知识。 \ No newline at end of file diff --git a/Day01-15/Day09/面向对象进阶.md b/Day01-15/09.面向对象进阶.md similarity index 99% rename from Day01-15/Day09/面向对象进阶.md rename to Day01-15/09.面向对象进阶.md index 168884d..956d910 100644 --- a/Day01-15/Day09/面向对象进阶.md +++ b/Day01-15/09.面向对象进阶.md @@ -636,7 +636,7 @@ if __name__ == '__main__': main() ``` ->**说明**:大家可以自己尝试在上面代码的基础上写一个简单的扑克游戏,例如21点(Black Jack),游戏的规则可以自己在网上找一找。 +>**说明:** 大家可以自己尝试在上面代码的基础上写一个简单的扑克游戏,例如21点(Black Jack),游戏的规则可以自己在网上找一找。 #### 案例3:工资结算系统 diff --git a/Day01-15/Day10/图形用户界面和游戏开发.md b/Day01-15/10.图形用户界面和游戏开发.md similarity index 99% rename from Day01-15/Day10/图形用户界面和游戏开发.md rename to Day01-15/10.图形用户界面和游戏开发.md index 6dfada7..ae65624 100644 --- a/Day01-15/Day10/图形用户界面和游戏开发.md +++ b/Day01-15/10.图形用户界面和游戏开发.md @@ -128,7 +128,7 @@ if __name__ == '__main__': main() ``` -####加载图像 +#### 加载图像 如果需要直接加载图像到窗口上,可以使用pygame中image模块的函数来加载图像,再通过之前获得的窗口对象的`blit`方法渲染图像,代码如下所示。 @@ -164,7 +164,7 @@ if __name__ == '__main__': main() ``` -####实现动画效果 +#### 实现动画效果 说到[动画](https://zh.wikipedia.org/wiki/%E5%8A%A8%E7%94%BB)这个词大家都不会陌生,事实上要实现动画效果,本身的原理也非常简单,就是将不连续的图片连续的播放,只要每秒钟达到了一定的帧数,那么就可以做出比较流畅的动画效果。如果要让上面代码中的小球动起来,可以将小球的位置用变量来表示,并在循环中修改小球的位置再刷新整个窗口即可。 diff --git a/Day01-15/Day11/文件和异常.md b/Day01-15/11.文件和异常.md similarity index 97% rename from Day01-15/Day11/文件和异常.md rename to Day01-15/11.文件和异常.md index 924aef9..3fca86d 100644 --- a/Day01-15/Day11/文件和异常.md +++ b/Day01-15/11.文件和异常.md @@ -103,7 +103,7 @@ if __name__ == '__main__': main() ``` -要将文本信息写入文件文件也非常简单,在使用`open`函数时指定好文件名并将文件模式设置为`'w'`即可。注意如果需要对文件内容进行追加式写入,应该将模式设置为`'a'`。如果要写入的文件不存在会自动创建文件而不是引发异常。下面的例子演示了如何将1-9999直接的素数分别写入三个文件中(1-99之间的素数保存在a.txt中,100-999之间的素数保存在b.txt中,1000-9999之间的素数保存在c.txt中)。 +要将文本信息写入文件文件也非常简单,在使用`open`函数时指定好文件名并将文件模式设置为`'w'`即可。注意如果需要对文件内容进行追加式写入,应该将模式设置为`'a'`。如果要写入的文件不存在会自动创建文件而不是引发异常。下面的例子演示了如何将1-9999之间的素数分别写入三个文件中(1-99之间的素数保存在a.txt中,100-999之间的素数保存在b.txt中,1000-9999之间的素数保存在c.txt中)。 ```Python from math import sqrt @@ -174,14 +174,14 @@ if __name__ == '__main__': ```JSON { - 'name': '骆昊', - 'age': 38, - 'qq': 957658, - 'friends': ['王大锤', '白元芳'], - 'cars': [ - {'brand': 'BYD', 'max_speed': 180}, - {'brand': 'Audi', 'max_speed': 280}, - {'brand': 'Benz', 'max_speed': 320} + "name": "骆昊", + "age": 38, + "qq": 957658, + "friends": ["王大锤", "白元芳"], + "cars": [ + {"brand": "BYD", "max_speed": 180}, + {"brand": "Audi", "max_speed": 280}, + {"brand": "Benz", "max_speed": 320} ] } ``` diff --git a/Day01-15/Day12/字符串和正则表达式.md b/Day01-15/12.字符串和正则表达式.md similarity index 85% rename from Day01-15/Day12/字符串和正则表达式.md rename to Day01-15/12.字符串和正则表达式.md index 418d039..b8b9ae9 100644 --- a/Day01-15/Day12/字符串和正则表达式.md +++ b/Day01-15/12.字符串和正则表达式.md @@ -32,7 +32,7 @@ | \| | 分支 | foo\|bar | 可以匹配foo或者bar | | (?#) | 注释 | | | | (exp) | 匹配exp并捕获到自动命名的组中 | | | -| (?<name>exp) | 匹配exp并捕获到名为name的组中 | | | +| (? <name>exp) | 匹配exp并捕获到名为name的组中 | | | | (?:exp) | 匹配exp但是不捕获匹配的文本 | | | | (?=exp) | 匹配exp前面的位置 | \\b\\w+(?=ing) | 可以匹配I'm dancing中的danc | | (?<=exp) | 匹配exp后面的位置 | (?<=\\bdanc)\\w+\\b | 可以匹配I love dancing and reading中的第一个ing | @@ -44,7 +44,7 @@ | {M,N}? | 重复M到N次,但尽可能少重复 | | | | {M,}? | 重复M次以上,但尽可能少重复 | | | -> **说明:**如果需要匹配的字符是正则表达式中的特殊字符,那么可以使用\\进行转义处理,例如想匹配小数点可以写成\\.就可以了,因为直接写.会匹配任意字符;同理,想匹配圆括号必须写成\\(和\\),否则圆括号被视为正则表达式中的分组。 +> **说明:** 如果需要匹配的字符是正则表达式中的特殊字符,那么可以使用\\进行转义处理,例如想匹配小数点可以写成\\.就可以了,因为直接写.会匹配任意字符;同理,想匹配圆括号必须写成\\(和\\),否则圆括号被视为正则表达式中的分组。 ### Python对正则表达式的支持 @@ -64,7 +64,7 @@ Python提供了re模块来支持正则表达式相关操作,下面是re模块 | re.I / re.IGNORECASE | 忽略大小写匹配标记 | | re.M / re.MULTILINE | 多行匹配标记 | -> **说明:**上面提到的re模块中的这些函数,实际开发中也可以用正则表达式对象的方法替代对这些函数的使用,如果一个正则表达式需要重复的使用,那么先通过compile函数编译正则表达式并创建出正则表达式对象无疑是更为明智的选择。 +> **说明:** 上面提到的re模块中的这些函数,实际开发中也可以用正则表达式对象的方法替代对这些函数的使用,如果一个正则表达式需要重复的使用,那么先通过compile函数编译正则表达式并创建出正则表达式对象无疑是更为明智的选择。 下面我们通过一系列的例子来告诉大家在Python中如何使用正则表达式。 @@ -98,7 +98,7 @@ if __name__ == '__main__': main() ``` -> **提示**:上面在书写正则表达式时使用了“原始字符串”的写法(在字符串前面加上了r),所谓“原始字符串”就是字符串中的每个字符都是它原始的意义,说得更直接一点就是字符串中没有所谓的转义字符啦。因为正则表达式中有很多元字符和需要进行转义的地方,如果不使用原始字符串就需要将反斜杠写作\\\\,例如表示数字的\\d得书写成\\\\d,这样不仅写起来不方便,阅读的时候也会很吃力。 +> **提示:** 上面在书写正则表达式时使用了“原始字符串”的写法(在字符串前面加上了r),所谓“原始字符串”就是字符串中的每个字符都是它原始的意义,说得更直接一点就是字符串中没有所谓的转义字符啦。因为正则表达式中有很多元字符和需要进行转义的地方,如果不使用原始字符串就需要将反斜杠写作\\\\,例如表示数字的\\d得书写成\\\\d,这样不仅写起来不方便,阅读的时候也会很吃力。 #### 例子2:从一段文字中提取出国内手机号码。 @@ -136,7 +136,7 @@ if __name__ == '__main__': main() ``` -> **说明**:上面匹配国内手机号的正则表达式并不够好,因为像14开头的号码只有145或147,而上面的正则表达式并没有考虑这种情况,要匹配国内手机号,更好的正则表达式的写法是:`(?<=\D)(1[38]\d{9}|14[57]\d{8}|15[0-35-9]\d{8}|17[678]\d{8})(?=\D)`,国内最近好像有19和16开头的手机号了,但是这个暂时不在我们考虑之列。 +> **说明:** 上面匹配国内手机号的正则表达式并不够好,因为像14开头的号码只有145或147,而上面的正则表达式并没有考虑这种情况,要匹配国内手机号,更好的正则表达式的写法是:`(?<=\D)(1[38]\d{9}|14[57]\d{8}|15[0-35-9]\d{8}|17[678]\d{8})(?=\D)`,国内最近好像有19和16开头的手机号了,但是这个暂时不在我们考虑之列。 #### 例子3:替换字符串中的不良内容 @@ -155,7 +155,7 @@ if __name__ == '__main__': main() ``` -> **说明**:re模块的正则表达式相关函数中都有一个flags参数,它代表了正则表达式的匹配标记,可以通过该标记来指定匹配时是否忽略大小写、是否进行多行匹配、是否显示调试信息等。如果需要为flags参数指定多个值,可以使用[按位或运算符](http://www.runoob.com/python/python-operators.html#ysf5)进行叠加,如`flags=re.I | re.M`。 +> **说明:** re模块的正则表达式相关函数中都有一个flags参数,它代表了正则表达式的匹配标记,可以通过该标记来指定匹配时是否忽略大小写、是否进行多行匹配、是否显示调试信息等。如果需要为flags参数指定多个值,可以使用[按位或运算符](http://www.runoob.com/python/python-operators.html#ysf5)进行叠加,如`flags=re.I | re.M`。 #### 例子4:拆分长字符串 diff --git a/Day01-15/Day13/进程和线程.md b/Day01-15/13.进程和线程.md similarity index 98% rename from Day01-15/Day13/进程和线程.md rename to Day01-15/13.进程和线程.md index c1d2093..befdc4c 100644 --- a/Day01-15/Day13/进程和线程.md +++ b/Day01-15/13.进程和线程.md @@ -320,7 +320,7 @@ if __name__ == '__main__': 除了计算密集型任务,其他的涉及到网络、存储介质I/O的任务都可以视为I/O密集型任务,这类任务的特点是CPU消耗很少,任务的大部分时间都在等待I/O操作完成(因为I/O的速度远远低于CPU和内存的速度)。对于I/O密集型任务,如果启动多任务,就可以减少I/O等待时间从而让CPU高效率的运转。有一大类的任务都属于I/O密集型任务,这其中包括了我们很快会涉及到的网络应用和Web应用。 -> **说明:**上面的内容和例子来自于[廖雪峰官方网站的《Python教程》](https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000),因为对作者文中的某些观点持有不同的看法,对原文的文字描述做了适当的调整。 +> **说明:** 上面的内容和例子来自于[廖雪峰官方网站的《Python教程》](https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000),因为对作者文中的某些观点持有不同的看法,对原文的文字描述做了适当的调整。 ### 单线程+异步I/O diff --git a/Day01-15/Day14-A/网络编程入门.md b/Day01-15/14.网络编程入门和网络应用开发.md similarity index 75% rename from Day01-15/Day14-A/网络编程入门.md rename to Day01-15/14.网络编程入门和网络应用开发.md index a466865..27a6cd5 100644 --- a/Day01-15/Day14-A/网络编程入门.md +++ b/Day01-15/14.网络编程入门和网络应用开发.md @@ -70,9 +70,9 @@ JSON的例子: ```JSON { - 'from': 'Alice', - 'to': 'Bob', - 'content': 'Will you marry me?' + "from": "Alice", + "to": "Bob", + "content": "Will you marry me?" } ``` @@ -291,9 +291,129 @@ if __name__ == '__main__': 在这个案例中,我们使用了JSON作为数据传输的格式(通过JSON格式对传输的数据进行了序列化和反序列化的操作),但是JSON并不能携带二进制数据,因此对图片的二进制数据进行了Base64编码的处理。Base64是一种用64个字符表示所有二进制数据的编码方式,通过将二进制数据每6位一组的方式重新组织,刚好可以使用0~9的数字、大小写字母以及“+”和“/”总共64个字符表示从`000000`到`111111`的64种状态。[维基百科](https://zh.wikipedia.org/wiki/Base64)上有关于Base64编码的详细讲解,不熟悉Base64的读者可以自行阅读。 -> **说明**:上面的代码主要为了讲解网络编程的相关内容因此并没有对异常状况进行处理,请读者自行添加异常处理代码来增强程序的健壮性。 +> **说明:** 上面的代码主要为了讲解网络编程的相关内容因此并没有对异常状况进行处理,请读者自行添加异常处理代码来增强程序的健壮性。 #### UDP套接字 传输层除了有可靠的传输协议TCP之外,还有一种非常轻便的传输协议叫做用户数据报协议,简称UDP。TCP和UDP都是提供端到端传输服务的协议,二者的差别就如同打电话和发短信的区别,后者不对传输的可靠性和可达性做出任何承诺从而避免了TCP中握手和重传的开销,所以在强调性能和而不是数据完整性的场景中(例如传输网络音视频数据),UDP可能是更好的选择。可能大家会注意到一个现象,就是在观看网络视频时,有时会出现卡顿,有时会出现花屏,这无非就是部分数据传丢或传错造成的。在Python中也可以使用UDP套接字来创建网络应用,对此我们不进行赘述,有兴趣的读者可以自行研究。 +### 网络应用开发 + +#### 发送电子邮件 + +在即时通信软件如此发达的今天,电子邮件仍然是互联网上使用最为广泛的应用之一,公司向应聘者发出录用通知、网站向用户发送一个激活账号的链接、银行向客户推广它们的理财产品等几乎都是通过电子邮件来完成的,而这些任务应该都是由程序自动完成的。 + +就像我们可以用HTTP(超文本传输协议)来访问一个网站一样,发送邮件要使用SMTP(简单邮件传输协议),SMTP也是一个建立在TCP(传输控制协议)提供的可靠数据传输服务的基础上的应用级协议,它规定了邮件的发送者如何跟发送邮件的服务器进行通信的细节,而Python中的smtplib模块将这些操作简化成了几个简单的函数。 + +下面的代码演示了如何在Python发送邮件。 + +```Python +from smtplib import SMTP +from email.header import Header +from email.mime.text import MIMEText + + +def main(): + # 请自行修改下面的邮件发送者和接收者 + sender = 'abcdefg@126.com' + receivers = ['uvwxyz@qq.com', 'uvwxyz@126.com'] + message = MIMEText('用Python发送邮件的示例代码.', 'plain', 'utf-8') + message['From'] = Header('王大锤', 'utf-8') + message['To'] = Header('骆昊', 'utf-8') + message['Subject'] = Header('示例代码实验邮件', 'utf-8') + smtper = SMTP('smtp.126.com') + # 请自行修改下面的登录口令 + smtper.login(sender, 'secretpass') + smtper.sendmail(sender, receivers, message.as_string()) + print('邮件发送完成!') + + +if __name__ == '__main__': + main() +``` + +如果要发送带有附件的邮件,那么可以按照下面的方式进行操作。 + +```Python +from smtplib import SMTP +from email.header import Header +from email.mime.text import MIMEText +from email.mime.image import MIMEImage +from email.mime.multipart import MIMEMultipart + +import urllib + + +def main(): + # 创建一个带附件的邮件消息对象 + message = MIMEMultipart() + + # 创建文本内容 + text_content = MIMEText('附件中有本月数据请查收', 'plain', 'utf-8') + message['Subject'] = Header('本月数据', 'utf-8') + # 将文本内容添加到邮件消息对象中 + message.attach(text_content) + + # 读取文件并将文件作为附件添加到邮件消息对象中 + with open('/Users/Hao/Desktop/hello.txt', 'rb') as f: + txt = MIMEText(f.read(), 'base64', 'utf-8') + txt['Content-Type'] = 'text/plain' + txt['Content-Disposition'] = 'attachment; filename=hello.txt' + message.attach(txt) + # 读取文件并将文件作为附件添加到邮件消息对象中 + with open('/Users/Hao/Desktop/汇总数据.xlsx', 'rb') as f: + xls = MIMEText(f.read(), 'base64', 'utf-8') + xls['Content-Type'] = 'application/vnd.ms-excel' + xls['Content-Disposition'] = 'attachment; filename=month-data.xlsx' + message.attach(xls) + + # 创建SMTP对象 + smtper = SMTP('smtp.126.com') + # 开启安全连接 + # smtper.starttls() + sender = 'abcdefg@126.com' + receivers = ['uvwxyz@qq.com'] + # 登录到SMTP服务器 + # 请注意此处不是使用密码而是邮件客户端授权码进行登录 + # 对此有疑问的读者可以联系自己使用的邮件服务器客服 + smtper.login(sender, 'secretpass') + # 发送邮件 + smtper.sendmail(sender, receivers, message.as_string()) + # 与邮件服务器断开连接 + smtper.quit() + print('发送完成!') + + +if __name__ == '__main__': + main() +``` + +#### 发送短信 + +发送短信也是项目中常见的功能,网站的注册码、验证码、营销信息基本上都是通过短信来发送给用户的。在下面的代码中我们使用了[互亿无线](http://www.ihuyi.com/)短信平台(该平台为注册用户提供了50条免费短信以及常用开发语言发送短信的demo,可以登录该网站并在用户自服务页面中对短信进行配置)提供的API接口实现了发送短信的服务,当然国内的短信平台很多,读者可以根据自己的需要进行选择(通常会考虑费用预算、短信达到率、使用的难易程度等指标),如果需要在商业项目中使用短信服务建议购买短信平台提供的套餐服务。 + +```Python +import urllib.parse +import http.client +import json + + +def main(): + host = "106.ihuyi.com" + sms_send_uri = "/webservice/sms.php?method=Submit" + # 下面的参数需要填入自己注册的账号和对应的密码 + params = urllib.parse.urlencode({'account': '你自己的账号', 'password' : '你自己的密码', 'content': '您的验证码是:147258。请不要把验证码泄露给其他人。', 'mobile': '接收者的手机号', 'format':'json' }) + print(params) + headers = {'Content-type': 'application/x-www-form-urlencoded', 'Accept': 'text/plain'} + conn = http.client.HTTPConnection(host, port=80, timeout=30) + conn.request('POST', sms_send_uri, params, headers) + response = conn.getresponse() + response_str = response.read() + jsonstr = response_str.decode('utf-8') + print(json.loads(jsonstr)) + conn.close() + + +if __name__ == '__main__': + main() +``` \ No newline at end of file diff --git a/Day01-15/Day15/图像和办公文档处理.md b/Day01-15/15.图像和办公文档处理.md similarity index 99% rename from Day01-15/Day15/图像和办公文档处理.md rename to Day01-15/15.图像和办公文档处理.md index d17be26..765afa0 100644 --- a/Day01-15/Day15/图像和办公文档处理.md +++ b/Day01-15/15.图像和办公文档处理.md @@ -76,7 +76,7 @@ Pillow中最为重要的是Image类,读取和处理图像都要通过这个类 ```Python >>> image = Image.open('./res/guido.png') - >>> image.rotata(180).show() + >>> image.rotate(180).show() >>> image.transpose(Image.FLIP_LEFT_RIGHT).show() ``` diff --git a/Day01-15/Day09/res/uml-example.gliffy b/Day01-15/Day09/res/uml-example.gliffy deleted file mode 100644 index 015163a..0000000 --- a/Day01-15/Day09/res/uml-example.gliffy +++ /dev/null @@ -1 +0,0 @@ -{"contentType":"application/gliffy+json","version":"1.1","metadata":{"title":"untitled","revision":0,"exportBorder":false},"embeddedResources":{"index":0,"resources":[]},"stage":{"objects":[{"x":211,"y":179.5,"rotation":0,"id":79,"uid":"com.gliffy.shape.basic.basic_v1.default.text","width":60,"height":28,"lockAspectRatio":false,"lockShape":false,"order":71,"graphic":{"type":"Text","Text":{"tid":null,"valign":"middle","overflow":"none","vposition":"none","hposition":"none","html":"

继承关系

","paddingLeft":2,"paddingRight":2,"paddingBottom":2,"paddingTop":2}},"children":null,"linkMap":[]},{"x":702,"y":249,"rotation":0,"id":78,"uid":"com.gliffy.shape.uml.uml_v1.default.generalization","width":100,"height":100,"lockAspectRatio":false,"lockShape":false,"order":70,"graphic":{"type":"Line","Line":{"strokeWidth":1,"strokeColor":"#000000","fillColor":"none","dashStyle":null,"startArrow":0,"endArrow":4,"startArrowRotation":"auto","endArrowRotation":"auto","ortho":true,"interpolationType":"linear","cornerRadius":null,"controlPath":[[82,-4],[50,-4],[50,-69.32485578727801],[18,-69.32485578727801]],"lockSegments":{}}},"children":null,"constraints":{"constraints":[],"startConstraint":{"type":"StartPositionConstraint","StartPositionConstraint":{"nodeId":70,"px":0,"py":0.5}},"endConstraint":{"type":"EndPositionConstraint","EndPositionConstraint":{"nodeId":32,"px":0.9999999999999998,"py":0.7071067811865475}}},"linkMap":[]},{"x":615,"y":70,"rotation":0,"id":77,"uid":"com.gliffy.shape.uml.uml_v1.default.generalization","width":100,"height":100,"lockAspectRatio":false,"lockShape":false,"order":69,"graphic":{"type":"Line","Line":{"strokeWidth":1,"strokeColor":"#000000","fillColor":"none","dashStyle":null,"startArrow":0,"endArrow":4,"startArrowRotation":"auto","endArrowRotation":"auto","ortho":true,"interpolationType":"linear","cornerRadius":null,"controlPath":[[169,6.75],[137,6.75],[137,70.32485578727798],[105,70.32485578727798]],"lockSegments":{}}},"children":null,"constraints":{"constraints":[],"startConstraint":{"type":"StartPositionConstraint","StartPositionConstraint":{"nodeId":63,"px":0,"py":0.5}},"endConstraint":{"type":"EndPositionConstraint","EndPositionConstraint":{"nodeId":32,"px":1,"py":0.29289321881345237}}},"linkMap":[]},{"x":228,"y":356,"rotation":0,"id":55,"uid":"com.gliffy.shape.uml.uml_v1.default.association","width":100,"height":100,"lockAspectRatio":false,"lockShape":false,"order":0,"graphic":{"type":"Line","Line":{"strokeWidth":1,"strokeColor":"#000000","fillColor":"none","dashStyle":null,"startArrow":0,"endArrow":0,"startArrowRotation":"auto","endArrowRotation":"auto","ortho":true,"interpolationType":"linear","cornerRadius":null,"controlPath":[[202,-46],[202,-22.666666666666686],[202,0.6666666666666856],[202,24]],"lockSegments":{}}},"children":null,"constraints":{"constraints":[],"startConstraint":{"type":"StartPositionConstraint","StartPositionConstraint":{"nodeId":24,"px":0.5,"py":1}},"endConstraint":{"type":"EndPositionConstraint","EndPositionConstraint":{"nodeId":48,"px":0.5,"py":0}}},"linkMap":[]},{"x":667,"y":225,"rotation":0,"id":47,"uid":"com.gliffy.shape.uml.uml_v1.default.aggregation","width":100,"height":100,"lockAspectRatio":false,"lockShape":false,"order":40,"graphic":{"type":"Line","Line":{"strokeWidth":1,"strokeColor":"#000000","fillColor":"none","dashStyle":null,"startArrow":0,"endArrow":5,"startArrowRotation":"auto","endArrowRotation":"auto","ortho":true,"interpolationType":"linear","cornerRadius":null,"controlPath":[[-17,-17.5],[-17,8.333333333333343],[-17,34.166666666666686],[-17,60]],"lockSegments":{}}},"children":null,"constraints":{"constraints":[],"startConstraint":{"type":"StartPositionConstraint","StartPositionConstraint":{"nodeId":32,"px":0.5,"py":1}},"endConstraint":{"type":"EndPositionConstraint","EndPositionConstraint":{"nodeId":56,"px":0.5,"py":0}}},"linkMap":[]},{"x":403,"y":390,"rotation":0,"id":39,"uid":"com.gliffy.shape.uml.uml_v1.default.dependency","width":100,"height":100,"lockAspectRatio":false,"lockShape":false,"order":39,"graphic":{"type":"Line","Line":{"strokeWidth":1,"strokeColor":"#000000","fillColor":"none","dashStyle":"8.0,2.0","startArrow":0,"endArrow":6,"startArrowRotation":"auto","endArrowRotation":"auto","ortho":true,"interpolationType":"linear","cornerRadius":null,"controlPath":[[97,-125],[137,-125],[137,-230],[177,-230]],"lockSegments":{}}},"children":null,"constraints":{"constraints":[],"startConstraint":{"type":"StartPositionConstraint","StartPositionConstraint":{"nodeId":24,"px":1,"py":0.5}},"endConstraint":{"type":"EndPositionConstraint","EndPositionConstraint":{"nodeId":32,"px":0,"py":0.5}}},"linkMap":[]},{"x":289,"y":219,"rotation":0,"id":31,"uid":"com.gliffy.shape.uml.uml_v1.default.generalization","width":100,"height":100,"lockAspectRatio":false,"lockShape":false,"order":31,"graphic":{"type":"Line","Line":{"strokeWidth":1,"strokeColor":"#000000","fillColor":"none","dashStyle":null,"startArrow":0,"endArrow":4,"startArrowRotation":"auto","endArrowRotation":"auto","ortho":true,"interpolationType":"linear","cornerRadius":null,"controlPath":[[141,1],[141,-124],[51,-124]],"lockSegments":{}}},"children":null,"constraints":{"constraints":[],"startConstraint":{"type":"StartPositionConstraint","StartPositionConstraint":{"nodeId":24,"px":0.5,"py":0}},"endConstraint":{"type":"EndPositionConstraint","EndPositionConstraint":{"nodeId":0,"px":1,"py":0.5}}},"linkMap":[]},{"x":325,"y":185,"rotation":0,"id":22,"uid":"com.gliffy.shape.uml.uml_v1.default.generalization","width":100,"height":100,"lockAspectRatio":false,"lockShape":false,"order":23,"graphic":{"type":"Line","Line":{"strokeWidth":1,"strokeColor":"#000000","fillColor":"none","dashStyle":null,"startArrow":0,"endArrow":4,"startArrowRotation":"auto","endArrowRotation":"auto","ortho":true,"interpolationType":"linear","cornerRadius":null,"controlPath":[[-55,35],[-55,11.666666666666657],[-55,-11.666666666666657],[-55,-35]],"lockSegments":{}}},"children":null,"constraints":{"constraints":[],"startConstraint":{"type":"StartPositionConstraint","StartPositionConstraint":{"nodeId":14,"px":0.5,"py":0}},"endConstraint":{"type":"EndPositionConstraint","EndPositionConstraint":{"nodeId":0,"px":0.5,"py":1}}},"linkMap":[]},{"x":98,"y":173,"rotation":0,"id":21,"uid":"com.gliffy.shape.uml.uml_v1.default.generalization","width":100,"height":100,"lockAspectRatio":false,"lockShape":false,"order":22,"graphic":{"type":"Line","Line":{"strokeWidth":1,"strokeColor":"#000000","fillColor":"none","dashStyle":null,"startArrow":0,"endArrow":4,"startArrowRotation":"auto","endArrowRotation":"auto","ortho":true,"interpolationType":"linear","cornerRadius":null,"controlPath":[[12,47],[12,-78],[102,-78]],"lockSegments":{}}},"children":null,"constraints":{"constraints":[],"startConstraint":{"type":"StartPositionConstraint","StartPositionConstraint":{"nodeId":7,"px":0.5,"py":0}},"endConstraint":{"type":"EndPositionConstraint","EndPositionConstraint":{"nodeId":0,"px":0,"py":0.5}}},"linkMap":[]},{"x":200,"y":220,"rotation":0,"id":14,"uid":"com.gliffy.shape.uml.uml_v1.default.class","width":140,"height":75,"lockAspectRatio":false,"lockShape":false,"order":15,"graphic":null,"children":[{"x":0,"y":0,"rotation":0,"id":15,"uid":null,"width":140,"height":18,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Shape","Shape":{"tid":"com.gliffy.stencil.rectangle.basic_v1","strokeWidth":2,"strokeColor":"#000000","fillColor":"#FFFFFF","gradient":false,"dropShadow":true,"state":0,"shadowX":4,"shadowY":4,"opacity":1}},"children":[{"x":0,"y":0,"rotation":0,"id":16,"uid":null,"width":140,"height":18,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Text","Text":{"tid":null,"valign":"top","overflow":"none","vposition":"none","hposition":"none","html":"

Teacher

","paddingLeft":2,"paddingRight":2,"paddingBottom":2,"paddingTop":2}},"children":null}],"constraints":{"constraints":[{"type":"HeightConstraint","HeightConstraint":{"isMin":false,"heightInfo":[{"id":16,"magnitude":1}],"growParent":true,"padding":0}}]}},{"x":0,"y":18,"rotation":0,"id":17,"uid":null,"width":140,"height":18,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Shape","Shape":{"tid":"com.gliffy.stencil.rectangle.basic_v1","strokeWidth":2,"strokeColor":"#000000","fillColor":"#FFFFFF","gradient":false,"dropShadow":true,"state":0,"shadowX":4,"shadowY":4,"opacity":1}},"children":[{"x":0,"y":0,"rotation":0,"id":18,"uid":null,"width":140,"height":18,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Text","Text":{"tid":null,"valign":"top","overflow":"none","vposition":"none","hposition":"none","html":"

title

","paddingLeft":2,"paddingRight":2,"paddingBottom":2,"paddingTop":2}},"children":null}],"constraints":{"constraints":[{"type":"PositionConstraint","PositionConstraint":{"nodeId":15,"px":0,"py":1}},{"type":"HeightConstraint","HeightConstraint":{"isMin":false,"heightInfo":[{"id":18,"magnitude":1}],"growParent":true,"padding":0}}]}},{"x":0,"y":36,"rotation":0,"id":19,"uid":null,"width":140,"height":39,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Shape","Shape":{"tid":"com.gliffy.stencil.rectangle.basic_v1","strokeWidth":2,"strokeColor":"#000000","fillColor":"#FFFFFF","gradient":false,"dropShadow":true,"state":0,"shadowX":4,"shadowY":4,"opacity":1}},"children":[{"x":0,"y":0,"rotation":0,"id":20,"uid":null,"width":140,"height":18,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Text","Text":{"tid":null,"valign":"top","overflow":"none","vposition":"none","hposition":"none","html":"

teach

","paddingLeft":2,"paddingRight":2,"paddingBottom":2,"paddingTop":2}},"children":null}],"constraints":{"constraints":[{"type":"HeightConstraint","HeightConstraint":{"isMin":false,"heightInfo":[{"id":14,"magnitude":1},{"id":15,"magnitude":-1},{"id":17,"magnitude":-1}],"growParent":false,"padding":0}},{"type":"PositionConstraint","PositionConstraint":{"nodeId":17,"px":0,"py":1}}]}}],"constraints":{"constraints":[{"type":"HeightConstraint","HeightConstraint":{"isMin":true,"heightInfo":[{"id":15,"magnitude":1},{"id":17,"magnitude":1},{"id":20,"magnitude":1}],"growParent":false,"padding":0}}]},"linkMap":[]},{"x":40,"y":220,"rotation":0,"id":7,"uid":"com.gliffy.shape.uml.uml_v1.default.class","width":140,"height":75,"lockAspectRatio":false,"lockShape":false,"order":8,"graphic":null,"children":[{"x":0,"y":0,"rotation":0,"id":8,"uid":null,"width":140,"height":18,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Shape","Shape":{"tid":"com.gliffy.stencil.rectangle.basic_v1","strokeWidth":2,"strokeColor":"#000000","fillColor":"#FFFFFF","gradient":false,"dropShadow":true,"state":0,"shadowX":4,"shadowY":4,"opacity":1}},"children":[{"x":0,"y":0,"rotation":0,"id":9,"uid":null,"width":140,"height":18,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Text","Text":{"tid":null,"valign":"top","overflow":"none","vposition":"none","hposition":"none","html":"

Student

","paddingLeft":2,"paddingRight":2,"paddingBottom":2,"paddingTop":2}},"children":null}],"constraints":{"constraints":[{"type":"HeightConstraint","HeightConstraint":{"isMin":false,"heightInfo":[{"id":9,"magnitude":1}],"growParent":true,"padding":0}}]}},{"x":0,"y":18,"rotation":0,"id":10,"uid":null,"width":140,"height":18,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Shape","Shape":{"tid":"com.gliffy.stencil.rectangle.basic_v1","strokeWidth":2,"strokeColor":"#000000","fillColor":"#FFFFFF","gradient":false,"dropShadow":true,"state":0,"shadowX":4,"shadowY":4,"opacity":1}},"children":[{"x":0,"y":0,"rotation":0,"id":11,"uid":null,"width":140,"height":18,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Text","Text":{"tid":null,"valign":"top","overflow":"none","vposition":"none","hposition":"none","html":"

grade

","paddingLeft":2,"paddingRight":2,"paddingBottom":2,"paddingTop":2}},"children":null}],"constraints":{"constraints":[{"type":"PositionConstraint","PositionConstraint":{"nodeId":8,"px":0,"py":1}},{"type":"HeightConstraint","HeightConstraint":{"isMin":false,"heightInfo":[{"id":11,"magnitude":1}],"growParent":true,"padding":0}}]}},{"x":0,"y":36,"rotation":0,"id":12,"uid":null,"width":140,"height":39,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Shape","Shape":{"tid":"com.gliffy.stencil.rectangle.basic_v1","strokeWidth":2,"strokeColor":"#000000","fillColor":"#FFFFFF","gradient":false,"dropShadow":true,"state":0,"shadowX":4,"shadowY":4,"opacity":1}},"children":[{"x":0,"y":0,"rotation":0,"id":13,"uid":null,"width":140,"height":18,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Text","Text":{"tid":null,"valign":"top","overflow":"none","vposition":"none","hposition":"none","html":"

study

","paddingLeft":2,"paddingRight":2,"paddingBottom":2,"paddingTop":2}},"children":null}],"constraints":{"constraints":[{"type":"HeightConstraint","HeightConstraint":{"isMin":false,"heightInfo":[{"id":7,"magnitude":1},{"id":8,"magnitude":-1},{"id":10,"magnitude":-1}],"growParent":false,"padding":0}},{"type":"PositionConstraint","PositionConstraint":{"nodeId":10,"px":0,"py":1}}]}}],"constraints":{"constraints":[{"type":"HeightConstraint","HeightConstraint":{"isMin":true,"heightInfo":[{"id":8,"magnitude":1},{"id":10,"magnitude":1},{"id":13,"magnitude":1}],"growParent":false,"padding":0}}]},"linkMap":[]},{"x":200,"y":40,"rotation":0,"id":0,"uid":"com.gliffy.shape.uml.uml_v1.default.class","width":140,"height":110,"lockAspectRatio":false,"lockShape":false,"order":1,"graphic":null,"children":[{"x":0,"y":0,"rotation":0,"id":1,"uid":null,"width":140,"height":18,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Shape","Shape":{"tid":"com.gliffy.stencil.rectangle.basic_v1","strokeWidth":2,"strokeColor":"#000000","fillColor":"#FFFFFF","gradient":false,"dropShadow":true,"state":0,"shadowX":4,"shadowY":4,"opacity":1}},"children":[{"x":0,"y":0,"rotation":0,"id":2,"uid":null,"width":140,"height":18,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Text","Text":{"tid":null,"valign":"top","overflow":"none","vposition":"none","hposition":"none","html":"

Person

","paddingLeft":2,"paddingRight":2,"paddingBottom":2,"paddingTop":2}},"children":null}],"constraints":{"constraints":[{"type":"HeightConstraint","HeightConstraint":{"isMin":false,"heightInfo":[{"id":2,"magnitude":1}],"growParent":true,"padding":0}}]}},{"x":0,"y":18,"rotation":0,"id":3,"uid":null,"width":140,"height":32,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Shape","Shape":{"tid":"com.gliffy.stencil.rectangle.basic_v1","strokeWidth":2,"strokeColor":"#000000","fillColor":"#FFFFFF","gradient":false,"dropShadow":true,"state":0,"shadowX":4,"shadowY":4,"opacity":1}},"children":[{"x":0,"y":0,"rotation":0,"id":4,"uid":null,"width":140,"height":32,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Text","Text":{"tid":null,"valign":"top","overflow":"none","vposition":"none","hposition":"none","html":"

name\n

age

","paddingLeft":2,"paddingRight":2,"paddingBottom":2,"paddingTop":2}},"children":null}],"constraints":{"constraints":[{"type":"PositionConstraint","PositionConstraint":{"nodeId":1,"px":0,"py":1}},{"type":"HeightConstraint","HeightConstraint":{"isMin":false,"heightInfo":[{"id":4,"magnitude":1}],"growParent":true,"padding":0}}]}},{"x":0,"y":50,"rotation":0,"id":5,"uid":null,"width":140,"height":60,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Shape","Shape":{"tid":"com.gliffy.stencil.rectangle.basic_v1","strokeWidth":2,"strokeColor":"#000000","fillColor":"#FFFFFF","gradient":false,"dropShadow":true,"state":0,"shadowX":4,"shadowY":4,"opacity":1}},"children":[{"x":0,"y":0,"rotation":0,"id":6,"uid":null,"width":140,"height":32,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Text","Text":{"tid":null,"valign":"top","overflow":"none","vposition":"none","hposition":"none","html":"

eat\n

play

","paddingLeft":2,"paddingRight":2,"paddingBottom":2,"paddingTop":2}},"children":null}],"constraints":{"constraints":[{"type":"HeightConstraint","HeightConstraint":{"isMin":false,"heightInfo":[{"id":0,"magnitude":1},{"id":1,"magnitude":-1},{"id":3,"magnitude":-1}],"growParent":false,"padding":0}},{"type":"PositionConstraint","PositionConstraint":{"nodeId":3,"px":0,"py":1}}]}}],"constraints":{"constraints":[{"type":"HeightConstraint","HeightConstraint":{"isMin":true,"heightInfo":[{"id":1,"magnitude":1},{"id":3,"magnitude":1},{"id":6,"magnitude":1}],"growParent":false,"padding":0}}]},"linkMap":[]},{"x":360,"y":220,"rotation":0,"id":24,"uid":"com.gliffy.shape.uml.uml_v1.default.class","width":140,"height":90,"lockAspectRatio":false,"lockShape":false,"order":24,"graphic":null,"children":[{"x":0,"y":0,"rotation":0,"id":25,"uid":null,"width":140,"height":18,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Shape","Shape":{"tid":"com.gliffy.stencil.rectangle.basic_v1","strokeWidth":2,"strokeColor":"#000000","fillColor":"#FFFFFF","gradient":false,"dropShadow":true,"state":0,"shadowX":4,"shadowY":4,"opacity":1}},"children":[{"x":0,"y":0,"rotation":0,"id":26,"uid":null,"width":140,"height":18,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Text","Text":{"tid":null,"valign":"top","overflow":"none","vposition":"none","hposition":"none","html":"

Driver

","paddingLeft":2,"paddingRight":2,"paddingBottom":2,"paddingTop":2}},"children":null}],"constraints":{"constraints":[{"type":"HeightConstraint","HeightConstraint":{"isMin":false,"heightInfo":[{"id":26,"magnitude":1}],"growParent":true,"padding":0}}]}},{"x":0,"y":18,"rotation":0,"id":27,"uid":null,"width":140,"height":32,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Shape","Shape":{"tid":"com.gliffy.stencil.rectangle.basic_v1","strokeWidth":2,"strokeColor":"#000000","fillColor":"#FFFFFF","gradient":false,"dropShadow":true,"state":0,"shadowX":4,"shadowY":4,"opacity":1}},"children":[{"x":0,"y":0,"rotation":0,"id":28,"uid":null,"width":140,"height":32,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Text","Text":{"tid":null,"valign":"top","overflow":"none","vposition":"none","hposition":"none","html":"

work_experience\n

license

","paddingLeft":2,"paddingRight":2,"paddingBottom":2,"paddingTop":2}},"children":null}],"constraints":{"constraints":[{"type":"PositionConstraint","PositionConstraint":{"nodeId":25,"px":0,"py":1}},{"type":"HeightConstraint","HeightConstraint":{"isMin":false,"heightInfo":[{"id":28,"magnitude":1}],"growParent":true,"padding":0}}]}},{"x":0,"y":50,"rotation":0,"id":29,"uid":null,"width":140,"height":40,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Shape","Shape":{"tid":"com.gliffy.stencil.rectangle.basic_v1","strokeWidth":2,"strokeColor":"#000000","fillColor":"#FFFFFF","gradient":false,"dropShadow":true,"state":0,"shadowX":4,"shadowY":4,"opacity":1}},"children":[{"x":0,"y":0,"rotation":0,"id":30,"uid":null,"width":140,"height":18,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Text","Text":{"tid":null,"valign":"top","overflow":"none","vposition":"none","hposition":"none","html":"

drive

","paddingLeft":2,"paddingRight":2,"paddingBottom":2,"paddingTop":2}},"children":null}],"constraints":{"constraints":[{"type":"HeightConstraint","HeightConstraint":{"isMin":false,"heightInfo":[{"id":24,"magnitude":1},{"id":25,"magnitude":-1},{"id":27,"magnitude":-1}],"growParent":false,"padding":0}},{"type":"PositionConstraint","PositionConstraint":{"nodeId":27,"px":0,"py":1}}]}}],"constraints":{"constraints":[{"type":"HeightConstraint","HeightConstraint":{"isMin":true,"heightInfo":[{"id":25,"magnitude":1},{"id":27,"magnitude":1},{"id":30,"magnitude":1}],"growParent":false,"padding":0}}]},"linkMap":[]},{"x":580,"y":112.5,"rotation":0,"id":32,"uid":"com.gliffy.shape.uml.uml_v1.default.class","width":140,"height":95,"lockAspectRatio":false,"lockShape":false,"order":32,"graphic":null,"children":[{"x":0,"y":0,"rotation":0,"id":33,"uid":null,"width":140,"height":18,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Shape","Shape":{"tid":"com.gliffy.stencil.rectangle.basic_v1","strokeWidth":2,"strokeColor":"#000000","fillColor":"#FFFFFF","gradient":false,"dropShadow":true,"state":0,"shadowX":4,"shadowY":4,"opacity":1}},"children":[{"x":0,"y":0,"rotation":0,"id":34,"uid":null,"width":140,"height":18,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Text","Text":{"tid":null,"valign":"top","overflow":"none","vposition":"none","hposition":"none","html":"

Vehicle

","paddingLeft":2,"paddingRight":2,"paddingBottom":2,"paddingTop":2}},"children":null}],"constraints":{"constraints":[{"type":"HeightConstraint","HeightConstraint":{"isMin":false,"heightInfo":[{"id":34,"magnitude":1}],"growParent":true,"padding":0}}]}},{"x":0,"y":18,"rotation":0,"id":35,"uid":null,"width":140,"height":32,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Shape","Shape":{"tid":"com.gliffy.stencil.rectangle.basic_v1","strokeWidth":2,"strokeColor":"#000000","fillColor":"#FFFFFF","gradient":false,"dropShadow":true,"state":0,"shadowX":4,"shadowY":4,"opacity":1}},"children":[{"x":0,"y":0,"rotation":0,"id":36,"uid":null,"width":140,"height":32,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Text","Text":{"tid":null,"valign":"top","overflow":"none","vposition":"none","hposition":"none","html":"

brand\n

engine

","paddingLeft":2,"paddingRight":2,"paddingBottom":2,"paddingTop":2}},"children":null}],"constraints":{"constraints":[{"type":"PositionConstraint","PositionConstraint":{"nodeId":33,"px":0,"py":1}},{"type":"HeightConstraint","HeightConstraint":{"isMin":false,"heightInfo":[{"id":36,"magnitude":1}],"growParent":true,"padding":0}}]}},{"x":0,"y":50,"rotation":0,"id":37,"uid":null,"width":140,"height":45,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Shape","Shape":{"tid":"com.gliffy.stencil.rectangle.basic_v1","strokeWidth":2,"strokeColor":"#000000","fillColor":"#FFFFFF","gradient":false,"dropShadow":true,"state":0,"shadowX":4,"shadowY":4,"opacity":1}},"children":[{"x":0,"y":0,"rotation":0,"id":38,"uid":null,"width":140,"height":32,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Text","Text":{"tid":null,"valign":"top","overflow":"none","vposition":"none","hposition":"none","html":"

accelerate\n

slow_down

","paddingLeft":2,"paddingRight":2,"paddingBottom":2,"paddingTop":2}},"children":null}],"constraints":{"constraints":[{"type":"HeightConstraint","HeightConstraint":{"isMin":false,"heightInfo":[{"id":32,"magnitude":1},{"id":33,"magnitude":-1},{"id":35,"magnitude":-1}],"growParent":false,"padding":0}},{"type":"PositionConstraint","PositionConstraint":{"nodeId":35,"px":0,"py":1}}]}}],"constraints":{"constraints":[{"type":"HeightConstraint","HeightConstraint":{"isMin":true,"heightInfo":[{"id":33,"magnitude":1},{"id":35,"magnitude":1},{"id":38,"magnitude":1}],"growParent":false,"padding":0}}]},"linkMap":[]},{"x":360,"y":380,"rotation":0,"id":48,"uid":"com.gliffy.shape.uml.uml_v1.default.class","width":140,"height":75,"lockAspectRatio":false,"lockShape":false,"order":41,"graphic":null,"children":[{"x":0,"y":0,"rotation":0,"id":49,"uid":null,"width":140,"height":18,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Shape","Shape":{"tid":"com.gliffy.stencil.rectangle.basic_v1","strokeWidth":2,"strokeColor":"#000000","fillColor":"#FFFFFF","gradient":false,"dropShadow":true,"state":0,"shadowX":4,"shadowY":4,"opacity":1}},"children":[{"x":0,"y":0,"rotation":0,"id":50,"uid":null,"width":140,"height":18,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Text","Text":{"tid":null,"valign":"top","overflow":"none","vposition":"none","hposition":"none","html":"

License

","paddingLeft":2,"paddingRight":2,"paddingBottom":2,"paddingTop":2}},"children":null}],"constraints":{"constraints":[{"type":"HeightConstraint","HeightConstraint":{"isMin":false,"heightInfo":[{"id":50,"magnitude":1}],"growParent":true,"padding":0}}]}},{"x":0,"y":18,"rotation":0,"id":51,"uid":null,"width":140,"height":18,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Shape","Shape":{"tid":"com.gliffy.stencil.rectangle.basic_v1","strokeWidth":2,"strokeColor":"#000000","fillColor":"#FFFFFF","gradient":false,"dropShadow":true,"state":0,"shadowX":4,"shadowY":4,"opacity":1}},"children":[{"x":0,"y":0,"rotation":0,"id":52,"uid":null,"width":140,"height":18,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Text","Text":{"tid":null,"valign":"top","overflow":"none","vposition":"none","hposition":"none","html":"

Attribute

","paddingLeft":2,"paddingRight":2,"paddingBottom":2,"paddingTop":2}},"children":null}],"constraints":{"constraints":[{"type":"PositionConstraint","PositionConstraint":{"nodeId":49,"px":0,"py":1}},{"type":"HeightConstraint","HeightConstraint":{"isMin":false,"heightInfo":[{"id":52,"magnitude":1}],"growParent":true,"padding":0}}]}},{"x":0,"y":36,"rotation":0,"id":53,"uid":null,"width":140,"height":39,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Shape","Shape":{"tid":"com.gliffy.stencil.rectangle.basic_v1","strokeWidth":2,"strokeColor":"#000000","fillColor":"#FFFFFF","gradient":false,"dropShadow":true,"state":0,"shadowX":4,"shadowY":4,"opacity":1}},"children":[{"x":0,"y":0,"rotation":0,"id":54,"uid":null,"width":140,"height":18,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Text","Text":{"tid":null,"valign":"top","overflow":"none","vposition":"none","hposition":"none","html":"

Method

","paddingLeft":2,"paddingRight":2,"paddingBottom":2,"paddingTop":2}},"children":null}],"constraints":{"constraints":[{"type":"HeightConstraint","HeightConstraint":{"isMin":false,"heightInfo":[{"id":48,"magnitude":1},{"id":49,"magnitude":-1},{"id":51,"magnitude":-1}],"growParent":false,"padding":0}},{"type":"PositionConstraint","PositionConstraint":{"nodeId":51,"px":0,"py":1}}]}}],"constraints":{"constraints":[{"type":"HeightConstraint","HeightConstraint":{"isMin":true,"heightInfo":[{"id":49,"magnitude":1},{"id":51,"magnitude":1},{"id":54,"magnitude":1}],"growParent":false,"padding":0}}]},"linkMap":[]},{"x":580,"y":285,"rotation":0,"id":56,"uid":"com.gliffy.shape.uml.uml_v1.default.class","width":140,"height":75,"lockAspectRatio":false,"lockShape":false,"order":48,"graphic":null,"children":[{"x":0,"y":0,"rotation":0,"id":57,"uid":null,"width":140,"height":18,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Shape","Shape":{"tid":"com.gliffy.stencil.rectangle.basic_v1","strokeWidth":2,"strokeColor":"#000000","fillColor":"#FFFFFF","gradient":false,"dropShadow":true,"state":0,"shadowX":4,"shadowY":4,"opacity":1}},"children":[{"x":0,"y":0,"rotation":0,"id":58,"uid":null,"width":140,"height":18,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Text","Text":{"tid":null,"valign":"top","overflow":"none","vposition":"none","hposition":"none","html":"

Engine

","paddingLeft":2,"paddingRight":2,"paddingBottom":2,"paddingTop":2}},"children":null}],"constraints":{"constraints":[{"type":"HeightConstraint","HeightConstraint":{"isMin":false,"heightInfo":[{"id":58,"magnitude":1}],"growParent":true,"padding":0}}]}},{"x":0,"y":18,"rotation":0,"id":59,"uid":null,"width":140,"height":18,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Shape","Shape":{"tid":"com.gliffy.stencil.rectangle.basic_v1","strokeWidth":2,"strokeColor":"#000000","fillColor":"#FFFFFF","gradient":false,"dropShadow":true,"state":0,"shadowX":4,"shadowY":4,"opacity":1}},"children":[{"x":0,"y":0,"rotation":0,"id":60,"uid":null,"width":140,"height":18,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Text","Text":{"tid":null,"valign":"top","overflow":"none","vposition":"none","hposition":"none","html":"

number

","paddingLeft":2,"paddingRight":2,"paddingBottom":2,"paddingTop":2}},"children":null}],"constraints":{"constraints":[{"type":"PositionConstraint","PositionConstraint":{"nodeId":57,"px":0,"py":1}},{"type":"HeightConstraint","HeightConstraint":{"isMin":false,"heightInfo":[{"id":60,"magnitude":1}],"growParent":true,"padding":0}}]}},{"x":0,"y":36,"rotation":0,"id":61,"uid":null,"width":140,"height":39,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Shape","Shape":{"tid":"com.gliffy.stencil.rectangle.basic_v1","strokeWidth":2,"strokeColor":"#000000","fillColor":"#FFFFFF","gradient":false,"dropShadow":true,"state":0,"shadowX":4,"shadowY":4,"opacity":1}},"children":[{"x":0,"y":0,"rotation":0,"id":62,"uid":null,"width":140,"height":4,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Text","Text":{"tid":null,"valign":"top","overflow":"none","vposition":"none","hposition":"none","html":"

","paddingLeft":2,"paddingRight":2,"paddingBottom":2,"paddingTop":2}},"children":null}],"constraints":{"constraints":[{"type":"HeightConstraint","HeightConstraint":{"isMin":false,"heightInfo":[{"id":56,"magnitude":1},{"id":57,"magnitude":-1},{"id":59,"magnitude":-1}],"growParent":false,"padding":0}},{"type":"PositionConstraint","PositionConstraint":{"nodeId":59,"px":0,"py":1}}]}}],"constraints":{"constraints":[{"type":"HeightConstraint","HeightConstraint":{"isMin":true,"heightInfo":[{"id":57,"magnitude":1},{"id":59,"magnitude":1},{"id":62,"magnitude":1}],"growParent":false,"padding":0}}]},"linkMap":[]},{"x":784,"y":41,"rotation":0,"id":63,"uid":"com.gliffy.shape.uml.uml_v1.default.class","width":140,"height":71.5,"lockAspectRatio":false,"lockShape":false,"order":55,"graphic":null,"children":[{"x":0,"y":0,"rotation":0,"id":64,"uid":null,"width":140,"height":18,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Shape","Shape":{"tid":"com.gliffy.stencil.rectangle.basic_v1","strokeWidth":2,"strokeColor":"#000000","fillColor":"#FFFFFF","gradient":false,"dropShadow":true,"state":0,"shadowX":4,"shadowY":4,"opacity":1}},"children":[{"x":0,"y":0,"rotation":0,"id":65,"uid":null,"width":140,"height":18,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Text","Text":{"tid":null,"valign":"top","overflow":"none","vposition":"none","hposition":"none","html":"

Car

","paddingLeft":2,"paddingRight":2,"paddingBottom":2,"paddingTop":2}},"children":null}],"constraints":{"constraints":[{"type":"HeightConstraint","HeightConstraint":{"isMin":false,"heightInfo":[{"id":65,"magnitude":1}],"growParent":true,"padding":0}}]}},{"x":0,"y":18,"rotation":0,"id":66,"uid":null,"width":140,"height":32,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Shape","Shape":{"tid":"com.gliffy.stencil.rectangle.basic_v1","strokeWidth":2,"strokeColor":"#000000","fillColor":"#FFFFFF","gradient":false,"dropShadow":true,"state":0,"shadowX":4,"shadowY":4,"opacity":1}},"children":[{"x":0,"y":0,"rotation":0,"id":67,"uid":null,"width":140,"height":32,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Text","Text":{"tid":null,"valign":"top","overflow":"none","vposition":"none","hposition":"none","html":"

seats\n

displacement

","paddingLeft":2,"paddingRight":2,"paddingBottom":2,"paddingTop":2}},"children":null}],"constraints":{"constraints":[{"type":"PositionConstraint","PositionConstraint":{"nodeId":64,"px":0,"py":1}},{"type":"HeightConstraint","HeightConstraint":{"isMin":false,"heightInfo":[{"id":67,"magnitude":1}],"growParent":true,"padding":0}}]}},{"x":0,"y":50,"rotation":0,"id":68,"uid":null,"width":140,"height":21.5,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Shape","Shape":{"tid":"com.gliffy.stencil.rectangle.basic_v1","strokeWidth":2,"strokeColor":"#000000","fillColor":"#FFFFFF","gradient":false,"dropShadow":true,"state":0,"shadowX":4,"shadowY":4,"opacity":1}},"children":[{"x":0,"y":0,"rotation":0,"id":69,"uid":null,"width":140,"height":4,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Text","Text":{"tid":null,"valign":"top","overflow":"none","vposition":"none","hposition":"none","html":"

","paddingLeft":2,"paddingRight":2,"paddingBottom":2,"paddingTop":2}},"children":null}],"constraints":{"constraints":[{"type":"HeightConstraint","HeightConstraint":{"isMin":false,"heightInfo":[{"id":63,"magnitude":1},{"id":64,"magnitude":-1},{"id":66,"magnitude":-1}],"growParent":false,"padding":0}},{"type":"PositionConstraint","PositionConstraint":{"nodeId":66,"px":0,"py":1}}]}}],"constraints":{"constraints":[{"type":"HeightConstraint","HeightConstraint":{"isMin":true,"heightInfo":[{"id":64,"magnitude":1},{"id":66,"magnitude":1},{"id":69,"magnitude":1}],"growParent":false,"padding":0}}]},"linkMap":[]},{"x":784,"y":207.5,"rotation":0,"id":70,"uid":"com.gliffy.shape.uml.uml_v1.default.class","width":140,"height":75,"lockAspectRatio":false,"lockShape":false,"order":62,"graphic":null,"children":[{"x":0,"y":0,"rotation":0,"id":71,"uid":null,"width":140,"height":18,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Shape","Shape":{"tid":"com.gliffy.stencil.rectangle.basic_v1","strokeWidth":2,"strokeColor":"#000000","fillColor":"#FFFFFF","gradient":false,"dropShadow":true,"state":0,"shadowX":4,"shadowY":4,"opacity":1}},"children":[{"x":0,"y":0,"rotation":0,"id":72,"uid":null,"width":140,"height":18,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Text","Text":{"tid":null,"valign":"top","overflow":"none","vposition":"none","hposition":"none","html":"

Truck

","paddingLeft":2,"paddingRight":2,"paddingBottom":2,"paddingTop":2}},"children":null}],"constraints":{"constraints":[{"type":"HeightConstraint","HeightConstraint":{"isMin":false,"heightInfo":[{"id":72,"magnitude":1}],"growParent":true,"padding":0}}]}},{"x":0,"y":18,"rotation":0,"id":73,"uid":null,"width":140,"height":18,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Shape","Shape":{"tid":"com.gliffy.stencil.rectangle.basic_v1","strokeWidth":2,"strokeColor":"#000000","fillColor":"#FFFFFF","gradient":false,"dropShadow":true,"state":0,"shadowX":4,"shadowY":4,"opacity":1}},"children":[{"x":0,"y":0,"rotation":0,"id":74,"uid":null,"width":140,"height":18,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Text","Text":{"tid":null,"valign":"top","overflow":"none","vposition":"none","hposition":"none","html":"

capacity

","paddingLeft":2,"paddingRight":2,"paddingBottom":2,"paddingTop":2}},"children":null}],"constraints":{"constraints":[{"type":"PositionConstraint","PositionConstraint":{"nodeId":71,"px":0,"py":1}},{"type":"HeightConstraint","HeightConstraint":{"isMin":false,"heightInfo":[{"id":74,"magnitude":1}],"growParent":true,"padding":0}}]}},{"x":0,"y":36,"rotation":0,"id":75,"uid":null,"width":140,"height":39,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Shape","Shape":{"tid":"com.gliffy.stencil.rectangle.basic_v1","strokeWidth":2,"strokeColor":"#000000","fillColor":"#FFFFFF","gradient":false,"dropShadow":true,"state":0,"shadowX":4,"shadowY":4,"opacity":1}},"children":[{"x":0,"y":0,"rotation":0,"id":76,"uid":null,"width":140,"height":4,"lockAspectRatio":false,"lockShape":false,"order":"auto","graphic":{"type":"Text","Text":{"tid":null,"valign":"top","overflow":"none","vposition":"none","hposition":"none","html":"

","paddingLeft":2,"paddingRight":2,"paddingBottom":2,"paddingTop":2}},"children":null}],"constraints":{"constraints":[{"type":"HeightConstraint","HeightConstraint":{"isMin":false,"heightInfo":[{"id":70,"magnitude":1},{"id":71,"magnitude":-1},{"id":73,"magnitude":-1}],"growParent":false,"padding":0}},{"type":"PositionConstraint","PositionConstraint":{"nodeId":73,"px":0,"py":1}}]}}],"constraints":{"constraints":[{"type":"HeightConstraint","HeightConstraint":{"isMin":true,"heightInfo":[{"id":71,"magnitude":1},{"id":73,"magnitude":1},{"id":76,"magnitude":1}],"growParent":false,"padding":0}}]},"linkMap":[]},{"x":371,"y":346,"rotation":0,"id":81,"uid":"com.gliffy.shape.basic.basic_v1.default.text","width":60,"height":14,"lockAspectRatio":false,"lockShape":false,"order":72,"graphic":{"type":"Text","Text":{"tid":null,"valign":"middle","overflow":"none","vposition":"none","hposition":"none","html":"

关联关系

","paddingLeft":2,"paddingRight":2,"paddingBottom":2,"paddingTop":2}},"children":null,"linkMap":[]},{"x":490,"y":186.5,"rotation":0,"id":82,"uid":"com.gliffy.shape.basic.basic_v1.default.text","width":60,"height":14,"lockAspectRatio":false,"lockShape":false,"order":73,"graphic":{"type":"Text","Text":{"tid":null,"valign":"middle","overflow":"none","vposition":"none","hposition":"none","html":"

依赖关系

","paddingLeft":2,"paddingRight":2,"paddingBottom":2,"paddingTop":2}},"children":null,"linkMap":[]},{"x":591,"y":238,"rotation":0,"id":83,"uid":"com.gliffy.shape.basic.basic_v1.default.text","width":60,"height":14,"lockAspectRatio":false,"lockShape":false,"order":74,"graphic":{"type":"Text","Text":{"tid":null,"valign":"middle","overflow":"none","vposition":"none","hposition":"none","html":"

聚合关系

","paddingLeft":2,"paddingRight":2,"paddingBottom":2,"paddingTop":2}},"children":null,"linkMap":[]}],"background":"#FFFFFF","width":926,"height":455,"maxWidth":5000,"maxHeight":5000,"nodeIndex":84,"autoFit":true,"exportBorder":false,"gridOn":true,"snapToGrid":true,"drawingGuidesOn":true,"pageBreaksOn":false,"printGridOn":false,"printPaper":"LETTER","printShrinkToFit":false,"printPortrait":true,"shapeStyles":{},"lineStyles":{},"textStyles":{},"themeData":null}} \ No newline at end of file diff --git a/Day01-15/Day14-B/网络应用开发.md b/Day01-15/Day14-B/网络应用开发.md deleted file mode 100644 index 09edf81..0000000 --- a/Day01-15/Day14-B/网络应用开发.md +++ /dev/null @@ -1,121 +0,0 @@ -## 网络应用开发 - -### 发送电子邮件 - -在即时通信软件如此发达的今天,电子邮件仍然是互联网上使用最为广泛的应用之一,公司向应聘者发出录用通知、网站向用户发送一个激活账号的链接、银行向客户推广它们的理财产品等几乎都是通过电子邮件来完成的,而这些任务应该都是由程序自动完成的。 - -就像我们可以用HTTP(超文本传输协议)来访问一个网站一样,发送邮件要使用SMTP(简单邮件传输协议),SMTP也是一个建立在TCP(传输控制协议)提供的可靠数据传输服务的基础上的应用级协议,它规定了邮件的发送者如何跟发送邮件的服务器进行通信的细节,而Python中的smtplib模块将这些操作简化成了几个简单的函数。 - -下面的代码演示了如何在Python发送邮件。 - -```Python -from smtplib import SMTP -from email.header import Header -from email.mime.text import MIMEText - - -def main(): - # 请自行修改下面的邮件发送者和接收者 - sender = 'abcdefg@126.com' - receivers = ['uvwxyz@qq.com', 'uvwxyz@126.com'] - message = MIMEText('用Python发送邮件的示例代码.', 'plain', 'utf-8') - message['From'] = Header('王大锤', 'utf-8') - message['To'] = Header('骆昊', 'utf-8') - message['Subject'] = Header('示例代码实验邮件', 'utf-8') - smtper = SMTP('smtp.126.com') - # 请自行修改下面的登录口令 - smtper.login(sender, 'secretpass') - smtper.sendmail(sender, receivers, message.as_string()) - print('邮件发送完成!') - - -if __name__ == '__main__': - main() -``` - -如果要发送带有附件的邮件,那么可以按照下面的方式进行操作。 - -```Python -from smtplib import SMTP -from email.header import Header -from email.mime.text import MIMEText -from email.mime.image import MIMEImage -from email.mime.multipart import MIMEMultipart - -import urllib - - -def main(): - # 创建一个带附件的邮件消息对象 - message = MIMEMultipart() - - # 创建文本内容 - text_content = MIMEText('附件中有本月数据请查收', 'plain', 'utf-8') - message['Subject'] = Header('本月数据', 'utf-8') - # 将文本内容添加到邮件消息对象中 - message.attach(text_content) - - # 读取文件并将文件作为附件添加到邮件消息对象中 - with open('/Users/Hao/Desktop/hello.txt', 'rb') as f: - txt = MIMEText(f.read(), 'base64', 'utf-8') - txt['Content-Type'] = 'text/plain' - txt['Content-Disposition'] = 'attachment; filename=hello.txt' - message.attach(txt) - # 读取文件并将文件作为附件添加到邮件消息对象中 - with open('/Users/Hao/Desktop/汇总数据.xlsx', 'rb') as f: - xls = MIMEText(f.read(), 'base64', 'utf-8') - xls['Content-Type'] = 'application/vnd.ms-excel' - xls['Content-Disposition'] = 'attachment; filename=month-data.xlsx' - message.attach(xls) - - # 创建SMTP对象 - smtper = SMTP('smtp.126.com') - # 开启安全连接 - # smtper.starttls() - sender = 'abcdefg@126.com' - receivers = ['uvwxyz@qq.com'] - # 登录到SMTP服务器 - # 请注意此处不是使用密码而是邮件客户端授权码进行登录 - # 对此有疑问的读者可以联系自己使用的邮件服务器客服 - smtper.login(sender, 'secretpass') - # 发送邮件 - smtper.sendmail(sender, receivers, message.as_string()) - # 与邮件服务器断开连接 - smtper.quit() - print('发送完成!') - - -if __name__ == '__main__': - main() -``` - -### 发送短信 - -发送短信也是项目中常见的功能,网站的注册码、验证码、营销信息基本上都是通过短信来发送给用户的。在下面的代码中我们使用了[互亿无线](http://www.ihuyi.com/)短信平台(该平台为注册用户提供了50条免费短信以及常用开发语言发送短信的demo,可以登录该网站并在用户自服务页面中对短信进行配置)提供的API接口实现了发送短信的服务,当然国内的短信平台很多,读者可以根据自己的需要进行选择(通常会考虑费用预算、短信达到率、使用的难易程度等指标),如果需要在商业项目中使用短信服务建议购买短信平台提供的套餐服务。 - -```Python -import urllib.parse -import http.client -import json - - -def main(): - host = "106.ihuyi.com" - sms_send_uri = "/webservice/sms.php?method=Submit" - # 下面的参数需要填入自己注册的账号和对应的密码 - params = urllib.parse.urlencode({'account': '你自己的账号', 'password' : '你自己的密码', 'content': '您的验证码是:147258。请不要把验证码泄露给其他人。', 'mobile': '接收者的手机号', 'format':'json' }) - print(params) - headers = {'Content-type': 'application/x-www-form-urlencoded', 'Accept': 'text/plain'} - conn = http.client.HTTPConnection(host, port=80, timeout=30) - conn.request('POST', sms_send_uri, params, headers) - response = conn.getresponse() - response_str = response.read() - jsonstr = response_str.decode('utf-8') - print(json.loads(jsonstr)) - conn.close() - - -if __name__ == '__main__': - main() -``` - diff --git a/Day01-15/Day01/code/hello.py b/Day01-15/code/Day01/hello.py similarity index 100% rename from Day01-15/Day01/code/hello.py rename to Day01-15/code/Day01/hello.py diff --git a/Day01-15/Day01/code/peppa_pig.py b/Day01-15/code/Day01/peppa_pig.py similarity index 100% rename from Day01-15/Day01/code/peppa_pig.py rename to Day01-15/code/Day01/peppa_pig.py diff --git a/Day01-15/Day02/code/centigrade.py b/Day01-15/code/Day02/centigrade.py similarity index 100% rename from Day01-15/Day02/code/centigrade.py rename to Day01-15/code/Day02/centigrade.py diff --git a/Day01-15/Day02/code/circle.py b/Day01-15/code/Day02/circle.py similarity index 100% rename from Day01-15/Day02/code/circle.py rename to Day01-15/code/Day02/circle.py diff --git a/Day01-15/Day02/code/leap.py b/Day01-15/code/Day02/leap.py similarity index 100% rename from Day01-15/Day02/code/leap.py rename to Day01-15/code/Day02/leap.py diff --git a/Day01-15/Day02/code/operator.py b/Day01-15/code/Day02/operator.py similarity index 100% rename from Day01-15/Day02/code/operator.py rename to Day01-15/code/Day02/operator.py diff --git a/Day01-15/Day02/code/string.py b/Day01-15/code/Day02/strings.py similarity index 100% rename from Day01-15/Day02/code/string.py rename to Day01-15/code/Day02/strings.py diff --git a/Day01-15/Day02/code/variable1.py b/Day01-15/code/Day02/variable1.py similarity index 100% rename from Day01-15/Day02/code/variable1.py rename to Day01-15/code/Day02/variable1.py diff --git a/Day01-15/Day02/code/variable2.py b/Day01-15/code/Day02/variable2.py similarity index 100% rename from Day01-15/Day02/code/variable2.py rename to Day01-15/code/Day02/variable2.py diff --git a/Day01-15/Day02/code/variable3.py b/Day01-15/code/Day02/variable3.py similarity index 100% rename from Day01-15/Day02/code/variable3.py rename to Day01-15/code/Day02/variable3.py diff --git a/Day01-15/Day02/code/variable4.py b/Day01-15/code/Day02/variable4.py similarity index 100% rename from Day01-15/Day02/code/variable4.py rename to Day01-15/code/Day02/variable4.py diff --git a/Day01-15/Day02/code/variable5.py b/Day01-15/code/Day02/variable5.py similarity index 100% rename from Day01-15/Day02/code/variable5.py rename to Day01-15/code/Day02/variable5.py diff --git a/Day01-15/Day03/code/convert.py b/Day01-15/code/Day03/convert.py similarity index 100% rename from Day01-15/Day03/code/convert.py rename to Day01-15/code/Day03/convert.py diff --git a/Day01-15/Day03/code/grade.py b/Day01-15/code/Day03/grade.py similarity index 100% rename from Day01-15/Day03/code/grade.py rename to Day01-15/code/Day03/grade.py diff --git a/Day01-15/Day03/code/piecewise.py b/Day01-15/code/Day03/piecewise.py similarity index 100% rename from Day01-15/Day03/code/piecewise.py rename to Day01-15/code/Day03/piecewise.py diff --git a/Day01-15/Day03/code/rolldice.py b/Day01-15/code/Day03/rolldice.py similarity index 100% rename from Day01-15/Day03/code/rolldice.py rename to Day01-15/code/Day03/rolldice.py diff --git a/Day01-15/Day03/code/tax.py b/Day01-15/code/Day03/tax.py similarity index 100% rename from Day01-15/Day03/code/tax.py rename to Day01-15/code/Day03/tax.py diff --git a/Day01-15/Day03/code/triangle.py b/Day01-15/code/Day03/triangle.py similarity index 100% rename from Day01-15/Day03/code/triangle.py rename to Day01-15/code/Day03/triangle.py diff --git a/Day01-15/Day03/code/verify.py b/Day01-15/code/Day03/verify.py similarity index 100% rename from Day01-15/Day03/code/verify.py rename to Day01-15/code/Day03/verify.py diff --git a/Day01-15/Day04/code/for1.py b/Day01-15/code/Day04/for1.py similarity index 78% rename from Day01-15/Day04/code/for1.py rename to Day01-15/code/Day04/for1.py index 062179a..659724d 100644 --- a/Day01-15/Day04/code/for1.py +++ b/Day01-15/code/Day04/for1.py @@ -8,6 +8,5 @@ Date: 2018-03-01 sum = 0 for x in range(1, 101): - if x % 2 == 0: - sum += x + sum += x print(sum) diff --git a/Day01-15/Day04/code/for2.py b/Day01-15/code/Day04/for2.py similarity index 100% rename from Day01-15/Day04/code/for2.py rename to Day01-15/code/Day04/for2.py diff --git a/Day01-15/Day04/code/for3.py b/Day01-15/code/Day04/for3.py similarity index 100% rename from Day01-15/Day04/code/for3.py rename to Day01-15/code/Day04/for3.py diff --git a/Day01-15/Day04/code/for4.py b/Day01-15/code/Day04/for4.py similarity index 100% rename from Day01-15/Day04/code/for4.py rename to Day01-15/code/Day04/for4.py diff --git a/Day01-15/Day04/code/for5.py b/Day01-15/code/Day04/for5.py similarity index 100% rename from Day01-15/Day04/code/for5.py rename to Day01-15/code/Day04/for5.py diff --git a/Day01-15/Day04/code/for6.py b/Day01-15/code/Day04/for6.py similarity index 100% rename from Day01-15/Day04/code/for6.py rename to Day01-15/code/Day04/for6.py diff --git a/Day01-15/Day04/code/while1.py b/Day01-15/code/Day04/while1.py similarity index 100% rename from Day01-15/Day04/code/while1.py rename to Day01-15/code/Day04/while1.py diff --git a/Day01-15/Day04/code/while2.py b/Day01-15/code/Day04/while2.py similarity index 100% rename from Day01-15/Day04/code/while2.py rename to Day01-15/code/Day04/while2.py diff --git a/Day01-15/Day05/code/chicken.py b/Day01-15/code/Day05/chicken.py similarity index 100% rename from Day01-15/Day05/code/chicken.py rename to Day01-15/code/Day05/chicken.py diff --git a/Day01-15/Day05/code/craps.py b/Day01-15/code/Day05/craps.py similarity index 100% rename from Day01-15/Day05/code/craps.py rename to Day01-15/code/Day05/craps.py diff --git a/Day01-15/Day05/code/fibonacci.py b/Day01-15/code/Day05/fibonacci.py similarity index 100% rename from Day01-15/Day05/code/fibonacci.py rename to Day01-15/code/Day05/fibonacci.py diff --git a/Day01-15/Day05/code/guess.py b/Day01-15/code/Day05/guess.py similarity index 100% rename from Day01-15/Day05/code/guess.py rename to Day01-15/code/Day05/guess.py diff --git a/Day01-15/Day05/code/lily.py b/Day01-15/code/Day05/lily.py similarity index 100% rename from Day01-15/Day05/code/lily.py rename to Day01-15/code/Day05/lily.py diff --git a/Day01-15/Day05/code/palindrome.py b/Day01-15/code/Day05/palindrome.py similarity index 100% rename from Day01-15/Day05/code/palindrome.py rename to Day01-15/code/Day05/palindrome.py diff --git a/Day01-15/Day05/code/perfect.py b/Day01-15/code/Day05/perfect.py similarity index 100% rename from Day01-15/Day05/code/perfect.py rename to Day01-15/code/Day05/perfect.py diff --git a/Day01-15/Day05/code/prime.py b/Day01-15/code/Day05/prime.py similarity index 100% rename from Day01-15/Day05/code/prime.py rename to Day01-15/code/Day05/prime.py diff --git a/Day01-15/Day05/code/table.py b/Day01-15/code/Day05/table.py similarity index 100% rename from Day01-15/Day05/code/table.py rename to Day01-15/code/Day05/table.py diff --git a/Day01-15/Day06/code/function1.py b/Day01-15/code/Day06/function1.py similarity index 100% rename from Day01-15/Day06/code/function1.py rename to Day01-15/code/Day06/function1.py diff --git a/Day01-15/Day06/code/function2.py b/Day01-15/code/Day06/function2.py similarity index 100% rename from Day01-15/Day06/code/function2.py rename to Day01-15/code/Day06/function2.py diff --git a/Day01-15/Day06/code/function3.py b/Day01-15/code/Day06/function3.py similarity index 100% rename from Day01-15/Day06/code/function3.py rename to Day01-15/code/Day06/function3.py diff --git a/Day01-15/Day06/code/function4.py b/Day01-15/code/Day06/function4.py similarity index 100% rename from Day01-15/Day06/code/function4.py rename to Day01-15/code/Day06/function4.py diff --git a/Day01-15/Day06/code/function5.py b/Day01-15/code/Day06/function5.py similarity index 100% rename from Day01-15/Day06/code/function5.py rename to Day01-15/code/Day06/function5.py diff --git a/Day01-15/Day06/code/function6.py b/Day01-15/code/Day06/function6.py similarity index 100% rename from Day01-15/Day06/code/function6.py rename to Day01-15/code/Day06/function6.py diff --git a/Day01-15/Day07/code/avgscore.py b/Day01-15/code/Day07/avgscore.py similarity index 100% rename from Day01-15/Day07/code/avgscore.py rename to Day01-15/code/Day07/avgscore.py diff --git a/Day01-15/Day07/code/dict1.py b/Day01-15/code/Day07/dict1.py similarity index 100% rename from Day01-15/Day07/code/dict1.py rename to Day01-15/code/Day07/dict1.py diff --git a/Day01-15/Day07/code/dict2.py b/Day01-15/code/Day07/dict2.py similarity index 100% rename from Day01-15/Day07/code/dict2.py rename to Day01-15/code/Day07/dict2.py diff --git a/Day01-15/Day07/code/fibonacci.py b/Day01-15/code/Day07/fibonacci.py similarity index 100% rename from Day01-15/Day07/code/fibonacci.py rename to Day01-15/code/Day07/fibonacci.py diff --git a/Day01-15/Day07/code/findmax.py b/Day01-15/code/Day07/findmax.py similarity index 100% rename from Day01-15/Day07/code/findmax.py rename to Day01-15/code/Day07/findmax.py diff --git a/Day01-15/Day07/code/list1.py b/Day01-15/code/Day07/list1.py similarity index 100% rename from Day01-15/Day07/code/list1.py rename to Day01-15/code/Day07/list1.py diff --git a/Day01-15/Day07/code/list2.py b/Day01-15/code/Day07/list2.py similarity index 100% rename from Day01-15/Day07/code/list2.py rename to Day01-15/code/Day07/list2.py diff --git a/Day01-15/Day07/code/list3.py b/Day01-15/code/Day07/list3.py similarity index 100% rename from Day01-15/Day07/code/list3.py rename to Day01-15/code/Day07/list3.py diff --git a/Day01-15/Day07/code/lottery.py b/Day01-15/code/Day07/lottery.py similarity index 100% rename from Day01-15/Day07/code/lottery.py rename to Day01-15/code/Day07/lottery.py diff --git a/Day01-15/Day07/code/marquee.py b/Day01-15/code/Day07/marquee.py similarity index 100% rename from Day01-15/Day07/code/marquee.py rename to Day01-15/code/Day07/marquee.py diff --git a/Day01-15/Day07/code/scoretable.py b/Day01-15/code/Day07/scoretable.py similarity index 100% rename from Day01-15/Day07/code/scoretable.py rename to Day01-15/code/Day07/scoretable.py diff --git a/Day01-15/Day07/code/set1.py b/Day01-15/code/Day07/set1.py similarity index 100% rename from Day01-15/Day07/code/set1.py rename to Day01-15/code/Day07/set1.py diff --git a/Day01-15/Day07/code/set2.py b/Day01-15/code/Day07/set2.py similarity index 100% rename from Day01-15/Day07/code/set2.py rename to Day01-15/code/Day07/set2.py diff --git a/Day01-15/Day07/code/tic-tac-toe.py b/Day01-15/code/Day07/tic-tac-toe.py similarity index 100% rename from Day01-15/Day07/code/tic-tac-toe.py rename to Day01-15/code/Day07/tic-tac-toe.py diff --git a/Day01-15/Day07/code/tuple.py b/Day01-15/code/Day07/tuple.py similarity index 100% rename from Day01-15/Day07/code/tuple.py rename to Day01-15/code/Day07/tuple.py diff --git a/Day01-15/Day07/code/yanghui.py b/Day01-15/code/Day07/yanghui.py similarity index 100% rename from Day01-15/Day07/code/yanghui.py rename to Day01-15/code/Day07/yanghui.py diff --git a/Day01-15/Day08/code/access.py b/Day01-15/code/Day08/access.py similarity index 100% rename from Day01-15/Day08/code/access.py rename to Day01-15/code/Day08/access.py diff --git a/Day01-15/Day08/code/circle.py b/Day01-15/code/Day08/circle.py similarity index 100% rename from Day01-15/Day08/code/circle.py rename to Day01-15/code/Day08/circle.py diff --git a/Day01-15/Day08/code/clock.py b/Day01-15/code/Day08/clock.py similarity index 100% rename from Day01-15/Day08/code/clock.py rename to Day01-15/code/Day08/clock.py diff --git a/Day01-15/Day08/code/guess.py b/Day01-15/code/Day08/guess.py similarity index 100% rename from Day01-15/Day08/code/guess.py rename to Day01-15/code/Day08/guess.py diff --git a/Day01-15/Day08/code/hack.py b/Day01-15/code/Day08/hack.py similarity index 100% rename from Day01-15/Day08/code/hack.py rename to Day01-15/code/Day08/hack.py diff --git a/Day01-15/Day08/code/rect.py b/Day01-15/code/Day08/rect.py similarity index 100% rename from Day01-15/Day08/code/rect.py rename to Day01-15/code/Day08/rect.py diff --git a/Day01-15/Day08/code/student.py b/Day01-15/code/Day08/student.py similarity index 100% rename from Day01-15/Day08/code/student.py rename to Day01-15/code/Day08/student.py diff --git a/Day01-15/Day09/code/association.py b/Day01-15/code/Day09/association.py similarity index 100% rename from Day01-15/Day09/code/association.py rename to Day01-15/code/Day09/association.py diff --git a/Day01-15/Day09/code/car1.py b/Day01-15/code/Day09/car1.py similarity index 100% rename from Day01-15/Day09/code/car1.py rename to Day01-15/code/Day09/car1.py diff --git a/Day01-15/Day09/code/car2.py b/Day01-15/code/Day09/car2.py similarity index 100% rename from Day01-15/Day09/code/car2.py rename to Day01-15/code/Day09/car2.py diff --git a/Day01-15/Day09/code/clock.py b/Day01-15/code/Day09/clock.py similarity index 100% rename from Day01-15/Day09/code/clock.py rename to Day01-15/code/Day09/clock.py diff --git a/Day01-15/Day09/code/dependency.py b/Day01-15/code/Day09/dependency.py similarity index 100% rename from Day01-15/Day09/code/dependency.py rename to Day01-15/code/Day09/dependency.py diff --git a/Day01-15/Day09/code/diamond.py b/Day01-15/code/Day09/diamond.py similarity index 100% rename from Day01-15/Day09/code/diamond.py rename to Day01-15/code/Day09/diamond.py diff --git a/Day01-15/Day09/code/employee.py b/Day01-15/code/Day09/employee.py similarity index 100% rename from Day01-15/Day09/code/employee.py rename to Day01-15/code/Day09/employee.py diff --git a/Day01-15/Day09/code/multi.py b/Day01-15/code/Day09/multi.py similarity index 100% rename from Day01-15/Day09/code/multi.py rename to Day01-15/code/Day09/multi.py diff --git a/Day01-15/Day09/code/pet.py b/Day01-15/code/Day09/pet.py similarity index 100% rename from Day01-15/Day09/code/pet.py rename to Day01-15/code/Day09/pet.py diff --git a/Day01-15/Day09/code/rational.py b/Day01-15/code/Day09/rational.py similarity index 100% rename from Day01-15/Day09/code/rational.py rename to Day01-15/code/Day09/rational.py diff --git a/Day01-15/Day09/code/shape.py b/Day01-15/code/Day09/shape.py similarity index 100% rename from Day01-15/Day09/code/shape.py rename to Day01-15/code/Day09/shape.py diff --git a/Day01-15/Day09/code/triangle.py b/Day01-15/code/Day09/triangle.py similarity index 100% rename from Day01-15/Day09/code/triangle.py rename to Day01-15/code/Day09/triangle.py diff --git a/Day01-15/Day10/code/ball.py b/Day01-15/code/Day10/ball.py similarity index 100% rename from Day01-15/Day10/code/ball.py rename to Day01-15/code/Day10/ball.py diff --git a/Day01-15/Day10/code/gui1.py b/Day01-15/code/Day10/gui1.py similarity index 100% rename from Day01-15/Day10/code/gui1.py rename to Day01-15/code/Day10/gui1.py diff --git a/Day01-15/Day10/code/gui2.py b/Day01-15/code/Day10/gui2.py similarity index 100% rename from Day01-15/Day10/code/gui2.py rename to Day01-15/code/Day10/gui2.py diff --git a/Day01-15/Day10/code/gui3.py b/Day01-15/code/Day10/gui3.py similarity index 100% rename from Day01-15/Day10/code/gui3.py rename to Day01-15/code/Day10/gui3.py diff --git a/Day01-15/Day10/code/renju.py b/Day01-15/code/Day10/renju.py similarity index 97% rename from Day01-15/Day10/code/renju.py rename to Day01-15/code/Day10/renju.py index 89cea1e..5f8cd88 100644 --- a/Day01-15/Day10/code/renju.py +++ b/Day01-15/code/Day10/renju.py @@ -1,78 +1,78 @@ -import pygame - -EMPTY = 0 -BLACK = 1 -WHITE = 2 - -black_color = [0, 0, 0] -white_color = [255, 255, 255] - - -class RenjuBoard(object): - - def __init__(self): - self._board = [[]] * 15 - self.reset() - - def reset(self): - for row in range(len(self._board)): - self._board[row] = [EMPTY] * 15 - - def move(self, row, col, is_black): - if self._board[row][col] == EMPTY: - self._board[row][col] = BLACK if is_black else WHITE - return True - return False - - def draw(self, screen): - for index in range(1, 16): - pygame.draw.line(screen, black_color, - [40, 40 * index], [600, 40 * index], 1) - pygame.draw.line(screen, black_color, - [40 * index, 40], [40 * index, 600], 1) - pygame.draw.rect(screen, black_color, [36, 36, 568, 568], 4) - pygame.draw.circle(screen, black_color, [320, 320], 5, 0) - pygame.draw.circle(screen, black_color, [160, 160], 5, 0) - pygame.draw.circle(screen, black_color, [480, 480], 5, 0) - pygame.draw.circle(screen, black_color, [480, 160], 5, 0) - pygame.draw.circle(screen, black_color, [160, 480], 5, 0) - for row in range(len(self._board)): - for col in range(len(self._board[row])): - if self._board[row][col] != EMPTY: - ccolor = black_color \ - if self._board[row][col] == BLACK else white_color - pos = [40 * (col + 1), 40 * (row + 1)] - pygame.draw.circle(screen, ccolor, pos, 20, 0) - - -def main(): - board = RenjuBoard() - is_black = True - pygame.init() - pygame.display.set_caption('五子棋') - screen = pygame.display.set_mode([640, 640]) - screen.fill([255, 255, 0]) - board.draw(screen) - pygame.display.flip() - running = True - while running: - for event in pygame.event.get(): - if event.type == pygame.QUIT: - running = False - elif event.type == pygame.KEYUP: - pass - elif event.type == pygame.MOUSEBUTTONDOWN\ - and event.button == 1: - x, y = event.pos - row = round((y - 40) / 40) - col = round((x - 40) / 40) - if board.move(row, col, is_black): - is_black = not is_black - screen.fill([255, 255, 0]) - board.draw(screen) - pygame.display.flip() - pygame.quit() - - -if __name__ == '__main__': - main() +import pygame + +EMPTY = 0 +BLACK = 1 +WHITE = 2 + +black_color = [0, 0, 0] +white_color = [255, 255, 255] + + +class RenjuBoard(object): + + def __init__(self): + self._board = [[]] * 15 + self.reset() + + def reset(self): + for row in range(len(self._board)): + self._board[row] = [EMPTY] * 15 + + def move(self, row, col, is_black): + if self._board[row][col] == EMPTY: + self._board[row][col] = BLACK if is_black else WHITE + return True + return False + + def draw(self, screen): + for index in range(1, 16): + pygame.draw.line(screen, black_color, + [40, 40 * index], [600, 40 * index], 1) + pygame.draw.line(screen, black_color, + [40 * index, 40], [40 * index, 600], 1) + pygame.draw.rect(screen, black_color, [36, 36, 568, 568], 4) + pygame.draw.circle(screen, black_color, [320, 320], 5, 0) + pygame.draw.circle(screen, black_color, [160, 160], 5, 0) + pygame.draw.circle(screen, black_color, [480, 480], 5, 0) + pygame.draw.circle(screen, black_color, [480, 160], 5, 0) + pygame.draw.circle(screen, black_color, [160, 480], 5, 0) + for row in range(len(self._board)): + for col in range(len(self._board[row])): + if self._board[row][col] != EMPTY: + ccolor = black_color \ + if self._board[row][col] == BLACK else white_color + pos = [40 * (col + 1), 40 * (row + 1)] + pygame.draw.circle(screen, ccolor, pos, 20, 0) + + +def main(): + board = RenjuBoard() + is_black = True + pygame.init() + pygame.display.set_caption('五子棋') + screen = pygame.display.set_mode([640, 640]) + screen.fill([255, 255, 0]) + board.draw(screen) + pygame.display.flip() + running = True + while running: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False + elif event.type == pygame.KEYUP: + pass + elif event.type == pygame.MOUSEBUTTONDOWN\ + and event.button == 1: + x, y = event.pos + row = round((y - 40) / 40) + col = round((x - 40) / 40) + if board.move(row, col, is_black): + is_black = not is_black + screen.fill([255, 255, 0]) + board.draw(screen) + pygame.display.flip() + pygame.quit() + + +if __name__ == '__main__': + main() diff --git a/Day01-15/Day10/code/snake.py b/Day01-15/code/Day10/snake.py similarity index 96% rename from Day01-15/Day10/code/snake.py rename to Day01-15/code/Day10/snake.py index 71d7148..5147930 100644 --- a/Day01-15/Day10/code/snake.py +++ b/Day01-15/code/Day10/snake.py @@ -1,334 +1,334 @@ -from abc import ABCMeta, abstractmethod -from enum import Enum, unique -from random import randrange -from threading import Thread - -import pygame - - -class Color(object): - """颜色""" - - GRAY = (242, 242, 242) - BLACK = (0, 0, 0) - GREEN = (0, 255, 0) - PINK = (255, 20, 147) - - -@unique -class Direction(Enum): - """方向""" - - UP = 0 - RIGHT = 1 - DOWN = 2 - LEFT = 3 - - -class GameObject(object, metaclass=ABCMeta): - """游戏中的对象""" - - def __init__(self, x=0, y=0, color=Color.BLACK): - """ - 初始化方法 - - :param x: 横坐标 - :param y: 纵坐标 - :param color: 颜色 - """ - self._x = x - self._y = y - self._color = color - - @property - def x(self): - return self._x - - @property - def y(self): - return self._y - - @abstractmethod - def draw(self, screen): - """ - 绘制 - - :param screen: 屏幕 - """ - pass - - -class Wall(GameObject): - """围墙""" - - def __init__(self, x, y, width, height, color=Color.BLACK): - """ - 初始化方法 - - :param x: 横坐标 - :param y: 纵坐标 - :param width: 宽度 - :param height: 高度 - :param color: 颜色 - """ - super().__init__(x, y, color) - self._width = width - self._height = height - - @property - def width(self): - return self._width - - @property - def height(self): - return self._height - - def draw(self, screen): - pygame.draw.rect(screen, self._color, - (self._x, self._y, self._width, self._height), 4) - - -class Food(GameObject): - """食物""" - - def __init__(self, x, y, size, color=Color.PINK): - """ - 初始化方法 - - :param x: 横坐标 - :param y: 纵坐标 - :param size: 大小 - :param color: 颜色 - """ - super().__init__(x, y, color) - self._size = size - self._hidden = False - - def draw(self, screen): - if not self._hidden: - pygame.draw.circle(screen, self._color, - (self._x + self._size // 2, self._y + self._size // 2), - self._size // 2, 0) - self._hidden = not self._hidden - - -class SnakeNode(GameObject): - """蛇身上的节点""" - - def __init__(self, x, y, size, color=Color.GREEN): - """ - 初始化方法 - - :param x: 横坐标 - :param y: 纵坐标 - :param size: 大小 - :param color: 颜色 - """ - super().__init__(x, y, color) - self._size = size - - @property - def size(self): - return self._size - - def draw(self, screen): - pygame.draw.rect(screen, self._color, - (self._x, self._y, self._size, self._size), 0) - pygame.draw.rect(screen, Color.BLACK, - (self._x, self._y, self._size, self._size), 1) - - -class Snake(GameObject): - """蛇""" - - def __init__(self, x, y, size=20, length=5): - """ - 初始化方法 - - :param x: 横坐标 - :param y: 纵坐标 - :param size: 大小 - :param length: 初始长度 - """ - super().__init__() - self._dir = Direction.LEFT - self._nodes = [] - self._alive = True - self._new_dir = None - for index in range(length): - node = SnakeNode(x + index * size, y, size) - self._nodes.append(node) - - @property - def dir(self): - return self._dir - - @property - def alive(self): - return self._alive - - @property - def head(self): - return self._nodes[0] - - def change_dir(self, new_dir): - """ - 改变方向 - - :param new_dir: 新方向 - """ - if new_dir != self._dir and \ - (self._dir.value + new_dir.value) % 2 != 0: - self._new_dir = new_dir - - def move(self): - """移动""" - if self._new_dir: - self._dir, self._new_dir = self._new_dir, None - snake_dir = self._dir - x, y, size = self.head.x, self.head.y, self.head.size - if snake_dir == Direction.UP: - y -= size - elif snake_dir == Direction.RIGHT: - x += size - elif snake_dir == Direction.DOWN: - y += size - else: - x -= size - new_head = SnakeNode(x, y, size) - self._nodes.insert(0, new_head) - self._nodes.pop() - - def collide(self, wall): - """ - 撞墙 - - :param wall: 围墙 - """ - head = self.head - if head.x < wall.x or head.x + head.size > wall.x + wall.width \ - or head.y < wall.y or head.y + head.size > wall.y + wall.height: - self._alive = False - - def eat_food(self, food): - """ - 吃食物 - - :param food: 食物 - - :return: 吃到食物返回True否则返回False - """ - if self.head.x == food.x and self.head.y == food.y: - tail = self._nodes[-1] - self._nodes.append(tail) - return True - return False - - def eat_self(self): - """咬自己""" - for index in range(4, len(self._nodes)): - node = self._nodes[index] - if node.x == self.head.x and node.y == self.head.y: - self._alive = False - - def draw(self, screen): - for node in self._nodes: - node.draw(screen) - - -def main(): - - def refresh(): - """刷新游戏窗口""" - screen.fill(Color.GRAY) - wall.draw(screen) - food.draw(screen) - snake.draw(screen) - pygame.display.flip() - - def handle_key_event(key_event): - """处理按键事件""" - key = key_event.key - if key == pygame.K_F2: - reset_game() - elif key in (pygame.K_a, pygame.K_w, pygame.K_d, pygame.K_s): - if snake.alive: - if key == pygame.K_w: - new_dir = Direction.UP - elif key == pygame.K_d: - new_dir = Direction.RIGHT - elif key == pygame.K_s: - new_dir = Direction.DOWN - else: - new_dir = Direction.LEFT - snake.change_dir(new_dir) - - def create_food(): - """创建食物""" - unit_size = snake.head.size - max_row = wall.height // unit_size - max_col = wall.width // unit_size - row = randrange(0, max_row) - col = randrange(0, max_col) - return Food(wall.x + unit_size * col, wall.y + unit_size * row, unit_size) - - def reset_game(): - """重置游戏""" - nonlocal food, snake - food = create_food() - snake = Snake(250, 290) - - def background_task(): - nonlocal running, food - while running: - if snake.alive: - refresh() - clock.tick(10) - if snake.alive: - snake.move() - snake.collide(wall) - if snake.eat_food(food): - food = create_food() - snake.eat_self() - - """ - class BackgroundTask(Thread): - - def run(self): - nonlocal running, food - while running: - if snake.alive: - refresh() - clock.tick(10) - if snake.alive: - snake.move() - snake.collide(wall) - if snake.eat_food(food): - food = create_food() - snake.eat_self() - """ - - wall = Wall(10, 10, 600, 600) - snake = Snake(250, 290) - food = create_food() - pygame.init() - screen = pygame.display.set_mode((620, 620)) - pygame.display.set_caption('贪吃蛇') - # 创建控制游戏每秒帧数的时钟 - clock = pygame.time.Clock() - running = True - # 启动后台线程负责刷新窗口和让蛇移动 - # BackgroundTask().start() - Thread(target=background_task).start() - # 处理事件的消息循环 - while running: - for event in pygame.event.get(): - if event.type == pygame.QUIT: - running = False - elif event.type == pygame.KEYDOWN: - handle_key_event(event) - pygame.quit() - - -if __name__ == '__main__': - main() +from abc import ABCMeta, abstractmethod +from enum import Enum, unique +from random import randrange +from threading import Thread + +import pygame + + +class Color(object): + """颜色""" + + GRAY = (242, 242, 242) + BLACK = (0, 0, 0) + GREEN = (0, 255, 0) + PINK = (255, 20, 147) + + +@unique +class Direction(Enum): + """方向""" + + UP = 0 + RIGHT = 1 + DOWN = 2 + LEFT = 3 + + +class GameObject(object, metaclass=ABCMeta): + """游戏中的对象""" + + def __init__(self, x=0, y=0, color=Color.BLACK): + """ + 初始化方法 + + :param x: 横坐标 + :param y: 纵坐标 + :param color: 颜色 + """ + self._x = x + self._y = y + self._color = color + + @property + def x(self): + return self._x + + @property + def y(self): + return self._y + + @abstractmethod + def draw(self, screen): + """ + 绘制 + + :param screen: 屏幕 + """ + pass + + +class Wall(GameObject): + """围墙""" + + def __init__(self, x, y, width, height, color=Color.BLACK): + """ + 初始化方法 + + :param x: 横坐标 + :param y: 纵坐标 + :param width: 宽度 + :param height: 高度 + :param color: 颜色 + """ + super().__init__(x, y, color) + self._width = width + self._height = height + + @property + def width(self): + return self._width + + @property + def height(self): + return self._height + + def draw(self, screen): + pygame.draw.rect(screen, self._color, + (self._x, self._y, self._width, self._height), 4) + + +class Food(GameObject): + """食物""" + + def __init__(self, x, y, size, color=Color.PINK): + """ + 初始化方法 + + :param x: 横坐标 + :param y: 纵坐标 + :param size: 大小 + :param color: 颜色 + """ + super().__init__(x, y, color) + self._size = size + self._hidden = False + + def draw(self, screen): + if not self._hidden: + pygame.draw.circle(screen, self._color, + (self._x + self._size // 2, self._y + self._size // 2), + self._size // 2, 0) + self._hidden = not self._hidden + + +class SnakeNode(GameObject): + """蛇身上的节点""" + + def __init__(self, x, y, size, color=Color.GREEN): + """ + 初始化方法 + + :param x: 横坐标 + :param y: 纵坐标 + :param size: 大小 + :param color: 颜色 + """ + super().__init__(x, y, color) + self._size = size + + @property + def size(self): + return self._size + + def draw(self, screen): + pygame.draw.rect(screen, self._color, + (self._x, self._y, self._size, self._size), 0) + pygame.draw.rect(screen, Color.BLACK, + (self._x, self._y, self._size, self._size), 1) + + +class Snake(GameObject): + """蛇""" + + def __init__(self, x, y, size=20, length=5): + """ + 初始化方法 + + :param x: 横坐标 + :param y: 纵坐标 + :param size: 大小 + :param length: 初始长度 + """ + super().__init__() + self._dir = Direction.LEFT + self._nodes = [] + self._alive = True + self._new_dir = None + for index in range(length): + node = SnakeNode(x + index * size, y, size) + self._nodes.append(node) + + @property + def dir(self): + return self._dir + + @property + def alive(self): + return self._alive + + @property + def head(self): + return self._nodes[0] + + def change_dir(self, new_dir): + """ + 改变方向 + + :param new_dir: 新方向 + """ + if new_dir != self._dir and \ + (self._dir.value + new_dir.value) % 2 != 0: + self._new_dir = new_dir + + def move(self): + """移动""" + if self._new_dir: + self._dir, self._new_dir = self._new_dir, None + snake_dir = self._dir + x, y, size = self.head.x, self.head.y, self.head.size + if snake_dir == Direction.UP: + y -= size + elif snake_dir == Direction.RIGHT: + x += size + elif snake_dir == Direction.DOWN: + y += size + else: + x -= size + new_head = SnakeNode(x, y, size) + self._nodes.insert(0, new_head) + self._nodes.pop() + + def collide(self, wall): + """ + 撞墙 + + :param wall: 围墙 + """ + head = self.head + if head.x < wall.x or head.x + head.size > wall.x + wall.width \ + or head.y < wall.y or head.y + head.size > wall.y + wall.height: + self._alive = False + + def eat_food(self, food): + """ + 吃食物 + + :param food: 食物 + + :return: 吃到食物返回True否则返回False + """ + if self.head.x == food.x and self.head.y == food.y: + tail = self._nodes[-1] + self._nodes.append(tail) + return True + return False + + def eat_self(self): + """咬自己""" + for index in range(4, len(self._nodes)): + node = self._nodes[index] + if node.x == self.head.x and node.y == self.head.y: + self._alive = False + + def draw(self, screen): + for node in self._nodes: + node.draw(screen) + + +def main(): + + def refresh(): + """刷新游戏窗口""" + screen.fill(Color.GRAY) + wall.draw(screen) + food.draw(screen) + snake.draw(screen) + pygame.display.flip() + + def handle_key_event(key_event): + """处理按键事件""" + key = key_event.key + if key == pygame.K_F2: + reset_game() + elif key in (pygame.K_a, pygame.K_w, pygame.K_d, pygame.K_s): + if snake.alive: + if key == pygame.K_w: + new_dir = Direction.UP + elif key == pygame.K_d: + new_dir = Direction.RIGHT + elif key == pygame.K_s: + new_dir = Direction.DOWN + else: + new_dir = Direction.LEFT + snake.change_dir(new_dir) + + def create_food(): + """创建食物""" + unit_size = snake.head.size + max_row = wall.height // unit_size + max_col = wall.width // unit_size + row = randrange(0, max_row) + col = randrange(0, max_col) + return Food(wall.x + unit_size * col, wall.y + unit_size * row, unit_size) + + def reset_game(): + """重置游戏""" + nonlocal food, snake + food = create_food() + snake = Snake(250, 290) + + def background_task(): + nonlocal running, food + while running: + if snake.alive: + refresh() + clock.tick(10) + if snake.alive: + snake.move() + snake.collide(wall) + if snake.eat_food(food): + food = create_food() + snake.eat_self() + + """ + class BackgroundTask(Thread): + + def run(self): + nonlocal running, food + while running: + if snake.alive: + refresh() + clock.tick(10) + if snake.alive: + snake.move() + snake.collide(wall) + if snake.eat_food(food): + food = create_food() + snake.eat_self() + """ + + wall = Wall(10, 10, 600, 600) + snake = Snake(250, 290) + food = create_food() + pygame.init() + screen = pygame.display.set_mode((620, 620)) + pygame.display.set_caption('贪吃蛇') + # 创建控制游戏每秒帧数的时钟 + clock = pygame.time.Clock() + running = True + # 启动后台线程负责刷新窗口和让蛇移动 + # BackgroundTask().start() + Thread(target=background_task).start() + # 处理事件的消息循环 + while running: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False + elif event.type == pygame.KEYDOWN: + handle_key_event(event) + pygame.quit() + + +if __name__ == '__main__': + main() diff --git a/Day01-15/Day10/code/turtle1.py b/Day01-15/code/Day10/turtle1.py similarity index 100% rename from Day01-15/Day10/code/turtle1.py rename to Day01-15/code/Day10/turtle1.py diff --git a/Day01-15/Day11/code/csv1.py b/Day01-15/code/Day11/csv1.py similarity index 100% rename from Day01-15/Day11/code/csv1.py rename to Day01-15/code/Day11/csv1.py diff --git a/Day01-15/Day11/code/csv2.py b/Day01-15/code/Day11/csv2.py similarity index 100% rename from Day01-15/Day11/code/csv2.py rename to Day01-15/code/Day11/csv2.py diff --git a/Day01-15/Day11/code/ex1.py b/Day01-15/code/Day11/ex1.py similarity index 100% rename from Day01-15/Day11/code/ex1.py rename to Day01-15/code/Day11/ex1.py diff --git a/Day01-15/Day11/code/ex2.py b/Day01-15/code/Day11/ex2.py similarity index 100% rename from Day01-15/Day11/code/ex2.py rename to Day01-15/code/Day11/ex2.py diff --git a/Day01-15/Day11/code/ex3.py b/Day01-15/code/Day11/ex3.py similarity index 100% rename from Day01-15/Day11/code/ex3.py rename to Day01-15/code/Day11/ex3.py diff --git a/Day01-15/Day11/code/ex4.py b/Day01-15/code/Day11/ex4.py similarity index 100% rename from Day01-15/Day11/code/ex4.py rename to Day01-15/code/Day11/ex4.py diff --git a/Day01-15/Day11/code/example.csv b/Day01-15/code/Day11/example.csv similarity index 100% rename from Day01-15/Day11/code/example.csv rename to Day01-15/code/Day11/example.csv diff --git a/Day01-15/Day11/code/file1.py b/Day01-15/code/Day11/file1.py similarity index 100% rename from Day01-15/Day11/code/file1.py rename to Day01-15/code/Day11/file1.py diff --git a/Day01-15/Day11/code/file2.py b/Day01-15/code/Day11/file2.py similarity index 100% rename from Day01-15/Day11/code/file2.py rename to Day01-15/code/Day11/file2.py diff --git a/Day01-15/Day11/code/file3.py b/Day01-15/code/Day11/file3.py similarity index 100% rename from Day01-15/Day11/code/file3.py rename to Day01-15/code/Day11/file3.py diff --git a/Day01-15/Day11/code/file4.py b/Day01-15/code/Day11/file4.py similarity index 100% rename from Day01-15/Day11/code/file4.py rename to Day01-15/code/Day11/file4.py diff --git a/Day01-15/Day11/code/json1.py b/Day01-15/code/Day11/json1.py similarity index 100% rename from Day01-15/Day11/code/json1.py rename to Day01-15/code/Day11/json1.py diff --git a/Day01-15/Day11/code/json2.py b/Day01-15/code/Day11/json2.py similarity index 100% rename from Day01-15/Day11/code/json2.py rename to Day01-15/code/Day11/json2.py diff --git a/Day01-15/Day11/code/mm.jpg b/Day01-15/code/Day11/mm.jpg similarity index 100% rename from Day01-15/Day11/code/mm.jpg rename to Day01-15/code/Day11/mm.jpg diff --git a/Day01-15/Day11/code/pi_million_digits.txt b/Day01-15/code/Day11/pi_million_digits.txt similarity index 100% rename from Day01-15/Day11/code/pi_million_digits.txt rename to Day01-15/code/Day11/pi_million_digits.txt diff --git a/Day01-15/Day11/code/teacher.csv b/Day01-15/code/Day11/teacher.csv similarity index 100% rename from Day01-15/Day11/code/teacher.csv rename to Day01-15/code/Day11/teacher.csv diff --git a/Day01-15/Day11/code/致橡树.txt b/Day01-15/code/Day11/致橡树.txt similarity index 100% rename from Day01-15/Day11/code/致橡树.txt rename to Day01-15/code/Day11/致橡树.txt diff --git a/Day01-15/Day12/code/str1.py b/Day01-15/code/Day12/str1.py similarity index 100% rename from Day01-15/Day12/code/str1.py rename to Day01-15/code/Day12/str1.py diff --git a/Day01-15/Day12/code/str2.py b/Day01-15/code/Day12/str2.py similarity index 100% rename from Day01-15/Day12/code/str2.py rename to Day01-15/code/Day12/str2.py diff --git a/Day01-15/Day12/code/test3.py b/Day01-15/code/Day12/test3.py similarity index 100% rename from Day01-15/Day12/code/test3.py rename to Day01-15/code/Day12/test3.py diff --git a/Day01-15/Day12/code/test4.py b/Day01-15/code/Day12/test4.py similarity index 100% rename from Day01-15/Day12/code/test4.py rename to Day01-15/code/Day12/test4.py diff --git a/Day01-15/Day12/code/test5.py b/Day01-15/code/Day12/test5.py similarity index 100% rename from Day01-15/Day12/code/test5.py rename to Day01-15/code/Day12/test5.py diff --git a/Day01-15/Day13/code/asyncio1.py b/Day01-15/code/Day13/asyncio1.py similarity index 100% rename from Day01-15/Day13/code/asyncio1.py rename to Day01-15/code/Day13/asyncio1.py diff --git a/Day01-15/Day13/code/asyncio2.py b/Day01-15/code/Day13/asyncio2.py similarity index 100% rename from Day01-15/Day13/code/asyncio2.py rename to Day01-15/code/Day13/asyncio2.py diff --git a/Day01-15/Day13/code/asyncio3.py b/Day01-15/code/Day13/asyncio3.py similarity index 100% rename from Day01-15/Day13/code/asyncio3.py rename to Day01-15/code/Day13/asyncio3.py diff --git a/Day01-15/Day13/code/coroutine1.py b/Day01-15/code/Day13/coroutine1.py similarity index 100% rename from Day01-15/Day13/code/coroutine1.py rename to Day01-15/code/Day13/coroutine1.py diff --git a/Day01-15/Day13/code/coroutine2.py b/Day01-15/code/Day13/coroutine2.py similarity index 100% rename from Day01-15/Day13/code/coroutine2.py rename to Day01-15/code/Day13/coroutine2.py diff --git a/Day01-15/Day13/code/generator1.py b/Day01-15/code/Day13/generator1.py similarity index 100% rename from Day01-15/Day13/code/generator1.py rename to Day01-15/code/Day13/generator1.py diff --git a/Day01-15/Day13/code/generator2.py b/Day01-15/code/Day13/generator2.py similarity index 100% rename from Day01-15/Day13/code/generator2.py rename to Day01-15/code/Day13/generator2.py diff --git a/Day01-15/Day13/code/multiprocess1.py b/Day01-15/code/Day13/multiprocess1.py similarity index 100% rename from Day01-15/Day13/code/multiprocess1.py rename to Day01-15/code/Day13/multiprocess1.py diff --git a/Day01-15/Day13/code/multiprocess2.py b/Day01-15/code/Day13/multiprocess2.py similarity index 100% rename from Day01-15/Day13/code/multiprocess2.py rename to Day01-15/code/Day13/multiprocess2.py diff --git a/Day01-15/Day13/code/multiprocess3.py b/Day01-15/code/Day13/multiprocess3.py similarity index 100% rename from Day01-15/Day13/code/multiprocess3.py rename to Day01-15/code/Day13/multiprocess3.py diff --git a/Day01-15/Day13/code/multiprocess4.py b/Day01-15/code/Day13/multiprocess4.py similarity index 100% rename from Day01-15/Day13/code/multiprocess4.py rename to Day01-15/code/Day13/multiprocess4.py diff --git a/Day01-15/Day13/code/multithread1.py b/Day01-15/code/Day13/multithread1.py similarity index 100% rename from Day01-15/Day13/code/multithread1.py rename to Day01-15/code/Day13/multithread1.py diff --git a/Day01-15/Day13/code/multithread2.py b/Day01-15/code/Day13/multithread2.py similarity index 100% rename from Day01-15/Day13/code/multithread2.py rename to Day01-15/code/Day13/multithread2.py diff --git a/Day01-15/Day13/code/multithread3.py b/Day01-15/code/Day13/multithread3.py similarity index 100% rename from Day01-15/Day13/code/multithread3.py rename to Day01-15/code/Day13/multithread3.py diff --git a/Day01-15/Day13/code/multithread4.py b/Day01-15/code/Day13/multithread4.py similarity index 100% rename from Day01-15/Day13/code/multithread4.py rename to Day01-15/code/Day13/multithread4.py diff --git a/Day01-15/Day13/code/multithread5.py b/Day01-15/code/Day13/multithread5.py similarity index 100% rename from Day01-15/Day13/code/multithread5.py rename to Day01-15/code/Day13/multithread5.py diff --git a/Day01-15/Day13/code/multithread6.py b/Day01-15/code/Day13/multithread6.py similarity index 100% rename from Day01-15/Day13/code/multithread6.py rename to Day01-15/code/Day13/multithread6.py diff --git a/Day01-15/Day13/code/singlethread1.py b/Day01-15/code/Day13/singlethread1.py similarity index 100% rename from Day01-15/Day13/code/singlethread1.py rename to Day01-15/code/Day13/singlethread1.py diff --git a/Day01-15/Day13/code/singlethread2.py b/Day01-15/code/Day13/singlethread2.py similarity index 100% rename from Day01-15/Day13/code/singlethread2.py rename to Day01-15/code/Day13/singlethread2.py diff --git a/Day01-15/Day13/code/test2.py b/Day01-15/code/Day13/test2.py similarity index 100% rename from Day01-15/Day13/code/test2.py rename to Day01-15/code/Day13/test2.py diff --git a/Day01-15/Day13/code/test3.py b/Day01-15/code/Day13/test3.py similarity index 100% rename from Day01-15/Day13/code/test3.py rename to Day01-15/code/Day13/test3.py diff --git a/Day01-15/Day14-A/code/chatclient.py b/Day01-15/code/Day14/chatclient.py similarity index 100% rename from Day01-15/Day14-A/code/chatclient.py rename to Day01-15/code/Day14/chatclient.py diff --git a/Day01-15/Day14-A/code/chatserver.py b/Day01-15/code/Day14/chatserver.py similarity index 100% rename from Day01-15/Day14-A/code/chatserver.py rename to Day01-15/code/Day14/chatserver.py diff --git a/Day01-15/Day14-A/code/fileclient.py b/Day01-15/code/Day14/fileclient.py similarity index 100% rename from Day01-15/Day14-A/code/fileclient.py rename to Day01-15/code/Day14/fileclient.py diff --git a/Day01-15/Day14-A/code/fileserver.py b/Day01-15/code/Day14/fileserver.py similarity index 100% rename from Day01-15/Day14-A/code/fileserver.py rename to Day01-15/code/Day14/fileserver.py diff --git a/Day01-15/Day14-A/code/guido.jpg b/Day01-15/code/Day14/guido.jpg similarity index 100% rename from Day01-15/Day14-A/code/guido.jpg rename to Day01-15/code/Day14/guido.jpg diff --git a/Day01-15/Day14-A/code/mmdownloader.py b/Day01-15/code/Day14/mmdownloader.py similarity index 100% rename from Day01-15/Day14-A/code/mmdownloader.py rename to Day01-15/code/Day14/mmdownloader.py diff --git a/Day01-15/Day14-A/code/socket1.py b/Day01-15/code/Day14/socket1.py similarity index 100% rename from Day01-15/Day14-A/code/socket1.py rename to Day01-15/code/Day14/socket1.py diff --git a/Day01-15/Day14-A/code/socket2.py b/Day01-15/code/Day14/socket2.py similarity index 100% rename from Day01-15/Day14-A/code/socket2.py rename to Day01-15/code/Day14/socket2.py diff --git a/Day01-15/Day14-A/code/socket3.py b/Day01-15/code/Day14/socket3.py similarity index 100% rename from Day01-15/Day14-A/code/socket3.py rename to Day01-15/code/Day14/socket3.py diff --git a/Day01-15/Day14-A/code/socket4.py b/Day01-15/code/Day14/socket4.py similarity index 100% rename from Day01-15/Day14-A/code/socket4.py rename to Day01-15/code/Day14/socket4.py diff --git a/Day01-15/Day14-A/code/socket5.py b/Day01-15/code/Day14/socket5.py similarity index 100% rename from Day01-15/Day14-A/code/socket5.py rename to Day01-15/code/Day14/socket5.py diff --git a/Day01-15/Day14-A/code/timeclient.py b/Day01-15/code/Day14/timeclient.py similarity index 100% rename from Day01-15/Day14-A/code/timeclient.py rename to Day01-15/code/Day14/timeclient.py diff --git a/Day01-15/Day14-A/code/timeserver.py b/Day01-15/code/Day14/timeserver.py similarity index 100% rename from Day01-15/Day14-A/code/timeserver.py rename to Day01-15/code/Day14/timeserver.py diff --git a/Day01-15/Day15/code/excel1.py b/Day01-15/code/Day15/excel1.py similarity index 100% rename from Day01-15/Day15/code/excel1.py rename to Day01-15/code/Day15/excel1.py diff --git a/Day01-15/Day15/code/excel2.py b/Day01-15/code/Day15/excel2.py similarity index 100% rename from Day01-15/Day15/code/excel2.py rename to Day01-15/code/Day15/excel2.py diff --git a/Day01-15/Day15/code/pdf1.py b/Day01-15/code/Day15/pdf1.py similarity index 100% rename from Day01-15/Day15/code/pdf1.py rename to Day01-15/code/Day15/pdf1.py diff --git a/Day01-15/Day15/code/pdf2.py b/Day01-15/code/Day15/pdf2.py similarity index 100% rename from Day01-15/Day15/code/pdf2.py rename to Day01-15/code/Day15/pdf2.py diff --git a/Day01-15/Day15/code/pillow1.py b/Day01-15/code/Day15/pillow1.py similarity index 100% rename from Day01-15/Day15/code/pillow1.py rename to Day01-15/code/Day15/pillow1.py diff --git a/Day01-15/Day15/code/res/Docker入门教程.pdf b/Day01-15/code/Day15/res/Docker入门教程.pdf similarity index 100% rename from Day01-15/Day15/code/res/Docker入门教程.pdf rename to Day01-15/code/Day15/res/Docker入门教程.pdf diff --git a/Day01-15/Day15/code/res/guido.jpg b/Day01-15/code/Day15/res/guido.jpg similarity index 100% rename from Day01-15/Day15/code/res/guido.jpg rename to Day01-15/code/Day15/res/guido.jpg diff --git a/Day01-15/Day15/code/res/luohao.png b/Day01-15/code/Day15/res/luohao.png similarity index 100% rename from Day01-15/Day15/code/res/luohao.png rename to Day01-15/code/Day15/res/luohao.png diff --git a/Day01-15/Day15/code/res/学生明细表.xlsx b/Day01-15/code/Day15/res/学生明细表.xlsx similarity index 100% rename from Day01-15/Day15/code/res/学生明细表.xlsx rename to Day01-15/code/Day15/res/学生明细表.xlsx diff --git a/Day01-15/Day15/code/res/用函数还是用复杂的表达式.docx b/Day01-15/code/Day15/res/用函数还是用复杂的表达式.docx similarity index 100% rename from Day01-15/Day15/code/res/用函数还是用复杂的表达式.docx rename to Day01-15/code/Day15/res/用函数还是用复杂的表达式.docx diff --git a/Day01-15/Day15/code/word1.py b/Day01-15/code/Day15/word1.py similarity index 100% rename from Day01-15/Day15/code/word1.py rename to Day01-15/code/Day15/word1.py diff --git a/Day01-15/Day15/code/word2.py b/Day01-15/code/Day15/word2.py similarity index 100% rename from Day01-15/Day15/code/word2.py rename to Day01-15/code/Day15/word2.py diff --git a/Day01-15/Day14-A/res/TCP-IP-model.png b/Day01-15/res/TCP-IP-model.png similarity index 100% rename from Day01-15/Day14-A/res/TCP-IP-model.png rename to Day01-15/res/TCP-IP-model.png diff --git a/Day01-15/Day14-A/res/after-browser.jpg b/Day01-15/res/after-browser.jpg similarity index 100% rename from Day01-15/Day14-A/res/after-browser.jpg rename to Day01-15/res/after-browser.jpg diff --git a/Day01-15/Day14-A/res/arpanet.png b/Day01-15/res/arpanet.png similarity index 100% rename from Day01-15/Day14-A/res/arpanet.png rename to Day01-15/res/arpanet.png diff --git a/Day01-15/Day10/res/ball-game.png b/Day01-15/res/ball-game.png similarity index 100% rename from Day01-15/Day10/res/ball-game.png rename to Day01-15/res/ball-game.png diff --git a/Day01-15/Day10/res/ball.png b/Day01-15/res/ball.png similarity index 100% rename from Day01-15/Day10/res/ball.png rename to Day01-15/res/ball.png diff --git a/Day01-15/Day14-A/res/before-browser.jpg b/Day01-15/res/before-browser.jpg similarity index 100% rename from Day01-15/Day14-A/res/before-browser.jpg rename to Day01-15/res/before-browser.jpg diff --git a/Day01-15/Day14-A/res/browers.jpg b/Day01-15/res/browers.jpg similarity index 100% rename from Day01-15/Day14-A/res/browers.jpg rename to Day01-15/res/browers.jpg diff --git a/Day01-15/Day14-A/res/browser-market-place.jpeg b/Day01-15/res/browser-market-place.jpeg similarity index 100% rename from Day01-15/Day14-A/res/browser-market-place.jpeg rename to Day01-15/res/browser-market-place.jpeg diff --git a/Day01-15/Day07/res/fibonacci-blocks.png b/Day01-15/res/fibonacci-blocks.png similarity index 100% rename from Day01-15/Day07/res/fibonacci-blocks.png rename to Day01-15/res/fibonacci-blocks.png diff --git a/Day01-15/Day11/res/file-open-mode.png b/Day01-15/res/file-open-mode.png similarity index 100% rename from Day01-15/Day11/res/file-open-mode.png rename to Day01-15/res/file-open-mode.png diff --git a/Day01-15/res/formula_1.png b/Day01-15/res/formula_1.png new file mode 100644 index 0000000..5a0a7e2 Binary files /dev/null and b/Day01-15/res/formula_1.png differ diff --git a/Day01-15/res/formula_2.png b/Day01-15/res/formula_2.png new file mode 100644 index 0000000..c21c2ae Binary files /dev/null and b/Day01-15/res/formula_2.png differ diff --git a/Day01-15/res/formula_3.png b/Day01-15/res/formula_3.png new file mode 100644 index 0000000..b9a84ed Binary files /dev/null and b/Day01-15/res/formula_3.png differ diff --git a/Day01-15/res/formula_4.png b/Day01-15/res/formula_4.png new file mode 100644 index 0000000..692fb08 Binary files /dev/null and b/Day01-15/res/formula_4.png differ diff --git a/Day01-15/res/formula_5.png b/Day01-15/res/formula_5.png new file mode 100644 index 0000000..28fad34 Binary files /dev/null and b/Day01-15/res/formula_5.png differ diff --git a/Day01-15/res/formula_6.png b/Day01-15/res/formula_6.png new file mode 100644 index 0000000..86df5da Binary files /dev/null and b/Day01-15/res/formula_6.png differ diff --git a/Day01-15/res/formula_7.png b/Day01-15/res/formula_7.png new file mode 100644 index 0000000..92cd76d Binary files /dev/null and b/Day01-15/res/formula_7.png differ diff --git a/Day01-15/res/formula_8.png b/Day01-15/res/formula_8.png new file mode 100644 index 0000000..a37f92b Binary files /dev/null and b/Day01-15/res/formula_8.png differ diff --git a/Day01-15/Day14-A/res/how-data-is-processed.jpg b/Day01-15/res/how-data-is-processed.jpg similarity index 100% rename from Day01-15/Day14-A/res/how-data-is-processed.jpg rename to Day01-15/res/how-data-is-processed.jpg diff --git a/Day01-15/Day15/res/image-crop.png b/Day01-15/res/image-crop.png similarity index 100% rename from Day01-15/Day15/res/image-crop.png rename to Day01-15/res/image-crop.png diff --git a/Day01-15/Day15/res/image-filter.png b/Day01-15/res/image-filter.png similarity index 100% rename from Day01-15/Day15/res/image-filter.png rename to Day01-15/res/image-filter.png diff --git a/Day01-15/Day15/res/image-paste.png b/Day01-15/res/image-paste.png similarity index 100% rename from Day01-15/Day15/res/image-paste.png rename to Day01-15/res/image-paste.png diff --git a/Day01-15/Day15/res/image-putpixel.png b/Day01-15/res/image-putpixel.png similarity index 100% rename from Day01-15/Day15/res/image-putpixel.png rename to Day01-15/res/image-putpixel.png diff --git a/Day01-15/Day15/res/image-rotate.png b/Day01-15/res/image-rotate.png similarity index 100% rename from Day01-15/Day15/res/image-rotate.png rename to Day01-15/res/image-rotate.png diff --git a/Day01-15/Day15/res/image-show.png b/Day01-15/res/image-show.png similarity index 100% rename from Day01-15/Day15/res/image-show.png rename to Day01-15/res/image-show.png diff --git a/Day01-15/Day15/res/image-thumbnail.png b/Day01-15/res/image-thumbnail.png similarity index 100% rename from Day01-15/Day15/res/image-thumbnail.png rename to Day01-15/res/image-thumbnail.png diff --git a/Day01-15/Day15/res/image-transpose.png b/Day01-15/res/image-transpose.png similarity index 100% rename from Day01-15/Day15/res/image-transpose.png rename to Day01-15/res/image-transpose.png diff --git a/Day01-15/Day07/res/ipython-timeit.png b/Day01-15/res/ipython-timeit.png similarity index 100% rename from Day01-15/Day07/res/ipython-timeit.png rename to Day01-15/res/ipython-timeit.png diff --git a/Day01-15/Day13/res/macos-monitor.png b/Day01-15/res/macos-monitor.png similarity index 100% rename from Day01-15/Day13/res/macos-monitor.png rename to Day01-15/res/macos-monitor.png diff --git a/Day01-15/Day08/res/object-feature.png b/Day01-15/res/object-feature.png similarity index 100% rename from Day01-15/Day08/res/object-feature.png rename to Day01-15/res/object-feature.png diff --git a/Day01-15/Day08/res/oop-zhihu.png b/Day01-15/res/oop-zhihu.png similarity index 100% rename from Day01-15/Day08/res/oop-zhihu.png rename to Day01-15/res/oop-zhihu.png diff --git a/Day01-15/Day14-A/res/osi_rm.gif b/Day01-15/res/osi_rm.gif similarity index 100% rename from Day01-15/Day14-A/res/osi_rm.gif rename to Day01-15/res/osi_rm.gif diff --git a/Day01-15/Day14-A/res/osimodel.png b/Day01-15/res/osimodel.png similarity index 100% rename from Day01-15/Day14-A/res/osimodel.png rename to Day01-15/res/osimodel.png diff --git a/Day01-15/Day01/res/python-idle.png b/Day01-15/res/python-idle.png similarity index 100% rename from Day01-15/Day01/res/python-idle.png rename to Day01-15/res/python-idle.png diff --git a/Day01-15/Day01/res/python-ipython.png b/Day01-15/res/python-ipython.png similarity index 100% rename from Day01-15/Day01/res/python-ipython.png rename to Day01-15/res/python-ipython.png diff --git a/Day01-15/Day01/res/python-jupyter-1.png b/Day01-15/res/python-jupyter-1.png similarity index 100% rename from Day01-15/Day01/res/python-jupyter-1.png rename to Day01-15/res/python-jupyter-1.png diff --git a/Day01-15/Day01/res/python-jupyter-2.png b/Day01-15/res/python-jupyter-2.png similarity index 100% rename from Day01-15/Day01/res/python-jupyter-2.png rename to Day01-15/res/python-jupyter-2.png diff --git a/Day01-15/Day01/res/python-pycharm.png b/Day01-15/res/python-pycharm.png similarity index 100% rename from Day01-15/Day01/res/python-pycharm.png rename to Day01-15/res/python-pycharm.png diff --git a/Day01-15/Day07/res/python-set.png b/Day01-15/res/python-set.png similarity index 100% rename from Day01-15/Day07/res/python-set.png rename to Day01-15/res/python-set.png diff --git a/Day01-15/Day01/res/python-sublime.png b/Day01-15/res/python-sublime.png similarity index 100% rename from Day01-15/Day01/res/python-sublime.png rename to Day01-15/res/python-sublime.png diff --git a/Day01-15/Day14-A/res/tcpipprotocols.png b/Day01-15/res/tcpipprotocols.png similarity index 100% rename from Day01-15/Day14-A/res/tcpipprotocols.png rename to Day01-15/res/tcpipprotocols.png diff --git a/Day01-15/Day12/res/tel-start-number.png b/Day01-15/res/tel-start-number.png similarity index 100% rename from Day01-15/Day12/res/tel-start-number.png rename to Day01-15/res/tel-start-number.png diff --git a/Day01-15/Day14-A/res/telnet.png b/Day01-15/res/telnet.png similarity index 100% rename from Day01-15/Day14-A/res/telnet.png rename to Day01-15/res/telnet.png diff --git a/Day01-15/Day09/res/uml-components.png b/Day01-15/res/uml-components.png similarity index 100% rename from Day01-15/Day09/res/uml-components.png rename to Day01-15/res/uml-components.png diff --git a/Day01-15/Day09/res/uml-example.png b/Day01-15/res/uml-example.png similarity index 100% rename from Day01-15/Day09/res/uml-example.png rename to Day01-15/res/uml-example.png diff --git a/Day16-20/Python语言进阶.md b/Day16-20/16.Python语言进阶.md similarity index 99% rename from Day16-20/Python语言进阶.md rename to Day16-20/16.Python语言进阶.md index ab7c6fc..b304f6e 100644 --- a/Day16-20/Python语言进阶.md +++ b/Day16-20/16.Python语言进阶.md @@ -198,7 +198,8 @@ - 常用算法: - 穷举法 - 又称为暴力破解法,对所有的可能性进行验证,直到找到正确答案。 - - 贪婪法 - 在对问题求解时,总是做出在当前看来是最好的选择,不追求最优解,快速找到满意解。 + - 贪婪法 - 在对问题求解时,总是做出在当前看来 + - 最好的选择,不追求最优解,快速找到满意解。 - 分治法 - 把一个复杂的问题分成两个或更多的相同或相似的子问题,再把子问题分成更小的子问题,直到可以直接求解的程度,最后将子问题的解进行合并得到原问题的解。 - 回溯法 - 回溯法又称为试探法,按选优条件向前搜索,当搜索到某一步发现原先选择并不优或达不到目标时,就退回一步重新选择。 - 动态规划 - 基本思想也是将待求解问题分解成若干个子问题,先求解并保存这些子问题的解,避免产生大量的重复运算。 @@ -218,7 +219,7 @@ # 第二天A第一个醒来 他将鱼分为5份 扔掉多余的1条 拿走自己的一份 # B第二个醒来 也将鱼分为5份 扔掉多余的1条 拿走自己的一份 # 然后C、D、E依次醒来也按同样的方式分鱼 问他们至少捕了多少条鱼 - fish = 1 + fish = 6 while True: total = fish enough = True @@ -231,7 +232,7 @@ if enough: print(fish) break - fish += 1 + fish += 5 ``` 贪婪法例子:假设小偷有一个背包,最多能装20公斤赃物,他闯入一户人家,发现如下表所示的物品。很显然,他不能把所有物品都装进背包,所以必须确定拿走哪些物品,留下哪些物品。 @@ -555,6 +556,7 @@ ```Python from functools import wraps + from threading import Lock def singleton(cls): @@ -767,6 +769,8 @@ main() ``` + > 说明:上面的代码中使用了Emoji字符来表示扑克牌的四种花色,在某些不支持Emoji字符的系统上可能无法显示。 + - 对象的复制(深复制/深拷贝/深度克隆和浅复制/浅拷贝/影子克隆) - 垃圾回收、循环引用和弱引用 diff --git a/Day16-20/code/root@39.100.102.28 b/Day16-20/code/root@39.100.102.28 new file mode 100644 index 0000000..315679e --- /dev/null +++ b/Day16-20/code/root@39.100.102.28 @@ -0,0 +1,50 @@ +""" +多进程和进程池的使用 +多线程因为GIL的存在不能够发挥CPU的多核特性 +对于计算密集型任务应该考虑使用多进程 +time python3 example22.py +real 0m11.512s +user 0m39.319s +sys 0m0.169s +""" +import concurrent.futures +import math + +PRIMES = [ + 1116281, + 1297337, + 104395303, + 472882027, + 533000389, + 817504243, + 982451653, + 112272535095293, + 112582705942171, + 112272535095293, + 115280095190773, + 115797848077099, + 1099726899285419 +] * 5 + + +def is_prime(n): + """判断素数""" + if n % 2 == 0: + return False + + sqrt_n = int(math.floor(math.sqrt(n))) + for i in range(3, sqrt_n + 1, 2): + if n % i == 0: + return False + return True + + +def main(): + """主函数""" + with concurrent.futures.ProcessPoolExecutor() as executor: + for number, prime in zip(PRIMES, executor.map(is_prime, PRIMES)): + print('%d is prime: %s' % (number, prime)) + + +if __name__ == '__main__': + main() diff --git a/Day21-30/Web前端概述.md b/Day21-30/21.Web前端概述.md similarity index 100% rename from Day21-30/Web前端概述.md rename to Day21-30/21.Web前端概述.md diff --git a/Day31-35/玩转Linux操作系统.md b/Day31-35/31.玩转Linux操作系统.md similarity index 55% rename from Day31-35/玩转Linux操作系统.md rename to Day31-35/31.玩转Linux操作系统.md index 37e1d79..d3f457c 100644 --- a/Day31-35/玩转Linux操作系统.md +++ b/Day31-35/31.玩转Linux操作系统.md @@ -1,14 +1,14 @@ ## 玩转Linux操作系统 +> 说明:本文中对Linux命令的讲解都是基于名为CentOS的Linux发行版本,我自己使用的是阿里云服务器,系统版本为CentOS Linux release 7.6.1810。不同的Linux发行版本在Shell命令和工具程序上会有一些差别,但是这些差别是很小的。 + ### 操作系统发展史 -只有硬件没有软件的计算机系统被称之为“裸机”,我们很难用“裸机”来完成计算机日常的工作(如存储和运算),所以必须用特定的软件来控制硬件的工作。最靠近计算机硬件的软件是系统软件,其中最为重要的就是“操作系统”。“操作系统”是控制和管理整个计算机系统的硬件和软件资源,合理的分配资源和调配任务,为系统用户和其他软件提供接口和环境的程序的集合。 +只有硬件没有软件的计算机系统被称之为“裸机”,我们很难用“裸机”来完成计算机日常的工作(如存储和运算),所以必须用特定的软件来控制硬件的工作。最靠近计算机硬件的软件是系统软件,其中最为重要的就是“操作系统”。“操作系统”是控制和管理整个计算机硬件和软件资源、实现资源分配和任务调配、为系统用户以及其他软件提供接口和环境的程序的集合。 #### 没有操作系统(手工操作) -在计算机诞生之初没有操作系统的年代,人们先把程序纸带(或卡片)装上计算机,然后启动输入机把程序和送入计算机,接着通过控制台开关启动程序运行。当程序执行完毕,打印机输出计算的结果,用户卸下并取走纸带(或卡片)。第二个用户上机,重复同样的步骤。在整个过程中用户独占机器,CPU等待手工操作,资源利用率极低。下图是IBM生产的书写Fortran程序的80栏打孔卡,当然这个已经是比较先进的打孔卡了。 - -![](./res/ibm-col80-punched-card.png) +在计算机诞生之初没有操作系统的年代,人们先把程序纸带(或卡片)装上计算机,然后启动输入机把程序送入计算机,接着通过控制台开关启动程序运行。当程序执行完毕,打印机输出计算的结果,用户卸下并取走纸带(或卡片)。第二个用户上机,重复同样的步骤。在整个过程中用户独占机器,CPU等待手工操作,资源利用率极低。 #### 批处理系统 @@ -24,31 +24,35 @@ 2. 1965年:AT&T的贝尔实验室加入GE和MIT的合作计划开始开发MULTICS。 -3. 1969年:Ken Tompson为了玩“Space Travel”游戏用汇编语言在PDP-7上开发了Unics。 +3. 1969年:MULTICS项目失败,Ken Tompson赋闲在家,为了玩“Space Travel”游戏用汇编语言在当时已经被淘汰的PDP-7上开发了Unics。 - ![](./res/Ken-Thompson.png) + ![](./res/ken_young.jpg) ![](./res/pdp-7.png) + > 注:很难想象,Unix这么伟大的系统,居然是一个赋闲在家的程序员(关键是老婆回娘家还带上了孩子)在一台被淘汰的设备上为了玩游戏开发出来的。 + 4. 1970年~1971年:Ken Tompson和Dennis Ritchie用B语言在PDP-11上重写了Unics,并在Brian Kernighan的建议下将其更名为Unix。 + ![](./res/dmr.png) + ![](./res/ken-and-dennis-pdp-11.png) -5. 1972年~1973年:Dennis Ritchie发明了C语言来取代可移植性较差的B语言,并开启了用C语言重写Unix的工作。 + ![](./res/pdp-11.jpg) - ![](./res/dennis-ritchie.png) +5. 1972年~1973年:Dennis Ritchie发明了C语言来取代可移植性较差的B语言,并开启了用C语言重写Unix的工作。 6. 1974年:Unix推出了里程碑意义的第5版,几乎完全用C语言来实现。 7. 1979年:从Unix第7版开始,AT&T发布新的使用条款,将Unix私有化。 -8. 1987年:Andrew S. Tanenbaum教授为了能在课堂上教授学生操作系统运作的细节,决定在不使用任何AT&T的源代码前提下,自行开发与Unix兼容的操作系统,以避免版权上的争议并将其命名为Minix。 +8. 1987年:Andrew S. Tanenbaum教授为了能在课堂上为学生讲解操作系统运作的细节,决定在不使用任何AT&T的源代码前提下,自行开发与Unix兼容的操作系统以避免版权上的争议,该系统被命名为Minix。 - ![](./res/andrew-tanenbaum.png) + ![](./res/andrew.jpg) -9. 1991年:Linus Torvalds就读于芬兰赫尔辛基大学期间,尝试在Minix上做一些开发工作,但因为Minix只是作为教学用途的操作系统,功能并不强大,为了方便在学校的主机的新闻组和邮件系统中读写和下载文件,Linus编写了磁盘驱动程序和文件系统,这些成为了Linux系统内核的雏形。 +9. 1991年:Linus Torvalds就读于芬兰赫尔辛基大学期间,尝试在Minix上做一些开发工作,但因为Minix只是作为教学用途的操作系统,功能并不强大,为了方便在学校的新闻组和邮件系统中读写和下载文件,Linus编写了磁盘驱动程序和文件系统,这些东西形成了Linux系统内核的雏形。 - ![](./res/linus-torvalds.png) + ![](./res/linus.png) 下图是Unix操作系统家族的图谱。 @@ -63,7 +67,7 @@ Linux内核是芬兰人Linus Torvalds开发的,于1991年9月发布。而Linux ### Linux系统优点 1. 通用操作系统,不跟特定的硬件绑定。 -2. 用C语言编写,有可移植性,有内核编程接口。 +2. 用C语言编写,可移植性强,有内核编程接口。 3. 支持多用户和多任务,支持安全的分层文件系统。 4. 大量的实用程序,完善的网络功能以及强大的支持文档。 5. 可靠的安全性和良好的稳定性,对开发者更友好。 @@ -85,7 +89,7 @@ Linux系统的命令通常都是如下所示的格式: 命令名称 [命名参数] [命令对象] ``` -1. 获取登录信息 - **w** / **who** / **last**。 +1. 获取登录信息 - **w** / **who** / **last**/ **lastb**。 ```Shell [root@izwz97tbgo9lkabnat2lo8z ~]# w @@ -98,11 +102,21 @@ Linux系统的命令通常都是如下所示的格式: jackfrued pts/1 2018-04-12 23:26 (182.139.66.250) [root@izwz97tbgo9lkabnat2lo8z ~]# who am i root pts/0 2018-04-12 23:03 (182.139.66.250) + [root@izwz97tbgo9lkabnat2lo8z ~]# who mom likes + root pts/0 2018-04-12 23:03 (182.139.66.250) + [root@izwz97tbgo9lkabnat2lo8z ~]# last + root pts/0 117.136.63.184 Sun May 26 18:57 still logged in + reboot system boot 3.10.0-957.10.1. Mon May 27 02:52 - 19:10 (-7:-42) + root pts/4 117.136.63.184 Sun May 26 18:51 - crash (08:01) + root pts/4 117.136.63.184 Sun May 26 18:49 - 18:49 (00:00) + root pts/3 117.136.63.183 Sun May 26 18:35 - crash (08:17) + root pts/2 117.136.63.183 Sun May 26 18:34 - crash (08:17) + root pts/0 117.136.63.183 Sun May 26 18:10 - crash (08:42) ``` 2. 查看自己使用的Shell - **ps**。 - Shell也被称为“壳”,它是用户与内核交流的翻译官,简单的说就是人与计算机交互的接口。目前很多Linux系统默认的Shell都是bash(Bourne Again SHell),因为它可以使用Tab键进行命令补全、可以保存历史命令、可以方便的配置环境变量以及执行批处理操作等。 + Shell也被称为“壳”或“壳程序”,它是用户与操作系统内核交流的翻译官,简单的说就是人与计算机交互的界面和接口。目前很多Linux系统默认的Shell都是bash(Bourne Again SHell),因为它可以使用tab键进行命令和路径补全、可以保存历史命令、可以方便的配置环境变量以及执行批处理操作。 ```Shell [root@izwz97tbgo9lkabnat2lo8z ~]# ps @@ -111,18 +125,13 @@ Linux系统的命令通常都是如下所示的格式: 3553 pts/0 00:00:00 ps ``` -3. 查看命令的说明 - **whatis**。 +3. 查看命令的说明和位置 - **whatis** / **which** / **whereis**。 ```Shell [root@izwz97tbgo9lkabnat2lo8z ~]# whatis ps ps (1) - report a snapshot of the current processes. [root@izwz97tbgo9lkabnat2lo8z ~]# whatis python python (1) - an interpreted, interactive, object-oriented programming language - ``` - -4. 查看命令的位置 - **which** / **whereis**。 - - ```Shell [root@izwz97tbgo9lkabnat2lo8z ~]# whereis ps ps: /usr/bin/ps /usr/share/man/man1/ps.1.gz [root@izwz97tbgo9lkabnat2lo8z ~]# whereis python @@ -133,7 +142,9 @@ Linux系统的命令通常都是如下所示的格式: /usr/bin/python ``` -5. 查看帮助文档 - **man** / **info** / **apropos**。 +4. 清除屏幕上显示的内容 - **clear**。 + +5. 查看帮助文档 - **man** / **info** / **help** / **apropos**。 ```Shell [root@izwz97tbgo9lkabnat2lo8z ~]# ps --help Usage: @@ -150,47 +161,9 @@ Linux系统的命令通常都是如下所示的格式: ps [options] DESCRIPTION ... - [root@izwz97tbgo9lkabnat2lo8z ~]# info ps - ... ``` -6. 切换用户 - **su**。 - - ```Shell - [root@izwz97tbgo9lkabnat2lo8z ~]# su hellokitty - [hellokitty@izwz97tbgo9lkabnat2lo8z root]$ - ``` - -7. 以管理员身份执行命令 - **sudo**。 - - ```Shell - [jackfrued@izwz97tbgo9lkabnat2lo8z ~]$ ls /root - ls: cannot open directory /root: Permission denied - [jackfrued@izwz97tbgo9lkabnat2lo8z ~]$ sudo ls /root - [sudo] password for jackfrued: - calendar.py code error.txt hehe hello.c index.html myconf result.txt - ``` - - > **说明**:如果希望用户能够以管理员身份执行命令,用户必须被添加到sudoers名单中,该文件在 `/etc`目录下。 - -8. 登入登出相关 - **logout** / **exit** / **adduser** / **userdel** / **passwd** / **ssh**。 - - ```Shell - [root@izwz97tbgo9lkabnat2lo8z ~]# adduser hellokitty - [root@izwz97tbgo9lkabnat2lo8z ~]# passwd hellokitty - Changing password for user jackfrued. - New password: - Retype new password: - passwd: all authentication tokens updated successfully. - [root@izwz97tbgo9lkabnat2lo8z ~]# ssh hellokitty@1.2.3.4 - hellokitty@1.2.3.4's password: - Last login: Thu Apr 12 23:05:32 2018 from 10.12.14.16 - [hellokitty@izwz97tbgo9lkabnat2lo8z ~]$ logout - Connection to 1.2.3.4 closed. - [root@izwz97tbgo9lkabnat2lo8z ~]# - ``` - -9. 查看系统和主机名 - **uname** / **hostname**。 +6. 查看系统和主机名 - **uname** / **hostname**。 ```Shell [root@izwz97tbgo9lkabnat2lo8z ~]# uname @@ -198,30 +171,80 @@ Linux系统的命令通常都是如下所示的格式: [root@izwz97tbgo9lkabnat2lo8z ~]# hostname izwz97tbgo9lkabnat2lo8z [root@iZwz97tbgo9lkabnat2lo8Z ~]# cat /etc/centos-release - CentOS Linux release 7.4.1708 (Core) + CentOS Linux release 7.6.1810 (Core) ``` -10. 重启和关机 - **reboot** / **init 6** / **shutdown** / **init 0**。 + > 说明:`cat`是连接文件内容并打印到标准输出的命令,后面会讲到该命令;`/etc`是Linux系统上的一个非常重要的目录,它保存了很多的配置文件;`centos-release`是该目录下的一个文件,因为我自己使用的Linux发行版本是CentOS 7.6,因此这里会有一个这样的文件。 -11. 查看历史命令 - **history**。 +7. 时间和日期 - **date** / **cal**。 - ```Shell - [root@iZwz97tbgo9lkabnat2lo8Z ~]# history - ... - 452 ls - 453 cd Python-3.6.5/ - 454 clear - 455 history - [root@iZwz97tbgo9lkabnat2lo8Z ~]# !454 - ``` + ```Shell + [root@iZwz97tbgo9lkabnat2lo8Z ~]# date + Wed Jun 20 12:53:19 CST 2018 + [root@iZwz97tbgo9lkabnat2lo8Z ~]# cal + June 2018 + Su Mo Tu We Th Fr Sa + 1 2 + 3 4 5 6 7 8 9 + 10 11 12 13 14 15 16 + 17 18 19 20 21 22 23 + 24 25 26 27 28 29 30 + [root@iZwz97tbgo9lkabnat2lo8Z ~]# cal 5 2017 + May 2017 + Su Mo Tu We Th Fr Sa + 1 2 3 4 5 6 + 7 8 9 10 11 12 13 + 14 15 16 17 18 19 20 + 21 22 23 24 25 26 27 + 28 29 30 31 + ``` - > 说明:查看到历史命令之后,可以用`!历史命令编号`来重新执行该命令;通过`history -c`可以清除历史命令。 +8. 重启和关机 - **reboot** / **shutdown**。 + + ```Shell + [root@iZwz97tbgo9lkabnat2lo8Z ~]# shutdown -h +5 + Shutdown scheduled for Sun 2019-05-26 19:34:27 CST, use 'shutdown -c' to cancel. + [root@izwz97tbgo9lkabnat2lo8z ~]# + Broadcast message from root@izwz97tbgo9lkabnat2lo8z (Sun 2019-05-26 19:29:27 CST): + + The system is going down for power-off at Sun 2019-05-26 19:34:27 CST! + [root@izwz97tbgo9lkabnat2lo8z ~]# shutdown -c + + Broadcast message from root@izwz97tbgo9lkabnat2lo8z (Sun 2019-05-26 19:30:22 CST): + + The system shutdown has been cancelled at Sun 2019-05-26 19:31:22 CST! + [root@izwz97tbgo9lkabnat2lo8z ~]# shutdown -r 23:58 + Shutdown scheduled for Sun 2019-05-26 23:58:00 CST, use 'shutdown -c' to cancel. + [root@izwz97tbgo9lkabnat2lo8z ~]# shutdown -c + + Broadcast message from root@izwz97tbgo9lkabnat2lo8z (Sun 2019-05-26 19:31:06 CST): + + The system shutdown has been cancelled at Sun 2019-05-26 19:32:06 CST! + ``` + + > 说明:在执行`shutdown`命令时会向登录系统的用户发出警告,可以在命令后面跟上警告消息来替换默认的警告消息,也可以在`-h`参数后通过`now`来表示立刻关机。 + +9. 退出登录 - **exit** / **logout**。 + +10. 查看历史命令 - **history**。 + + ```Shell + [root@iZwz97tbgo9lkabnat2lo8Z ~]# history + ... + 452 ls + 453 cd Python-3.6.5/ + 454 clear + 455 history + [root@iZwz97tbgo9lkabnat2lo8Z ~]# !454 + ``` + + > 说明:查看到历史命令之后,可以用`!历史命令编号`来重新执行该命令;通过`history -c`可以清除历史命令。 ### 实用程序 #### 文件和文件夹操作 -1. 创建/删除目录 - **mkdir** / **rmdir**。 +1. 创建/删除空目录 - **mkdir** / **rmdir**。 ```Shell [root@iZwz97tbgo9lkabnat2lo8Z ~]# mkdir abc @@ -239,28 +262,28 @@ Linux系统的命令通常都是如下所示的格式: [root@iZwz97tbgo9lkabnat2lo8Z ~]# rm -rf xyz ``` - - touch命令用于创建空白文件或修改文件时间。在Linux系统中一个文件有三种时间: + - `touch`命令用于创建空白文件或修改文件时间。在Linux系统中一个文件有三种时间: - 更改内容的时间 - mtime。 - 更改权限的时间 - ctime。 - 最后访问时间 - atime。 - - rm的几个重要参数: - - -i:交互式删除,每个删除项都会进行询问。 - - -r:删除目录并递归的删除目录中的文件和目录。 - - -f:强制删除,忽略不存在的文件,没有任何提示。 + - `rm`的几个重要参数: + - `-i`:交互式删除,每个删除项都会进行询问。 + - `-r`:删除目录并递归的删除目录中的文件和目录。 + - `-f`:强制删除,忽略不存在的文件,没有任何提示。 3. 切换和查看当前工作目录 - **cd** / **pwd**。 - > 说明:`cd`命令后面可以跟相对路径(以当前路径作为参照)或绝对路径(以`/`开头)来切换到指定的目录,也可以用`cd ..`来返回上一级目录。 + > 说明:`cd`命令后面可以跟相对路径(以当前路径作为参照)或绝对路径(以`/`开头)来切换到指定的目录,也可以用`cd ..`来返回上一级目录。请大家想一想,如果要返回到上上一级目录应该给`cd`命令加上什么样的参数呢? 4. 查看目录内容 - **ls**。 - - -l:以长格式查看文件和目录。 - - -a:显示以点开头的文件和目录(隐藏文件)。 - - -R:遇到目录要进行递归展开(继续列出目录下面的文件和目录)。 - - -d:只列出目录,不列出其他内容。 - - -S/-t:按大小/时间排序。 + - `-l`:以长格式查看文件和目录。 + - `-a`:显示以点开头的文件和目录(隐藏文件)。 + - `-R`:遇到目录要进行递归展开(继续列出目录下面的文件和目录)。 + - `-d`:只列出目录,不列出其他内容。 + - `-S` / `-t`:按大小/时间排序。 -5. 查看文件内容 - **cat** / **head** / **tail** / **more** / **less**。 +5. 查看文件内容 - **cat** / **tac** / **head** / **tail** / **more** / **less** / **rev** / **od**。 ```Shell [root@iZwz97tbgo9lkabnat2lo8Z ~]# wget http://www.sohu.com/ -O sohu.html @@ -293,6 +316,8 @@ Linux系统的命令通常都是如下所示的格式: ... ``` + > 说明:上面用到了一个名为`wget`的命令,它是一个网络下载器程序,可以从指定的URL下载资源。 + 6. 拷贝/移动文件 - **cp** / **mv**。 ```Shell @@ -306,7 +331,13 @@ Linux系统的命令通常都是如下所示的格式: sohu_index.html ``` -7. 查找文件和查找内容 - **find** / **grep**。 +7. 文件重命名 - **rename**。 + + ```Shell + [root@iZwz97tbgo9lkabnat2lo8Z ~]# rename .htm .html *.htm + ``` + +8. 查找文件和查找内容 - **find** / **grep**。 ```Shell [root@iZwz97tbgo9lkabnat2lo8Z ~]# find / -name "*.html" @@ -329,7 +360,7 @@ Linux系统的命令通常都是如下所示的格式: ``` > 说明:`grep`在搜索字符串时可以使用正则表达式,如果需要使用正则表达式可以用`grep -E`或者直接使用`egrep`。 -8. 链接 - **ln**。 +9. 创建链接和查看链接 - **ln** / **readlink**。 ```Shell [root@iZwz97tbgo9lkabnat2lo8Z ~]# ls -l sohu.html @@ -351,23 +382,28 @@ Linux系统的命令通常都是如下所示的格式: > 说明:链接可以分为硬链接和软链接(符号链接)。硬链接可以认为是一个指向文件数据的指针,就像Python中对象的引用计数,每添加一个硬链接,文件的对应链接数就增加1,只有当文件的链接数为0时,文件所对应的存储空间才有可能被其他文件覆盖。我们平常删除文件时其实并没有删除硬盘上的数据,我们删除的只是一个指针,或者说是数据的一条使用记录,所以类似于“文件粉碎机”之类的软件在“粉碎”文件时除了删除文件指针,还会在文件对应的存储区域填入数据来保证文件无法再恢复。软链接类似于Windows系统下的快捷方式,当软链接链接的文件被删除时,软链接也就失效了。 -9. 压缩/解压缩和归档/解归档 - **gzip** / **gunzip** / **xz** / **tar**。 +10. 压缩/解压缩和归档/解归档 - **gzip** / **gunzip** / **xz**。 + + ```Shell + [root@iZwz97tbgo9lkabnat2lo8Z ~]# wget http://download.redis.io/releases/redis-4.0.10.tar.gz + --2018-06-20 19:29:59-- http://download.redis.io/releases/redis-4.0.10.tar.gz + Resolving download.redis.io (download.redis.io)... 109.74.203.151 + Connecting to download.redis.io (download.redis.io)|109.74.203.151|:80... connected. + HTTP request sent, awaiting response... 200 OK + Length: 1738465 (1.7M) [application/x-gzip] + Saving to: ‘redis-4.0.10.tar.gz’ + 100%[==================================================>] 1,738,465 70.1KB/s in 74s + 2018-06-20 19:31:14 (22.9 KB/s) - ‘redis-4.0.10.tar.gz’ saved [1738465/1738465] + [root@iZwz97tbgo9lkabnat2lo8Z ~]# ls redis* + redis-4.0.10.tar.gz + [root@iZwz97tbgo9lkabnat2lo8Z ~]# gunzip redis-4.0.10.tar.gz + [root@iZwz97tbgo9lkabnat2lo8Z ~]# ls redis* + redis-4.0.10.tar + ``` + +11. 归档和解归档 - **tar**。 ```Shell - [root@iZwz97tbgo9lkabnat2lo8Z ~]# wget http://download.redis.io/releases/redis-4.0.10.tar.gz - --2018-06-20 19:29:59-- http://download.redis.io/releases/redis-4.0.10.tar.gz - Resolving download.redis.io (download.redis.io)... 109.74.203.151 - Connecting to download.redis.io (download.redis.io)|109.74.203.151|:80... connected. - HTTP request sent, awaiting response... 200 OK - Length: 1738465 (1.7M) [application/x-gzip] - Saving to: ‘redis-4.0.10.tar.gz’ - 100%[==================================================>] 1,738,465 70.1KB/s in 74s - 2018-06-20 19:31:14 (22.9 KB/s) - ‘redis-4.0.10.tar.gz’ saved [1738465/1738465] - [root@iZwz97tbgo9lkabnat2lo8Z ~]# ls redis* - redis-4.0.10.tar.gz - [root@iZwz97tbgo9lkabnat2lo8Z ~]# gunzip redis-4.0.10.tar.gz - [root@iZwz97tbgo9lkabnat2lo8Z ~]# ls redis* - redis-4.0.10.tar [root@iZwz97tbgo9lkabnat2lo8Z ~]# tar -xvf redis-4.0.10.tar redis-4.0.10/ redis-4.0.10/.gitignore @@ -383,57 +419,76 @@ Linux系统的命令通常都是如下所示的格式: redis-4.0.10/deps/Makefile redis-4.0.10/deps/README.md ... - [root@iZwz97tbgo9lkabnat2lo8Z ~]# ls redis* - redis-4.0.10.tar - redis-4.0.10: - 00-RELEASENOTES COPYING Makefile redis.conf runtest-sentinel tests - BUGS deps MANIFESTO runtest sentinel.conf utils - CONTRIBUTING INSTALL README.md runtest-cluster src ``` -10. 其他工具 - **sort** / **uniq** / **diff** / **tr** / **cut** / **paste** / **file** / **wc**。 + > 说明:归档(也称为创建归档)和解归档都使用`tar`命令,通常创建归档需要`-cvf`三个参数,其中`c`表示创建(create),`v`表示显示创建归档详情(verbose),`f`表示指定归档的文件(file);解归档需要加上`-xvf`参数,其中`x`表示抽取(extract),其他两个参数跟创建归档相同。 - ```Shell - [root@iZwz97tbgo9lkabnat2lo8Z ~]# cat foo.txt - grape - apple - pitaya - [root@iZwz97tbgo9lkabnat2lo8Z ~]# cat bar.txt - 100 - 200 - 300 - 400 - [root@iZwz97tbgo9lkabnat2lo8Z ~]# paste foo.txt bar.txt - grape 100 - apple 200 - pitaya 300 - 400 - [root@iZwz97tbgo9lkabnat2lo8Z ~]# paste foo.txt bar.txt > hello.txt - [root@iZwz97tbgo9lkabnat2lo8Z ~]# cut -b 4-8 hello.txt - pe 10 - le 20 - aya 3 - 0 - [root@iZwz97tbgo9lkabnat2lo8Z ~]# cat hello.txt | tr '\t' ',' - grape,100 - apple,200 - pitaya,300 - ,400 - [root@iZwz97tbgo9lkabnat2lo8Z ~]# wget https://www.baidu.com/img/bd_logo1.png - --2018-06-20 18:46:53-- https://www.baidu.com/img/bd_logo1.png - Resolving www.baidu.com (www.baidu.com)... 220.181.111.188, 220.181.112.244 - Connecting to www.baidu.com (www.baidu.com)|220.181.111.188|:443... connected. - HTTP request sent, awaiting response... 200 OK - Length: 7877 (7.7K) [image/png] - Saving to: ‘bd_logo1.png’ - 100%[==================================================>] 7,877 --.-K/s in 0s - 2018-06-20 18:46:53 (118 MB/s) - ‘bd_logo1.png’ saved [7877/7877][root@iZwz97tbgo9lkabnat2lo8Z ~]# file bd_logo1.png - bd_logo1.png: PNG image data, 540 x 258, 8-bit colormap, non-interlaced - [root@iZwz97tbgo9lkabnat2lo8Z ~]# wc sohu.html - 2979 6355 212527 sohu.html - [root@iZwz97tbgo9lkabnat2lo8Z ~]# wc -l sohu.html - 2979 sohu.html - ``` +12. 将标准输入转成命令行参数 - **xargs**。 + + 下面的命令会将查找当前路径下的html文件,然后通过`xargs`将这些文件作为参数传给`rm`命令,实现查找并删除文件的操作。 + + ```Shell + [root@iZwz97tbgo9lkabnat2lo8Z ~]# find . -type f -name "*.html" | xargs rm -f + ``` + + 下面的命令将a.txt文件中的多行内容变成一行输出到b.txt文件中,其中`<`表示从a.txt中读取输入,`>`表示将命令的执行结果输出到b.txt中。 + + ```Shell + [root@iZwz97tbgo9lkabnat2lo8Z ~]# xargs < a.txt > b.txt + ``` + + > 说明:这个命令就像上面演示的那样常在管道(实现进程间通信的一种方式)和重定向(重新指定输入输出的位置)操作中用到,后面的内容中会讲到管道操作和输入输出重定向操作。 + +13. 显示文件或目录 - **basename** / **dirname**。 + +14. 其他相关工具。 + + - **sort** - 对内容排序 + - **uniq** - 去掉相邻重复内容 + - **tr** - 替换指定内容为新内容 + - **cut** / **paste** - 剪切/黏贴内容 + - **split** - 拆分文件 + - **file** - 判断文件类型 + - **wc** - 统计文件行数、单词数、字节数 + - **iconv** - 编码转换 + + ```Shell + [root@iZwz97tbgo9lkabnat2lo8Z ~]# cat foo.txt + grape + apple + pitaya + [root@iZwz97tbgo9lkabnat2lo8Z ~]# cat bar.txt + 100 + 200 + 300 + 400 + [root@iZwz97tbgo9lkabnat2lo8Z ~]# paste foo.txt bar.txt + grape 100 + apple 200 + pitaya 300 + 400 + [root@iZwz97tbgo9lkabnat2lo8Z ~]# paste foo.txt bar.txt > hello.txt + [root@iZwz97tbgo9lkabnat2lo8Z ~]# cut -b 4-8 hello.txt + pe 10 + le 20 + aya 3 + 0 + [root@iZwz97tbgo9lkabnat2lo8Z ~]# cat hello.txt | tr '\t' ',' + grape,100 + apple,200 + pitaya,300 + ,400 + [root@izwz97tbgo9lkabnat2lo8z ~]# split -l 100 sohu.html hello + [root@iZwz97tbgo9lkabnat2lo8Z ~]# wget https://www.baidu.com/img/bd_logo1.png + [root@iZwz97tbgo9lkabnat2lo8Z ~]# file bd_logo1.png + bd_logo1.png: PNG image data, 540 x 258, 8-bit colormap, non-interlaced + [root@iZwz97tbgo9lkabnat2lo8Z ~]# wc sohu.html + 2979 6355 212527 sohu.html + [root@iZwz97tbgo9lkabnat2lo8Z ~]# wc -l sohu.html + 2979 sohu.html + [root@iZwz97tbgo9lkabnat2lo8Z ~]# wget http://www.qq.com -O qq.html + [root@iZwz97tbgo9lkabnat2lo8Z ~]# iconv -f gb2312 -t utf-8 qq.html + ``` #### 管道和重定向 @@ -501,6 +556,14 @@ Linux系统的命令通常都是如下所示的格式: I will show you some code. ``` +4. 多重定向 - **tee**。 + + 下面的命令除了在终端显示命令`ls`的结果之外,还会追加输出到`ls.txt`文件中。 + + ```Shell + [root@iZwz97tbgo9lkabnat2lo8Z ~]# ls | tee -a ls.txt + ``` + #### 别名 1. **alias** @@ -523,35 +586,256 @@ Linux系统的命令通常都是如下所示的格式: -bash: frm: command not found ``` -#### 其他程序 +#### 文本处理 -1. 时间和日期 - **date** / **cal**。 +1. 字符流编辑器 - **sed**。 + + sed是操作、过滤和转换文本内容的工具。假设有一个名为fruit.txt的文件,内容如下所示。 ```Shell - [root@iZwz97tbgo9lkabnat2lo8Z ~]# date - Wed Jun 20 12:53:19 CST 2018 - [root@iZwz97tbgo9lkabnat2lo8Z ~]# cal - June 2018 - Su Mo Tu We Th Fr Sa - 1 2 - 3 4 5 6 7 8 9 - 10 11 12 13 14 15 16 - 17 18 19 20 21 22 23 - 24 25 26 27 28 29 30 - [root@iZwz97tbgo9lkabnat2lo8Z ~]# cal 5 2017 - May 2017 - Su Mo Tu We Th Fr Sa - 1 2 3 4 5 6 - 7 8 9 10 11 12 13 - 14 15 16 17 18 19 20 - 21 22 23 24 25 26 27 - 28 29 30 31 + [root@izwz97tbgo9lkabnat2lo8z ~]# cat -n fruit.txt + 1 banana + 2 grape + 3 apple + 4 watermelon + 5 orange ``` -2. 录制操作脚本 - **script**。 + 接下来,我们在第2行后面添加一个pitaya。 -3. 给用户发送消息 - **mesg** / **write** / **wall** / **mail**。 + ```Shell + [root@izwz97tbgo9lkabnat2lo8z ~]# sed '2a pitaya' fruit.txt + banana + grape + pitaya + apple + watermelon + orange + ``` + > 注意:刚才的命令和之前我们讲过的很多命令一样并没有改变fruit.txt文件,而是将添加了新行的内容输出到终端中,如果想保存到fruit.txt中,可以使用输出重定向操作。 + + 在第2行前面插入一个waxberry。 + + ```Shell + [root@izwz97tbgo9lkabnat2lo8z ~]# sed '2i waxberry' fruit.txt + banana + waxberry + grape + apple + watermelon + orange + ``` + + 删除第3行。 + + ```Shell + [root@izwz97tbgo9lkabnat2lo8z ~]# sed '3d' fruit.txt + banana + grape + watermelon + orange + ``` + + 删除第2行到第4行。 + + ```Shell + [root@izwz97tbgo9lkabnat2lo8z ~]# sed '2,4d' fruit.txt + banana + orange + ``` + + 将文本中的字符a替换为@。 + + ```Shell + [root@izwz97tbgo9lkabnat2lo8z ~]# sed 's#a#@#' fruit.txt + b@nana + gr@pe + @pple + w@termelon + or@nge + ``` + + 将文本中的字符a替换为@,使用全局模式。 + + ```Shell + [root@izwz97tbgo9lkabnat2lo8z ~]# sed 's#a#@#g' fruit.txt + b@n@n@ + gr@pe + @pple + w@termelon + or@nge + ``` + +2. 模式匹配和处理语言 - **awk**。 + + awk是一种编程语言,也是Linux系统中处理文本最为强大的工具,它的作者之一和现在的维护者就是之前提到过的Brian Kernighan(ken和dmr最亲密的伙伴)。通过该命令可以从文本中提取出指定的列、用正则表达式从文本中取出我们想要的内容、显示指定的行以及进行统计和运算,总之它非常强大。 + + 假设有一个名为fruit2.txt的文件,内容如下所示。 + + ```Shell + [root@izwz97tbgo9lkabnat2lo8z ~]# cat fruit2.txt + 1 banana 120 + 2 grape 500 + 3 apple 1230 + 4 watermelon 80 + 5 orange 400 + ``` + + 显示文件的第3行。 + + ```Shell + [root@izwz97tbgo9lkabnat2lo8z ~]# awk 'NR==3' fruit2.txt + 3 apple 1230 + ``` + + 显示文件的第2列。 + + ```Shell + [root@izwz97tbgo9lkabnat2lo8z ~]# awk '{print $2}' fruit2.txt + banana + grape + apple + watermelon + orange + ``` + + 显示文件的最后一列。 + + ```Shell + [root@izwz97tbgo9lkabnat2lo8z ~]# awk '{print $NF}' fruit2.txt + 120 + 500 + 1230 + 80 + 400 + ``` + + 输出末尾数字大于等于300的行。 + + ```Shell + [root@izwz97tbgo9lkabnat2lo8z ~]# awk '{if($3 >= 300) {print $0}}' fruit2.txt + 2 grape 500 + 3 apple 1230 + 5 orange 400 + ``` + + 上面展示的只是awk命令的冰山一角,更多的内容留给读者自己在实践中去探索。 + +### 用户管理 + +1. 创建和删除用户 - **useradd** / **userdel**。 + + ```Shell + [root@izwz97tbgo9lkabnat2lo8z home]# useradd hellokitty + [root@izwz97tbgo9lkabnat2lo8z home]# userdel hellokitty + ``` + + - `-d` - 创建用户时为用户指定用户主目录 + - `-g` - 创建用户时指定用户所属的用户组 + +2. 创建和删除用户组 - **groupadd** / **groupdel**。 + + > 说明:用户组主要是为了方便对一个组里面所有用户的管理。 + +3. 修改密码 - **passwd**。 + + ```Shell + [root@izwz97tbgo9lkabnat2lo8z ~]# passwd hellokitty + New password: + Retype new password: + passwd: all authentication tokens updated successfully. + ``` + + > 说明:输入密码和确认密码没有回显且必须一气呵成的输入完成(不能使用退格键),密码和确认密码需要一致。如果使用`passwd`命令时没有指定命令作用的对象,则表示要修改当前用户的密码。如果想批量修改用户密码,可以使用`chpasswd`命令。 + + - `-l` / `-u` - 锁定/解锁用户。 + - `-d` - 清除用户密码。 + - `-e` - 设置密码立即过期,用户登录时会强制要求修改密码。 + - `-i` - 设置密码过期多少天以后禁用该用户。 + +4. 查看和修改密码有效期 - **chage**。 + + 设置hellokitty用户100天后必须修改密码,过期前15天通知该用户,过期后15天禁用该用户。 + + ```Shell + chage -M 100 -W 15 -I 15 hellokitty + ``` + +5. 切换用户 - **su**。 + + ```Shell + [root@izwz97tbgo9lkabnat2lo8z ~]# su hellokitty + [hellokitty@izwz97tbgo9lkabnat2lo8z root]$ + ``` + +6. 以管理员身份执行命令 - **sudo**。 + + ```Shell + [hellokitty@izwz97tbgo9lkabnat2lo8z ~]$ ls /root + ls: cannot open directory /root: Permission denied + [hellokitty@izwz97tbgo9lkabnat2lo8z ~]$ sudo ls /root + [sudo] password for hellokitty: + ``` + + > **说明**:如果希望用户能够以管理员身份执行命令,用户必须要出现在sudoers名单中,sudoers文件在 `/etc`目录下,如果希望直接编辑该文件也可以使用下面的命令。 + +7. 编辑sudoers文件 - **visudo**。 + + 这里使用的编辑器是vi,关于vi的知识在后面有讲解。该文件的部分内容如下所示: + + ``` + ## Allow root to run any commands anywhere + root ALL=(ALL) ALL + + ## Allows members of the 'sys' group to run networking, software, + ## service management apps and more. + # %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS + ## Allows people in group wheel to run all commands + %wheel ALL=(ALL) ALL + + ## Same thing without a password + # %wheel ALL=(ALL) NOPASSWD: ALL + + ## Allows members of the users group to mount and unmount the + ## cdrom as root + # %users ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom + + ## Allows members of the users group to shutdown this system + # %users localhost=/sbin/shutdown -h now + ``` + +8. 显示用户与用户组的信息 - **id**。 + +9. 给其他用户发消息 -**write** / **wall**。 + + 发送方: + + ```Shell + [root@izwz97tbgo9lkabnat2lo8z ~]# write hellokitty + Dinner is on me. + Call me at 6pm. + ``` + + 接收方: + + ```Shell + [hellokitty@izwz97tbgo9lkabnat2lo8z ~]$ + Message from root@izwz97tbgo9lkabnat2lo8z on pts/0 at 17:41 ... + Dinner is on me. + Call me at 6pm. + EOF + ``` + +10. 查看/设置是否接收其他用户发送的消息 - **mesg**。 + + ```Shell + [hellokitty@izwz97tbgo9lkabnat2lo8z ~]$ mesg + is y + [hellokitty@izwz97tbgo9lkabnat2lo8z ~]$ mesg n + [hellokitty@izwz97tbgo9lkabnat2lo8z ~]$ mesg + is n + ``` ### 文件系统 @@ -623,6 +907,8 @@ Linux系统的命令通常都是如下所示的格式: ... ``` +3. **chgrp** - 改变用户组。 + #### 磁盘管理 1. 列出文件系统的磁盘使用状况 - **df**。 @@ -656,11 +942,27 @@ Linux系统的命令通常都是如下所示的格式: I/O size (minimum/optimal): 512 bytes / 512 bytes ``` -3. 格式化文件系统 - **mkfs**。 +3. 磁盘分区工具 - **parted**。 -4. 文件系统检查 - **fsck**。 +4. 格式化文件系统 - **mkfs**。 -5. 挂载/卸载 - **mount** / **umount**。 + ```Shell + [hellokitty@izwz97tbgo9lkabnat2lo8z ~]$ mkfs -t ext4 -v /dev/sdb + ``` + + - `-t` - 指定文件系统的类型。 + - `-c` - 创建文件系统时检查磁盘损坏情况。 + - `-v` - 显示详细信息。 + +5. 文件系统检查 - **fsck**。 + +6. 转换或拷贝文件 - **dd**。 + +7. 挂载/卸载 - **mount** / **umount**。 + +8. 创建/激活/关闭交换分区 - **mkswap** / **swapon** / **swapoff**。 + +> 说明:执行上面这些命令会带有一定的风险,如果不清楚这些命令的用法,最好不用随意使用,在使用的过程中,最好对照参考资料进行操作,并在操作前确认是否要这么做。 ### 编辑器 - vim @@ -708,7 +1010,7 @@ Linux系统的命令通常都是如下所示的格式: - 设置启用/关闭搜索结果高亮:`set hls` / `set nohls` - > 说明:如果希望上面的这些设定在每次启动vim时都能生效,需要将这些设定写到用户主目录下的.vimrc文件中。 + > 说明:如果希望上面的这些设定在每次启动vim时都能自动生效,需要将这些设定写到用户主目录下的.vimrc文件中。 8. 高级技巧 @@ -754,7 +1056,7 @@ Linux系统的命令通常都是如下所示的格式: - 通过`@a`(`a`是刚才使用的寄存器的名字)播放宏,如果要多次执行宏可以在前面加数字,例如`100@a`表示将宏播放100次。 - - 可以试一试下面的例子来体验录制宏的操作,该例子来源于[Harttle Land网站](https://harttle.land/tags.html#Vim),该网站上提供了很多关于vim的使用技巧,有兴趣的可以去了解一下。 + - 可以试一试下面的例子来体验录制宏的操作,该例子来源于[Harttle Land网站](https://harttle.land/tags.html#Vim),该网站上提供了很多关于vim的使用技巧,有兴趣的可以了解一下。 ![](./res/vim-macro.png) @@ -816,11 +1118,10 @@ nginx version: nginx/1.12.2 移除Nginx。 ```Shell -[root@iZwz97tbgo9lkabnat2lo8Z ~]# nginx -s stop [root@iZwz97tbgo9lkabnat2lo8Z ~]# yum -y remove nginx ``` -下面以MySQL为例,演示如何使用rpm安装软件。要安装MySQL需要先到[MySQL官方网站](https://www.mysql.com/)下载对应的[RPM文件](https://dev.mysql.com/downloads/mysql/),当然要选择和你使用的Linux系统对应的版本。MySQL现在是Oracle公司旗下的产品,在MySQL被收购后,MySQL的作者重新制作了一个MySQL的分支MariaDB,可以通过yum进行安装。如果要安装MySQL需要先通过yum删除`mariadb-libs`这个可能会跟MySQL底层库冲突的库,然后还需要安装一个名为`libaio`的依赖库。 +下面以MySQL为例,演示如何使用rpm安装软件。要安装MySQL需要先到[MySQL官方网站](https://www.mysql.com/)下载对应的[RPM文件](https://dev.mysql.com/downloads/mysql/),当然要选择和你使用的Linux系统对应的版本。MySQL现在是Oracle公司旗下的产品,在MySQL被收购后,MySQL的作者重新制作了一个MySQL的分支MariaDB,可以通过yum进行安装。 ```Shell [root@iZwz97tbgo9lkabnat2lo8Z mysql]# ls @@ -830,13 +1131,17 @@ mysql-community-libs-5.7.22-1.el7.x86_64.rpm mysql-community-server-5.7.22-1.el7.x86_64.rpm [root@iZwz97tbgo9lkabnat2lo8Z mysql]# yum -y remove mariadb-libs [root@iZwz97tbgo9lkabnat2lo8Z mysql]# yum -y install libaio -[root@iZwz97tbgo9lkabnat2lo8Z mysql]# ls | xargs rpm -ivh -warning: mysql-community-client-5.7.22-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY -Preparing... ################################# [100%] +[root@iZwz97tbgo9lkabnat2lo8Z mysql]#rpm -ivh mysql-community-common-5.7.26-1.el7.x86_64.rpm +... +[root@iZwz97tbgo9lkabnat2lo8Z mysql]#rpm -ivh mysql-community-libs-5.7.26-1.el7.x86_64.rpm +... +[root@iZwz97tbgo9lkabnat2lo8Z mysql]#rpm -ivh mysql-community-client-5.7.26-1.el7.x86_64.rpm +... +[root@iZwz97tbgo9lkabnat2lo8Z mysql]#rpm -ivh mysql-community-server-5.7.26-1.el7.x86_64.rpm ... ``` -> 说明:由于MySQL和[MariaDB](https://mariadb.org/)的底层依赖库是有冲突的,所以上面我们首先用`yum`移除了名为mariadb-libs的依赖库并安装了名为libaio的依赖库。由于我们将安装MySQL所需的rpm文件放在一个独立的目录中,所以可以通过`ls`命令查看到安装文件并用`xargs`将`ls`的输出作为参数交给`rpm -ivh`来进行安装。关于MySQL和MariaDB之间的关系,可以阅读[维基百科](https://zh.wikipedia.org/wiki/MariaDB)上关于MariaDB的介绍。 +> 说明:由于MySQL和[MariaDB](https://mariadb.org/)的底层依赖库是有冲突的,所以上面我们首先用`yum`移除了名为mariadb-libs的依赖库并安装了名为libaio支持异步I/O操作的依赖库。关于MySQL和MariaDB之间的关系,可以阅读[维基百科](https://zh.wikipedia.org/wiki/MariaDB)上关于MariaDB的介绍。 移除安装的MySQL。 @@ -921,7 +1226,7 @@ build environment: [root@iZwz97tbgo9lkabnat2lo8Z ~]# ./configure --prefix=/usr/local/python36 --enable-optimizations [root@iZwz97tbgo9lkabnat2lo8Z ~]# yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel [root@iZwz97tbgo9lkabnat2lo8Z ~]# make && make install - ... 配置环境变量 ... + ... [root@iZwz97tbgo9lkabnat2lo8Z ~]# ln -s /usr/local/python36/bin/python3.6 /usr/bin/python3 [root@iZwz97tbgo9lkabnat2lo8Z ~]# python3 --version Python 3.6.5 @@ -929,6 +1234,8 @@ build environment: [root@iZwz97tbgo9lkabnat2lo8Z ~]# pip3 --version ``` + > 说明:上面在安装好Python之后还需要注册PATH环境变量,将Python安装路径下bin文件夹的绝对路径注册到PATH环境变量中。注册环境变量可以修改用户主目录下的.bash_profile或者/etc目录下的profile文件,二者的区别在于前者相当于是用户环境变量,而后者相当于是系统环境变量。 + 2. 安装Redis-3.2.12。 ```Shell @@ -945,31 +1252,33 @@ build environment: ### 配置服务 -1. 启动服务。 +我们可以Linux系统下安装和配置各种服务,也就是说我们可以把Linux系统打造成数据库服务器、Web服务器、缓存服务器、文件服务器、消息队列服务器等等。Linux下的大多数服务都被设置为守护进程(驻留在系统后台运行,但不会因为服务还在运行而导致Linux无法停止运行),所以我们安装的服务通常名字后面都有一个字母`d`,它是英文单词`daemon`的缩写,例如:防火墙服务叫firewalld,我们之前安装的MySQL服务叫mysqld,Apache服务器叫httpd等。在安装好服务之后,可以使用`systemctl`命令或`service`命令来完成对服务的启动、停止等操作,具体操作如下所示。 + +1. 启动防火墙服务。 ```Shell [root@iZwz97tbgo9lkabnat2lo8Z ~]# systemctl start firewalld ``` -2. 终止服务。 +2. 终止防火墙服务。 ```Shell [root@iZwz97tbgo9lkabnat2lo8Z ~]# systemctl stop firewalld ``` -3. 重启服务。 +3. 重启防火墙服务。 ```Shell [root@iZwz97tbgo9lkabnat2lo8Z ~]# systemctl restart firewalld ``` -4. 查看服务。 +4. 查看防火墙服务状态。 ```Shell [root@iZwz97tbgo9lkabnat2lo8Z ~]# systemctl status firewalld ``` -5. 设置是否开机自启。 +5. 设置/禁用防火墙服务开机自启。 ```Shell [root@iZwz97tbgo9lkabnat2lo8Z ~]# systemctl enable firewalld @@ -982,16 +1291,44 @@ build environment: ### 计划任务 -1. **crontab**命令。 +1. 在指定的时间执行命令 + + - **at** - 将任务排队,在指定的时间执行。 + - **atq** - 查看待执行的任务队列。 + - **atrm** - 从队列中删除待执行的任务。 + + 指定3天以后下午5点要执行的任务。 + + ```Shell + [hellokitty@izwz97tbgo9lkabnat2lo8z ~]$ at 5pm+3days + at> rm -f /root/*.html + at> + job 9 at Wed Jun 5 17:00:00 2019 + ``` + + 查看待执行的任务队列。 + + ```Shell + [hellokitty@izwz97tbgo9lkabnat2lo8z ~]$ atq + 9 Wed Jun 5 17:00:00 2019 a hellokitty + ``` + + 从队列中删除指定的任务。 + + ```Shell + [hellokitty@izwz97tbgo9lkabnat2lo8z ~]$ atrm 9 + ``` + +2. 计划任务表 - **crontab**。 ```Shell [root@iZwz97tbgo9lkabnat2lo8Z ~]# crontab -e * * * * * echo "hello, world!" >> /root/hello.txt 59 23 * * * rm -f /root/*.log ``` - > 说明:输入`crontab -e`命令会打开vim来编辑Cron表达式并指定触发的任务,上面我们定制了两个计划任务,一个是每分钟向/root目录下的hello.txt中追加输出`hello, world!`;另一个是每天23时59分执行删除/root目录下以log为后缀名的文件。如果不知道Cron表达式如何书写,可以参照/etc/crontab文件中的提示(下面会讲到)或者用谷歌或百度搜索一下,也可以使用Cron表达式在线生成器来生成Cron表达式。 + > 说明:输入`crontab -e`命令会打开vim来编辑Cron表达式并指定触发的任务,上面我们定制了两个计划任务,一个是每分钟向/root目录下的hello.txt中追加输出`hello, world!`;另一个是每天23时59分执行删除/root目录下以log为后缀名的文件。如果不知道Cron表达式如何书写,可以参照/etc/crontab文件中的提示(下面会讲到)或者用谷歌搜索一下,也可以使用Cron表达式在线生成器来生成Cron表达式。 -2. crontab相关文件。 + 和crontab相关的文件在`/etc`目录下,通过修改`/etc`目录下的crontab文件也能够定制计划任务。 ```Shell [root@iZwz97tbgo9lkabnat2lo8Z ~]# cd /etc @@ -1021,19 +1358,30 @@ build environment: 14 # * * * * * user-name command to be executed ``` - 通过修改`/etc`目录下的crontab文件也能够定制计划任务。 ### 网络访问和管理 1. 安全远程连接 - **ssh**。 + ```Shell + [hellokitty@izwz97tbgo9lkabnat2lo8z ~]$ ssh root@120.77.222.217 + The authenticity of host '120.77.222.217 (120.77.222.217)' can't be established. + ECDSA key fingerprint is SHA256:BhUhykv+FvnIL03I9cLRpWpaCxI91m9n7zBWrcXRa8w. + ECDSA key fingerprint is MD5:cc:85:e9:f0:d7:07:1a:26:41:92:77:6b:7f:a0:92:65. + Are you sure you want to continue connecting (yes/no)? yes + Warning: Permanently added '120.77.222.217' (ECDSA) to the list of known hosts. + root@120.77.222.217's password: + ``` + 2. 通过网络获取资源 - **wget**。 - -b 后台下载模式 - -O 下载到指定的目录 - -r 递归下载 -3. 显示/操作网络配置(旧) - **ifconfig**。 +3. 发送和接收邮件 - **mail**。 + +4. 网络配置工具(旧) - **ifconfig**。 ```Shell [root@iZwz97tbgo9lkabnat2lo8Z ~]# ifconfig eth0 @@ -1046,7 +1394,7 @@ build environment: TX errors 0 dropped 0 overruns 0 carrier 0 collisions ``` -4. 显示/操作网络配置(新) - **ip**。 +5. 网络配置工具(新) - **ip**。 ```Shell [root@iZwz97tbgo9lkabnat2lo8Z ~]# ip address @@ -1060,7 +1408,7 @@ build environment: valid_lft forever preferred_lft forever ``` -5. 网络可达性检查 - **ping**。 +6. 网络可达性检查 - **ping**。 ```Shell [root@iZwz97tbgo9lkabnat2lo8Z ~]# ping www.baidu.com -c 3 @@ -1073,48 +1421,56 @@ build environment: rtt min/avg/max/mdev = 36.392/36.406/36.427/0.156 ms ``` -6. 查看网络服务和端口 - **netstat**。 +7. 显示或管理路由表 - **route**。 + +8. 查看网络服务和端口 - **netstat** / **ss**。 ```Shell [root@iZwz97tbgo9lkabnat2lo8Z ~]# netstat -nap | grep nginx ``` -7. 安全文件拷贝 - **scp**。 +9. 网络监听抓包 - **tcpdump**。 + +10. 安全文件拷贝 - **scp**。 ```Shell [root@iZwz97tbgo9lkabnat2lo8Z ~]# scp root@1.2.3.4:/root/guido.jpg hellokitty@4.3.2.1:/home/hellokitty/pic.jpg ``` -8. 安全文件传输 - **sftp**。 +11. 文件同步工具 - **rsync**。 - ```Shell - [root@iZwz97tbgo9lkabnat2lo8Z ~]# sftp root@120.77.222.217 - root@120.77.222.217's password: - Connected to 120.77.222.217. - sftp> - ``` + > 说明:使用`rsync`可以实现文件的自动同步,这个对于文件服务器来说相当重要。关于这个命令的用法,我们在后面讲项目部署的时候为大家详细说明。 - - `help`:显示帮助信息。 +12. 安全文件传输 - **sftp**。 - - `ls`/`lls`:显示远端/本地目录列表。 + ```Shell + [root@iZwz97tbgo9lkabnat2lo8Z ~]# sftp root@120.77.222.217 + root@120.77.222.217's password: + Connected to 120.77.222.217. + sftp> + ``` - - `cd`/`lcd`:切换远端/本地路径。 + - `help`:显示帮助信息。 - - `mkdir`/`lmkdir`:创建远端/本地目录。 + - `ls`/`lls`:显示远端/本地目录列表。 - - `pwd`/`lpwd`:显示远端/本地当前工作目录。 + - `cd`/`lcd`:切换远端/本地路径。 - - `get`:下载文件。 + - `mkdir`/`lmkdir`:创建远端/本地目录。 - - `put`:上传文件。 + - `pwd`/`lpwd`:显示远端/本地当前工作目录。 - - `rm`:删除远端文件。 + - `get`:下载文件。 - - `bye`/`exit`/`quit`:退出sftp。 + - `put`:上传文件。 + + - `rm`:删除远端文件。 + + - `bye`/`exit`/`quit`:退出sftp。 ### 进程管理 -1. **ps** - 查询进程。 +1. 查看进程 - **ps**。 ```Shell [root@iZwz97tbgo9lkabnat2lo8Z ~]# ps -ef @@ -1127,9 +1483,56 @@ build environment: mysql 25257 1 0 Jun25 ? 00:00:39 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid ``` -2. **kill** - 终止进程。 +2. 显示进程状态树 - **pstree**。 + + ```Shell + [root@izwz97tbgo9lkabnat2lo8z ~]# pstree + systemd─┬─AliYunDun───18*[{AliYunDun}] + ├─AliYunDunUpdate───3*[{AliYunDunUpdate}] + ├─2*[agetty] + ├─aliyun-service───2*[{aliyun-service}] + ├─atd + ├─auditd───{auditd} + ├─dbus-daemon + ├─dhclient + ├─irqbalance + ├─lvmetad + ├─mysqld───28*[{mysqld}] + ├─nginx───2*[nginx] + ├─ntpd + ├─polkitd───6*[{polkitd}] + ├─rsyslogd───2*[{rsyslogd}] + ├─sshd───sshd───bash───pstree + ├─systemd-journal + ├─systemd-logind + ├─systemd-udevd + └─tuned───4*[{tuned}] + ``` + +3. 查找与指定条件匹配的进程 - **pgrep**。 ```Shell + [hellokitty@izwz97tbgo9lkabnat2lo8z ~]$ pgrep mysqld + 3584 + ``` + +4. 通过进程号终止进程 - **kill**。 + + ```Shell + [hellokitty@izwz97tbgo9lkabnat2lo8z ~]$ kill -l + 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP + 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 + 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM + 16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP + 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ + 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR + 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 + 38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8 + 43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13 + 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12 + 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 + 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 + 63) SIGRTMAX-1 64) SIGRTMAX [root@iZwz97tbgo9lkabnat2lo8Z ~]# kill 1234 [root@iZwz97tbgo9lkabnat2lo8Z ~]# kill -9 1234 ``` @@ -1140,10 +1543,26 @@ build environment: ps -ef | grep redis | grep -v grep | awk '{print $2}' | xargs kill ``` -3. 将进程置于后台运行。 +5. 通过进程名终止进程 - **killall** / **pkill**。 - - `Ctrl+Z` - - `&` + 结束名为mysqld的进程。 + + ```Shell + [root@izwz97tbgo9lkabnat2lo8z ~]# pkill mysqld + ``` + + 结束hellokitty用户的所有进程。 + + ```Shell + [root@izwz97tbgo9lkabnat2lo8z ~]# pkill -u hellokitty + ``` + + > 说明:这样的操作会让hellokitty用户和服务器断开连接。 + +6. 将进程置于后台运行。 + + - `Ctrl+Z` - 快捷键,用于停止进程并置于后台。 + - `&` - 将进程置于后台运行。 ```Shell [root@iZwz97tbgo9lkabnat2lo8Z ~]# mongod & @@ -1153,7 +1572,7 @@ build environment: [4]+ Stopped redis-server ``` -4. **jobs** - 查询后台进程。 +7. 查询后台进程 - **jobs**。 ```Shell [root@iZwz97tbgo9lkabnat2lo8Z ~]# jobs @@ -1162,7 +1581,7 @@ build environment: [4]+ Stopped redis-server ``` -5. **bg** - 让进程在后台继续运行。 +8. 让进程在后台继续运行 - **bg**。 ```Shell [root@iZwz97tbgo9lkabnat2lo8Z ~]# bg %4 @@ -1173,37 +1592,92 @@ build environment: [4]- Running redis-server & ``` -6. **fg** - 将后台进程置于前台。 +9. 将后台进程置于前台 - **fg**。 ```Shell [root@iZwz97tbgo9lkabnat2lo8Z ~]# fg %4 redis-server - ^C5554:signal-handler (1530025281) Received SIGINT scheduling shutdown... - 5554:M 26 Jun 23:01:21.413 # User requested shutdown... - 5554:M 26 Jun 23:01:21.413 * Saving the final RDB snapshot before exiting. - 5554:M 26 Jun 23:01:21.415 * DB saved on disk - 5554:M 26 Jun 23:01:21.415 # Redis is now ready to exit, bye bye... ``` > 说明:置于前台的进程可以使用`Ctrl+C`来终止它。 -7. **top** - 进程监控。 +10. 调整程序/进程运行时优先级 - **nice** / **renice**。 - ```Shell - [root@iZwz97tbgo9lkabnat2lo8Z ~]# top - top - 23:04:23 up 3 days, 14:10, 1 user, load average: 0.00, 0.01, 0.05 - Tasks: 65 total, 1 running, 64 sleeping, 0 stopped, 0 zombie - %Cpu(s): 0.3 us, 0.3 sy, 0.0 ni, 99.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st - KiB Mem : 1016168 total, 191060 free, 324700 used, 500408 buff/cache - KiB Swap: 0 total, 0 free, 0 used. 530944 avail Mem - ... - ``` +11. 用户登出后进程继续工作 - **nohup**。 -### 系统性能 + ```Shell + [root@izwz97tbgo9lkabnat2lo8z ~]# nohup ping www.baidu.com > result.txt & + ``` -1. 查看系统活动信息 - **sar**。 +12. 跟踪进程系统调用情况 - **strace**。 -2. 查看内存使用情况 - **free**。 + ```Shell + [root@izwz97tbgo9lkabnat2lo8z ~]# pgrep mysqld + 8803 + [root@izwz97tbgo9lkabnat2lo8z ~]# strace -c -p 8803 + strace: Process 8803 attached + ^Cstrace: Process 8803 detached + % time seconds usecs/call calls errors syscall + ------ ----------- ----------- --------- --------- ---------------- + 99.18 0.005719 5719 1 restart_syscall + 0.49 0.000028 28 1 mprotect + 0.24 0.000014 14 1 clone + 0.05 0.000003 3 1 mmap + 0.03 0.000002 2 1 accept + ------ ----------- ----------- --------- --------- ---------------- + 100.00 0.005766 5 total + ``` + + > 说明:这个命令的用法和参数都比较复杂,建议大家在真正用到这个命令的时候再根据实际需要进行了解。 + +13. 查看当前运行级别 - **runlevel**。 + + ```Shell + [root@izwz97tbgo9lkabnat2lo8z ~]# runlevel + N 3 + ``` + +14. 实时监控进程占用资源状况 - **top**。 + + ```Shell + [root@iZwz97tbgo9lkabnat2lo8Z ~]# top + top - 23:04:23 up 3 days, 14:10, 1 user, load average: 0.00, 0.01, 0.05 + Tasks: 65 total, 1 running, 64 sleeping, 0 stopped, 0 zombie + %Cpu(s): 0.3 us, 0.3 sy, 0.0 ni, 99.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st + KiB Mem : 1016168 total, 191060 free, 324700 used, 500408 buff/cache + KiB Swap: 0 total, 0 free, 0 used. 530944 avail Mem + ... + ``` + + - `-c` - 显示进程的整个路径。 + - `-d` - 指定两次刷屏之间的间隔时间(秒为单位)。 + - `-i` - 不显示闲置进程或僵尸进程。 + - `-p` - 显示指定进程的信息。 + +### 系统诊断 + +1. 系统启动异常诊断 - **dmesg**。 + +2. 查看系统活动信息 - **sar**。 + + ```Shell + [root@izwz97tbgo9lkabnat2lo8z ~]# sar -u -r 5 10 + Linux 3.10.0-957.10.1.el7.x86_64 (izwz97tbgo9lkabnat2lo8z) 06/02/2019 _x86_64_ (2 CPU) + + 06:48:30 PM CPU %user %nice %system %iowait %steal %idle + 06:48:35 PM all 0.10 0.00 0.10 0.00 0.00 99.80 + + 06:48:30 PM kbmemfree kbmemused %memused kbbuffers kbcached kbcommit %commit kbactive kbinact kbdirty + 06:48:35 PM 1772012 2108392 54.33 102816 1634528 784940 20.23 793328 1164704 0 + ``` + + - `-A` - 显示所有设备(CPU、内存、磁盘)的运行状况。 + - `-u` - 显示所有CPU的负载情况。 + - `-d` - 显示所有磁盘的使用情况。 + - `-r` - 显示内存的使用情况。 + - `-n` - 显示网络运行状态。 + +3. 查看内存使用情况 - **free**。 ```Shell [root@iZwz97tbgo9lkabnat2lo8Z ~]# free @@ -1212,7 +1686,26 @@ build environment: Swap: 0 0 0 ``` -3. 查看进程使用内存状况 - **pmap**。 +4. 虚拟内存统计 - **vmstat**。 + + ```Shell + [root@iZ8vba0s66jjlfmo601w4xZ ~]# vmstat + procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- + r b swpd free buff cache si so bi bo in cs us sy id wa st + 2 0 0 204020 79036 667532 0 0 5 18 101 58 1 0 99 0 0 + ``` + +5. CPU信息统计 - **mpstat**。 + + ```Shell + [root@iZ8vba0s66jjlfmo601w4xZ ~]# mpstat + Linux 3.10.0-957.5.1.el7.x86_64 (iZ8vba0s66jjlfmo601w4xZ) 05/30/2019 _x86_64_ (1 CPU) + + 01:51:54 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle + 01:51:54 AM all 0.71 0.00 0.17 0.04 0.00 0.00 0.00 0.00 0.00 99.07 + ``` + +6. 查看进程使用内存状况 - **pmap**。 ```Shell [root@iZwz97tbgo9lkabnat2lo8Z ~]# ps @@ -1231,7 +1724,7 @@ build environment: ... ``` -4. 报告设备CPU和I/O统计信息 - **iostat**。 +7. 报告设备CPU和I/O统计信息 - **iostat**。 ```Shell [root@iZwz97tbgo9lkabnat2lo8Z ~]# iostat @@ -1243,3 +1736,79 @@ build environment: vdb 0.00 0.01 0.00 2088 0 ``` +8. 显示所有PCI设备 - **lspci**。 + + ```Shell + [root@izwz97tbgo9lkabnat2lo8z ~]# lspci + 00:00.0 Host bridge: Intel Corporation 440FX - 82441FX PMC [Natoma] (rev 02) + 00:01.0 ISA bridge: Intel Corporation 82371SB PIIX3 ISA [Natoma/Triton II] + 00:01.1 IDE interface: Intel Corporation 82371SB PIIX3 IDE [Natoma/Triton II] + 00:01.2 USB controller: Intel Corporation 82371SB PIIX3 USB [Natoma/Triton II] (rev 01) + 00:01.3 Bridge: Intel Corporation 82371AB/EB/MB PIIX4 ACPI (rev 03) + 00:02.0 VGA compatible controller: Cirrus Logic GD 5446 + 00:03.0 Ethernet controller: Red Hat, Inc. Virtio network device + 00:04.0 Communication controller: Red Hat, Inc. Virtio console + 00:05.0 SCSI storage controller: Red Hat, Inc. Virtio block device + 00:06.0 SCSI storage controller: Red Hat, Inc. Virtio block device + 00:07.0 Unclassified device [00ff]: Red Hat, Inc. Virtio memory balloon + ``` + +9. 显示进程间通信设施的状态 - **ipcs**。 + + ```Shell + [root@iZ8vba0s66jjlfmo601w4xZ ~]# ipcs + + ------ Message Queues -------- + key msqid owner perms used-bytes messages + + ------ Shared Memory Segments -------- + key shmid owner perms bytes nattch status + + ------ Semaphore Arrays -------- + key semid owner perms nsems + ``` + +### 相关资源 + +1. Linux命令行常用快捷键 + + | 快捷键 | 功能说明 | + | ---------- | -------------------------------------------- | + | tab | 自动补全命令或路径 | + | Ctrl+a | 将光标移动到命令行行首 | + | Ctrl+e | 将光标移动到命令行行尾 | + | Ctrl+f | 将光标向右移动一个字符 | + | Ctrl+b | 将光标向左移动一个字符 | + | Ctrl+k | 剪切从光标到行尾的字符 | + | Ctrl+u | 剪切从光标到行首的字符 | + | Ctrl+w | 剪切光标前面的一个单词 | + | Ctrl+y | 复制剪切命名剪切的内容 | + | Ctrl+c | 中断正在执行的任务 | + | Ctrl+h | 删除光标前面的一个字符 | + | Ctrl+d | 退出当前命令行 | + | Ctrl+r | 搜索历史命令 | + | Ctrl+g | 退出历史命令搜索 | + | Ctrl+l | 清除屏幕上所有内容在屏幕的最上方开启一个新行 | + | Ctrl+s | 锁定终端使之暂时无法输入内容 | + | Ctrl+q | 退出终端锁定 | + | Ctrl+z | 将正在终端执行的任务停下来放到后台 | + | !! | 执行上一条命令 | + | !数字 | 执行数字对应的历史命令 | + | !字母 | 执行最近的以字母打头的命令 | + | !$ / Esc+. | 获得上一条命令最后一个参数 | + | Esc+b | 移动到当前单词的开头 | + | Esc+f | 移动到当前单词的结尾 | + +2. man查阅命令手册的内容说明 + + | 手册中的标题 | 功能说明 | + | ------------ | ------------------------------------------------------------ | + | NAME | 命令的说明和介绍 | + | SYNOPSIS | 使用该命令的基本语法 | + | DESCRIPTION | 使用该命令的详细描述,各个参数的作用,有时候这些信息会出现在OPTIONS中 | + | OPTIONS | 命令相关参数选项的说明 | + | EXAMPLES | 使用该命令的参考例子 | + | EXIT STATUS | 命令结束的退出状态码,通常0表示成功执行 | + | SEE ALSO | 和命令相关的其他命令或信息 | + | BUGS | 和命令相关的缺陷的描述 | + | AUTHOR | 该命令的作者介绍 | diff --git a/Day31-35/res/andrew-tanenbaum.png b/Day31-35/res/andrew-tanenbaum.png deleted file mode 100644 index 195e6e7..0000000 Binary files a/Day31-35/res/andrew-tanenbaum.png and /dev/null differ diff --git a/Day31-35/res/andrew.jpg b/Day31-35/res/andrew.jpg new file mode 100644 index 0000000..8f001d3 Binary files /dev/null and b/Day31-35/res/andrew.jpg differ diff --git a/Day31-35/res/dennis-ritchie.png b/Day31-35/res/dmr.png similarity index 100% rename from Day31-35/res/dennis-ritchie.png rename to Day31-35/res/dmr.png diff --git a/Day31-35/res/Ken-Thompson.png b/Day31-35/res/ken_old.png similarity index 100% rename from Day31-35/res/Ken-Thompson.png rename to Day31-35/res/ken_old.png diff --git a/Day31-35/res/ken_young.jpg b/Day31-35/res/ken_young.jpg new file mode 100644 index 0000000..46e3009 Binary files /dev/null and b/Day31-35/res/ken_young.jpg differ diff --git a/Day31-35/res/linus-torvalds.png b/Day31-35/res/linus.png similarity index 100% rename from Day31-35/res/linus-torvalds.png rename to Day31-35/res/linus.png diff --git a/Day31-35/res/pdp-11.jpg b/Day31-35/res/pdp-11.jpg new file mode 100644 index 0000000..6b4486b Binary files /dev/null and b/Day31-35/res/pdp-11.jpg differ diff --git a/Day31-35/res/rk05.jpg b/Day31-35/res/rk05.jpg new file mode 100644 index 0000000..a10eaec Binary files /dev/null and b/Day31-35/res/rk05.jpg differ diff --git a/Day36-40/关系型数据库MySQL.md b/Day36-40/36.关系型数据库MySQL.md similarity index 77% rename from Day36-40/关系型数据库MySQL.md rename to Day36-40/36.关系型数据库MySQL.md index 57e7dce..9d53f6c 100644 --- a/Day36-40/关系型数据库MySQL.md +++ b/Day36-40/36.关系型数据库MySQL.md @@ -69,61 +69,59 @@ - 启动MySQL服务。 - 先修改MySQL的配置文件(`/etc/my.cnf`)添加一行`skip-grant-tables`,可以设置不进行身份验证即可连接MySQL服务器,然后就可以以超级管理员(root)身份登录。 - - ```Shell - vim /etc/my.cnf - ``` - - ```INI - [mysqld] - skip-grant-tables - - datadir=/var/lib/mysql - socket=/var/lib/mysql/mysql.sock - - symbolic-links=0 - - log-error=/var/log/mysqld.log - pid-file=/var/run/mysqld/mysqld.pid - ``` - - 接下来可以使用下面的命令来启动MySQL。 + 可以使用下面的命令来启动MySQL。 ```Shell service mysqld start ``` - 在CentOS 7中建议使用下面的命令来启动MySQL。 + 在CentOS 7中,更推荐使用下面的命令来启动MySQL。 ```Shell systemctl start mysqld ``` + 启动MySQL成功后,可以通过下面的命令来检查网络端口使用情况,MySQL默认使用3306端口。 + + ```Shell + netstat -nap | grep mysql + ``` + + 也可以使用下面的命令查找是否有名为mysqld的进程。 + + ```Shell + pgrep mysqld + ``` + - 使用MySQL客户端工具连接服务器。 命令行工具: ```Shell - mysql -u root - ``` - - 修改超级管理员(root)的访问口令为i_LOVE_macos_123。 - - ```SQL - use mysql; - update user set authentication_string=password('i_LOVE_macos_123') where user='root'; - flush privileges; - ``` - - 将MySQL配置文件中的`skip-grant-tables`去掉,然后重启服务器,重新登录。这一次需要提供用户名和口令才能连接MySQL服务器。 - - ```Shell - systemctl restart mysqld mysql -u root -p ``` - 也可以选择图形化的客户端工具来连接MySQL服务器,可以选择下列工具之一: + > 说明:启动客户端时,`-u`参数用来指定用户名,MySQL默认的超级管理账号为`root`;`-p`表示要输入密码(用户口令);如果连接的是其他主机而非本机,可以用`-h`来指定连接主机的主机名或IP地址。 + + 如果是首次安装MySQL,可以使用下面的命令来找到默认的初始密码。 + + ```Shell + cat /var/log/mysqld.log | grep password + ``` + + 上面的命令会查看MySQL的日志带有password的行,在显示的结果中`root@localhost:`后面的部分就是默认设置的初始密码。 + + 修改超级管理员(root)的访问口令为`123456`。 + + ```SQL + set global validate_password_policy=0; + set global validate_password_length=6; + alter user 'root'@'localhost' identified by '123456'; + ``` + + > 说明:MySQL默认不允许使用弱口令作为用户口令,所以我们通过上面的前两条命令修改了验证用户口令的策略和口令的长度。事实上我们不应该使用弱口令,因为存在用户口令被暴力破解的风险。近年来,攻击数据库窃取数据和劫持数据库勒索比特币的事件屡见不鲜,要避免这些潜在的风险,最为重要的一点是不要让数据库服务器暴露在公网上(最好的做法是将数据库置于内网,至少要做到不向公网开放数据库服务器的访问端口),另外要保管好`root`账号的口令,应用系统需要访问数据库时,通常不使用`root`账号进行访问,而是创建其他拥有适当权限的账号来访问。 + + 再次使用客户端工具连接MySQL服务器时,就可以使用新设置的口令了。在实际开发中,为了方便用户操作,可以选择图形化的客户端工具来连接MySQL服务器,包括: - MySQL Workbench(官方提供的工具) - Navicat for MySQL(界面简单优雅,功能直观强大) @@ -185,35 +183,32 @@ -- 创建学院表 create table tb_college ( - collid int not null auto_increment comment '编号', - collname varchar(50) not null comment '名称', - collmaster varchar(20) not null comment '院长', - collweb varchar(511) default '' comment '网站', + collid int auto_increment comment '编号', + collname varchar(50) not null comment '名称', + collmaster varchar(20) not null comment '院长', primary key (collid) ); -- 创建学生表 create table tb_student ( - stuid int not null comment '学号', - stuname varchar(20) not null comment '姓名', - stusex bit default 1 comment '性别', - stubirth date not null comment '出生日期', - stuaddr varchar(255) default '' comment '籍贯', - collid int not null comment '所属学院', + stuid int not null comment '学号', + stuname varchar(20) not null comment '姓名', + stusex boolean default 1 comment '性别', + stubirth date not null comment '出生日期', + stuaddr varchar(255) default '' comment '籍贯', + collid int not null comment '所属学院', primary key (stuid), foreign key (collid) references tb_college (collid) ); - -- alter table tb_student add constraint fk_student_collid foreign key (collid) references tb_college (collid); - -- 创建教师表 create table tb_teacher ( - teaid int not null comment '工号', - teaname varchar(20) not null comment '姓名', - teatitle varchar(10) default '助教' comment '职称', - collid int not null comment '所属学院', + teaid int not null comment '工号', + teaname varchar(20) not null comment '姓名', + teatitle varchar(10) default '助教' comment '职称', + collid int not null comment '所属学院', primary key (teaid), foreign key (collid) references tb_college (collid) ); @@ -221,40 +216,37 @@ -- 创建课程表 create table tb_course ( - couid int not null comment '编号', - couname varchar(50) not null comment '名称', - coucredit int not null comment '学分', - teaid int not null comment '授课老师', + couid int not null comment '编号', + couname varchar(50) not null comment '名称', + coucredit int not null comment '学分', + teaid int not null comment '授课老师', primary key (couid), foreign key (teaid) references tb_teacher (teaid) ); -- 创建选课记录表 - create table tb_score + create table tb_record ( - scid int auto_increment comment '选课记录编号', - stuid int not null comment '选课学生', - couid int not null comment '所选课程', - scdate datetime comment '选课时间日期', - scmark decimal(4,1) comment '考试成绩', - primary key (scid), - foreign key (stuid) references tb_student (stuid), - foreign key (couid) references tb_course (couid) + recid int auto_increment comment '选课记录编号', + sid int not null comment '选课学生', + cid int not null comment '所选课程', + seldate datetime default now() comment '选课时间日期', + score decimal(4,1) comment '考试成绩', + primary key (recid), + foreign key (sid) references tb_student (stuid), + foreign key (cid) references tb_course (couid), + unique (sid, cid) ); - - -- 添加唯一性约束(一个学生选某个课程只能选一次) - alter table tb_score add constraint uni_score_stuid_couid unique (stuid, couid); ``` 2. DML ```SQL - -- 插入学院数据 - insert into tb_college (collname, collmaster, collweb) values - ('计算机学院', '左冷禅', 'http://www.abc.com'), - ('外国语学院', '岳不群', 'http://www.xyz.com'), - ('经济管理学院', '风清扬', 'http://www.foo.com'); + insert into tb_college (collname, collmaster) values + ('计算机学院', '左冷禅'), + ('外国语学院', '岳不群'), + ('经济管理学院', '风清扬'); -- 插入学生数据 insert into tb_student (stuid, stuname, stusex, stubirth, stuaddr, collid) values @@ -297,7 +289,7 @@ (9999, '审计学', 3, 3366); -- 插入选课数据 - insert into tb_score (stuid, couid, scdate, scmark) values + insert into tb_record (sid, cid, seldate, score) values (1001, 1111, '2017-09-01', 95), (1001, 2222, '2017-09-01', 87.5), (1001, 3333, '2017-09-01', 100), @@ -312,9 +304,9 @@ (1378, 1111, '2017-09-05', 82), (1378, 7777, '2017-09-02', 65.5), (2035, 7777, '2018-09-03', 88), - (2035, 9999, curdate(), null), - (3755, 1111, date(now()), null), - (3755, 8888, date(now()), null), + (2035, 9999, default, null), + (3755, 1111, default, null), + (3755, 8888, default, null), (3755, 9999, '2017-09-01', 92); ``` @@ -358,16 +350,14 @@ select stuname from tb_student where stuaddr is not null; -- 查询学生选课的所有日期(去重) - select distinct scdate from tb_score; + select distinct seldate from tb_record; -- 查询学生的家庭住址(去重) select distinct stuaddr from tb_student where stuaddr is not null; -- 查询男学生的姓名和生日按年龄从大到小排列(排序) - -- asc (ascending) - 升序(从小到大)/ desc (descending) - 降序(从大到小) - select stuname as 姓名, year(now())-year(stubirth) as 年龄 from tb_student where stusex=1 order by 年龄 desc; + select stuname as 姓名, datediff(curdate(), stubirth) div 365 as 年龄 from tb_student where stusex=1 order by 年龄 desc; - -- 聚合函数:max / min / count / sum / avg -- 查询年龄最大的学生的出生日期(聚合函数) select min(stubirth) from tb_student; @@ -378,51 +368,42 @@ select stusex, count(*) from tb_student group by stusex; -- 查询课程编号为1111的课程的平均成绩(筛选和聚合函数) - select avg(scmark) from tb_score where couid=1111; + select avg(score) from tb_record where cid=1111; -- 查询学号为1001的学生所有课程的平均分(筛选和聚合函数) - select avg(scmark) from tb_score where stuid=1001; + select avg(score) from tb_record where sid=1001; -- 查询每个学生的学号和平均成绩(分组和聚合函数) - select stuid as 学号, avg(scmark) as 平均分 from tb_score group by stuid; + select sid as 学号, avg(score) as 平均分 from tb_record group by sid; -- 查询平均成绩大于等于90分的学生的学号和平均成绩 -- 分组以前的筛选使用where子句 / 分组以后的筛选使用having子句 - select stuid as 学号, avg(scmark) as 平均分 from tb_score group by stuid having 平均分>=90; + select sid as 学号, avg(score) as 平均分 from tb_record group by sid having 平均分>=90; -- 查询年龄最大的学生的姓名(子查询/嵌套的查询) - select stuname from tb_student where stubirth=( - select min(stubirth) from tb_student - ); + select stuname from tb_student where stubirth=( select min(stubirth) from tb_student ); -- 查询年龄最大的学生姓名和年龄(子查询+运算) - select stuname as 姓名, year(now())-year(stubirth) as 年龄 from tb_student where stubirth=( - select min(stubirth) from tb_student - ); + select stuname as 姓名, datediff(curdate(), stubirth) div 365 as 年龄 from tb_student where stubirth=( select min(stubirth) from tb_student ); -- 查询选了两门以上的课程的学生姓名(子查询/分组条件/集合运算) - select stuname from tb_student where stuid=( - select stuid from tb_score group by stuid having count(stuid)>2 - ) + select stuname from tb_student where stuid in ( select stuid from tb_record group by stuid having count(stuid)>2 ); -- 查询学生姓名、课程名称以及成绩(连接查询) - select stuname, couname, scmark from tb_student t1, tb_course t2, tb_score t3 where t1.stuid=t3.stuid and t2.couid=t3.couid and scmark is not null; + select stuname, couname, score from tb_student t1, tb_course t2, tb_record t3 where stuid=sid and couid=cid and score is not null; -- 查询学生姓名、课程名称以及成绩按成绩从高到低查询第11-15条记录(内连接+分页) - select stuname, couname, scmark from tb_student t1 inner join tb_score t3 on t1.stuid=t3.stuid inner join tb_course t2 on t2.couid=t3.couid where scmark is not null order by scmark desc limit 5 offset 10; + select stuname, couname, score from tb_student inner join tb_record on stuid=sid inner join tb_course on couid=cid where score is not null order by score desc limit 5 offset 10; - select stuname, couname, scmark from tb_student t1 inner join tb_score t3 on t1.stuid=t3.stuid inner join tb_course t2 on t2.couid=t3.couid where scmark is not null order by scmark desc limit 10, 5; + select stuname, couname, score from tb_student inner join tb_record on stuid=sid inner join tb_course on couid=cid where score is not null order by score desc limit 10, 5; -- 查询选课学生的姓名和平均成绩(子查询和连接查询) - select stuname, avgmark from tb_student t1, (select stuid, avg(scmark) as avgmark from tb_score group by stuid) t2 where t1.stuid=t2.stuid; + select stuname, avgmark from tb_student, ( select sid, avg(score) as avgmark from tb_record group by sid ) temp where stuid=sid; - select stuname, avgmark from tb_student t1 inner join - (select stuid, avg(scmark) as avgmark from tb_score group by stuid) t2 on t1.stuid=t2.stuid; + select stuname, avgmark from tb_student inner join ( select sid, avg(score) as avgmark from tb_record group by sid ) temp on stuid=sid; - -- 内连接(inner join)- 只有满足连接条件的记录才会被查出来 - -- 外连接(outer join)- 左外连接(left outer join) / 右外连接(right outer join) / 全外连接 -- 查询每个学生的姓名和选课数量(左外连接和子查询) - select stuname, ifnull(total, 0) from tb_student t1 left outer join (select stuid, count(stuid) as total from tb_score group by stuid) t2 on t1.stuid=t2.stuid; + select stuname, ifnull(total, 0) from tb_student left outer join ( select sid, count(sid) as total from tb_record group by sid ) temp on stuid=sid; ``` 4. DCL @@ -468,6 +449,26 @@ - 隔离性:多个事务并发执行时,一个事务的执行不应影响其他事务的执行 - 持久性:已被提交的事务对数据库的修改应该永久保存在数据库中 +3. MySQL中的事务操作 + + - 开启事务环境 + + ```SQL + start transaction + ``` + + - 提交事务 + + ```Shell + commit + ``` + + - 回滚事务 + + ```SQL + rollback + ``` + ### Python数据库编程 我们用如下所示的数据库来演示在Python中如何访问MySQL数据库。 @@ -696,4 +697,4 @@ insert into tb_emp values if __name__ == '__main__': main() - ``` \ No newline at end of file + ``` diff --git a/Day36-40/NoSQL入门.md b/Day36-40/39.NoSQL入门.md similarity index 98% rename from Day36-40/NoSQL入门.md rename to Day36-40/39.NoSQL入门.md index 0d6808d..e7345ec 100644 --- a/Day36-40/NoSQL入门.md +++ b/Day36-40/39.NoSQL入门.md @@ -345,8 +345,6 @@ mongo --host 172.18.61.250 MongoDB shell version v3.6.5 connecting to: mongodb://172.18.61.250:27017/ -... -> ``` 1. 查看、创建和删除数据库。 @@ -489,7 +487,7 @@ connecting to: mongodb://172.18.61.250:27017/ 使用MongoDB可以非常方便的配置数据复制,通过冗余数据来实现数据的高可用以及灾难恢复,也可以通过数据分片来应对数据量迅速增长的需求。关于MongoDB更多的操作可以查阅[官方文档](https://mongodb-documentation.readthedocs.io/en/latest/) ,同时推荐大家阅读Kristina Chodorow写的[《MongoDB权威指南》](http://www.ituring.com.cn/book/1172)。 -####在Python程序中操作MongoDB +#### 在Python程序中操作MongoDB 可以通过pip安装pymongo来实现对MongoDB的操作。 @@ -544,4 +542,4 @@ python3 >>> ``` -关于PyMongo更多的知识可以通过它的[官方文档](https://api.mongodb.com/python/current/tutorial.html)进行了解。 \ No newline at end of file +关于PyMongo更多的知识可以通过它的[官方文档](https://api.mongodb.com/python/current/tutorial.html)进行了解,也可以使用[MongoEngine]()这样的库来简化Python程序对MongoDB的操作,除此之外,还有以异步I/O方式访问MongoDB的三方库[motor]()都是不错的选择。 \ No newline at end of file diff --git a/Day36-40/code/HRS_create_and_init.sql b/Day36-40/code/HRS_create_and_init.sql index 0e52190..b8bc864 100644 --- a/Day36-40/code/HRS_create_and_init.sql +++ b/Day36-40/code/HRS_create_and_init.sql @@ -51,15 +51,15 @@ insert into tb_emp values (3588, '朱九真', '会计', 5566, 2500, null, 10); --- 查询薪资最高的员工姓名和工资 +-- 查询月薪最高的员工姓名和工资 --- 查询员工的姓名和年薪((月薪+补贴)*12) +-- 查询员工的姓名和年薪((月薪+补贴)*13) -- 查询有员工的部门的编号和人数 -- 查询所有部门的名称和人数 --- 查询薪资最高的员工(Boss除外)的姓名和工资 +-- 查询月薪最高的员工(Boss除外)的姓名和工资 -- 查询薪水超过平均薪水的员工的姓名和工资 @@ -69,24 +69,4 @@ insert into tb_emp values -- 查询主管的姓名和职位 --- 查询薪资排名4~6名的员工姓名和工资 - --- use hrs; - --- drop procedure if exists sp_avg_sal_by_dept; - - --- create procedure sp_avg_sal_by_dept(dno integer, out avg_sal float) --- begin --- select avg(sal) into avg_sal from tb_emp where dno=dno; --- end; - - - - --- call sp_avg_sal_by_dept(10, @avgSal); - --- select @avgSal; - - - +-- 查询月薪排名4~6名的员工姓名和工资 diff --git a/Day36-40/code/SRS_create_and_init.sql b/Day36-40/code/SRS_create_and_init.sql index 17ad397..301c7bc 100644 --- a/Day36-40/code/SRS_create_and_init.sql +++ b/Day36-40/code/SRS_create_and_init.sql @@ -216,7 +216,7 @@ select stuname as 姓名, year(now())-year(stubirth) as 年龄 from tb_student w ); -- 查询选了两门以上的课程的学生姓名(子查询/分组条件/集合运算) -select stuname from tb_student where stuid=( +select stuname from tb_student where stuid in ( select stuid from tb_score group by stuid having count(stuid)>2 ) diff --git a/Day36-40/code/bank_create_and_init.sql b/Day36-40/code/bank_create_and_init.sql deleted file mode 100644 index 1495a35..0000000 --- a/Day36-40/code/bank_create_and_init.sql +++ /dev/null @@ -1,15 +0,0 @@ -drop database if exists bank; - -create database bank default charset utf8; - -use bank; - -create table tb_account -( -accid char(8) primary key, -accowner varchar(20) not null, -accbalance float not null default 0 -); - -insert into tb_account values (11223344, '王大锤', 1000); -insert into tb_account values (22334455, '李小龙', 1000); \ No newline at end of file diff --git a/Day36-40/code/booksys_create_and_init.sql b/Day36-40/code/booksys_create_and_init.sql deleted file mode 100644 index 8a99a58..0000000 --- a/Day36-40/code/booksys_create_and_init.sql +++ /dev/null @@ -1,46 +0,0 @@ -drop database if exists booksys; - -create database booksys default charset utf8; - -use booksys; - -create table tb_book -( -bookid integer not null, -isbn char(13) not null, -bname varchar(100) not null, -price decimal(8,2) not null, -author varchar(100) not null, -publisher varchar(50) not null, -pubdate date, -intro varchar(500), -lended bit default 0, -counter integer default 0, -primary key (bookid) -); - -create table tb_reader -( -readerid integer not null, -rname varchar(20) not null, -gender bit not null, -tel char(11) not null, -birth date, -regdate date not null, -available bit default 1, -primary key (readerid) -); - -create table tb_record -( -recordid integer not null auto_increment, -bid integer not null, -rid integer not null, -lenddate datetime not null, -backdate datetime, -pulishment decimal(6,2), -primary key (recordid) -); - -alter table tb_record add constraint fk_record_bid foreign key (bid) references tb_book (bookid) on update cascade; -alter table tb_record add constraint fk_record_rid foreign key (rid) references tb_reader (readerid) on update cascade; \ No newline at end of file diff --git a/Day36-40/code/demo_create_and_init.sql b/Day36-40/code/demo.sql similarity index 100% rename from Day36-40/code/demo_create_and_init.sql rename to Day36-40/code/demo.sql diff --git a/Day36-40/code/人力资源管理.spf b/Day36-40/code/人力资源管理.spf deleted file mode 100644 index 9042725..0000000 --- a/Day36-40/code/人力资源管理.spf +++ /dev/null @@ -1,877 +0,0 @@ - - - - - ContentFilters - - auto_connect - - data - - connection - - database - hrs - host - 120.77.222.217 - kcid - 6157604644212181126 - name - MySQL@Aliyun - rdbms_type - mysql - sslCACertFileLocation - - sslCACertFileLocationEnabled - 0 - sslCertificateFileLocation - - sslCertificateFileLocationEnabled - 0 - sslKeyFileLocation - - sslKeyFileLocationEnabled - 0 - type - SPTCPIPConnection - useSSL - 0 - user - root - - session - - connectionEncoding - utf8 - contentPageNumber - 1 - contentSelection - - YnBsaXN0MDDUAQIDBAUGOTpYJHZlcnNpb25YJG9iamVjdHNZJGFy - Y2hpdmVyVCR0b3ASAAGGoK0HCBUWFxgZHSQoLDE2VSRudWxs0wkK - CwwQFFdOUy5rZXlzWk5TLm9iamVjdHNWJGNsYXNzow0OD4ACgAOA - BKMREhOABYAGgAiADFR0eXBlVHJvd3NUa2V5c18QJlNlbGVjdGlv - bkRldGFpbFR5cGVQcmltYXJ5S2V5ZWREZXRhaWxz0wkKCxobHKCg - gAfSHh8gIVokY2xhc3NuYW1lWCRjbGFzc2VzXxATTlNNdXRhYmxl - RGljdGlvbmFyeaMgIiNcTlNEaWN0aW9uYXJ5WE5TT2JqZWN00goL - JSehJoAJgAvSCykqK1lOUy5zdHJpbmeAClNlbm/SHh8tLl8QD05T - TXV0YWJsZVN0cmluZ6MtLzBYTlNTdHJpbmdYTlNPYmplY3TSHh8y - M15OU011dGFibGVBcnJheaMyNDVXTlNBcnJheVhOU09iamVjdNIe - HyI3oiI4WE5TT2JqZWN0XxAPTlNLZXllZEFyY2hpdmVy0Ts8VGRh - dGGAAQAIABEAGgAjAC0AMgA3AEUASwBSAFoAZQBsAHAAcgB0AHYA - egB8AH4AgACCAIcAjACRALoAwQDCAMMAxQDKANUA3gD0APgBBQEO - ARMBFQEXARkBHgEoASoBLgEzAUUBSQFSAVsBYAFvAXMBewGEAYkB - jAGVAacBqgGvAAAAAAAAAgEAAAAAAAAAPQAAAAAAAAAAAAAAAAAA - AbE= - - contentSortColIsAsc - - contentViewport - {{0, 0}, {694, 448}} - isToolbarVisible - - queries - -- 注意事项: --- 1. 给数据库和表命名的时候尽量使用全小写 --- 2. 作为筛选条件的字符串是否区分大小看设置的校对规则 -drop database if exists hrs; -create database hrs default charset utf8 collate utf8_general_ci; - -use hrs; - -drop table if exists tb_emp; -drop table if exists tb_dept; - --- 3. 数据库中的对象通常会用前缀加以区分 --- table / view / index / function / procedure / trigger -create table tb_dept -( -dno int not null comment '编号', -dname varchar(10) not null comment '名称', -dloc varchar(20) not null comment '所在地', -primary key (dno) -); - --- 批量插入操作 -insert into tb_dept values - (10, '会计部', '北京'), - (20, '研发部', '成都'), - (30, '销售部', '重庆'), - (40, '运维部', '深圳'); - -create table tb_emp -( -eno int not null comment '员工编号', -ename varchar(20) not null comment '员工姓名', -job varchar(20) not null comment '员工职位', -mgr int comment '主管编号', -sal int not null comment '员工月薪', -comm int comment '每月补贴', -dno int comment '所在部门编号', -primary key (eno) -); - --- 修改表添加一个列到mgr列的后面 --- alter table tb_emp add column hiredate date after mgr; - --- 修改表添加一个自参照的外键约束 --- alter table tb_emp add constraint fk_emp_mgr foreign key (mgr) references tb_emp (eno); - --- alter table tb_emp drop foreign key fk_emp_mgr; - --- alter table tb_emp drop foreign key fk_emp_dno; - --- restrict - 不允许操作 --- cascade - 级联操作 --- set null - 设置为null -alter table tb_emp add constraint fk_emp_dno foreign key (dno) references tb_dept (dno); --- on delete restrict on update cascade; - -insert into tb_emp values - (7800, '张三丰', '总裁', null, 9000, 1200, 20), - (2056, '乔峰', '分析师', 7800, 5000, 1500, 20), - (3088, '李莫愁', '设计师', 2056, 3500, 800, 20), - (3211, '张无忌', '程序员', 2056, 3200, null, 20), - (3233, '丘处机', '程序员', 2056, 3400, null, 20), - (3251, '张翠山', '程序员', 2056, 4000, null, 20), - (5566, '宋远桥', '会计师', 7800, 4000, 1000, 10), - (5234, '郭靖', '出纳', 5566, 2000, null, 10), - (3344, '黄蓉', '销售主管', 7800, 3000, 800, 30), - (1359, '胡一刀', '销售员', 3344, 1800, 200, 30), - (4466, '苗人凤', '销售员', 3344, 2500, null, 30), - (3244, '欧阳锋', '程序员', 3088, 3200, null, 20), - (3577, '杨过', '会计', 5566, 2200, null, 10), - (3588, '朱九真', '会计', 5566, 2500, null, 10); - --- 查询月薪最高的员工姓名和工资 - 子查询/嵌套查询+聚合函数 --- select ename, sal from tb_emp order by sal desc limit 1; -select ename, sal from tb_emp where sal=( - select max(sal) from tb_emp -); - --- 查询员工的姓名和年薪((月薪+补贴)*12) -select ename, (sal+ifnull(comm, 0))*12 as 年薪 from tb_emp order by 年薪 desc; - --- 查询有员工的部门的编号和人数 - 分组查询+聚合函数 -select dno, count(dno) from tb_emp group by dno with rollup; - --- 查询所有部门的名称和人数 - 子查询+连接查询(左外) -select dname as 部门名称, ifnull(total, 0) as 人数 from tb_dept t1 left join (select dno, count(dno) as total from tb_emp group by dno) t2 on t1.dno=t2.dno; - --- 查询月薪最高的员工(Boss除外)的姓名和工资 - 空值判断 -select ename, sal from tb_emp where sal=(select max(sal) from tb_emp where mgr is not null); - --- 查询月薪超过平均薪水的员工的姓名和工资 -select ename, sal from tb_emp where sal>(select avg(sal) as avgsal from tb_emp); - --- 查询月薪超过其所在部门平均薪水的员工的姓名、部门编号和工资 -select ename, t1.dno, sal, round(avgsal, 2) from tb_emp t1 inner join (select dno, avg(sal) as avgsal from tb_emp group by dno) t2 on t1.dno=t2.dno where sal>avgsal; - --- 查询部门中薪水最高的人姓名、工资和所在部门名称 -select ename, sal, dname from (select ename, sal, t1.dno from tb_emp t1 inner join (select dno, max(sal) as maxsal from tb_emp group by dno) t2 on t1.dno=t2.dno where sal=maxsal) t3 inner join tb_dept t4 on t3.dno=t4.dno; - --- 查询主管的姓名和职位 -select ename, job from tb_emp where eno in (select distinct mgr from tb_emp where mgr is not null); - --- 通常不推荐使用in或者not in集合运算和distinct去重操作 --- 可以考虑用exists或not exists替代掉集合运算和去重操作 -select ename, job from tb_emp t1 where exists (select 'x' from tb_emp t2 where t1.eno=t2.mgr); - --- 查询月薪排名4~6名的员工姓名和工资 -select ename, sal from tb_emp order by sal desc limit 3, 3; -select ename, sal from tb_emp order by sal desc limit 3 offset 3; - --- explain生成执行计划 -explain select eno, ename from tb_emp where eno=7800; -explain select eno, ename from tb_emp where eno<>7900; -explain select eno, ename from tb_emp where ename='张三丰'; -explain select eno, ename from tb_emp where ename like '张%'; -explain select eno, ename from tb_emp where ename like '%张'; -explain select eno, ename from tb_emp where ename<>'张三丰'; - --- 视图:查询的快照(简化查询操作) --- 通过视图可以将用户的访问权限限制到某些指定的列上 -create view vw_emp_dept as -select eno, ename, dname from tb_emp t1 inner join tb_dept t2 on t1.dno=t2.dno; - -select ename, dname from vw_emp_dept; - -drop view vw_emp_dept; - --- 索引(index) --- 索引可以加速查询所以应该在经常用于查询筛选条件的列上建立索引 --- 索引会使用额外的存储空间而且会让增删改变得更慢(因为要更新索引) --- 所以不能够滥用索引 -create index idx_emp_ename on tb_emp (ename); -drop index idx_emp_ename on tb_emp; - --- (存储)过程/函数:把一系列的SQL可以封装到一个过程中,而且可以加上分支和循环,将来通过过程的名字直接调用过程即可,因为创建过程时已经提前编译了SQL语句,所以比直接执行SQL语句性能更好 - --- 重新定义定界符为$$ -delimiter $$ - --- 创建存储过程 -create procedure sp_dept_avg_sal(deptno int, out avgsal float) -begin - select avg(sal) into avgsal from tb_emp where dno=deptno; -end$$ - --- 将定界符还原回; -delimiter ; - --- 调用存储过程 -call sp_dept_avg_sal(20, @a); - --- 通过输出参数取出部门平均工资 -select @a; - --- 删除存储过程 -drop procedure sp_dept_avg_sal; - --- 触发器:在执行增删改操作时可以触发其他的级联操作,但是有可能导致“锁表”现象,实际开发中应该尽量避免使用触发器 --- update tb_dept set dno=11 where dno=10; --- delete from tb_dept where dno=11; - -delimiter $$ - -create trigger tr_dept_update -after update on tb_dept for each row -begin - update tb_emp set dno=new.dno where dno=old.dno; -end$$ - -delimiter ; - -drop trigger tr_dept_update; - --- DCL:授予权限(grant to)和召回权限(revoke from) -create user 'hellokitty'@'%' identified by '123123'; - -grant all privileges on hrs.* to 'hellokitty'@'%'; - -revoke insert, delete, update on hrs.* from 'hellokitty'@'%'; - -drop user 'hellokitty'@'%'; - --- 事务(transaction)- 把多个增删改的操作做成不可分割的原子性操作 --- 要么全部都做,要么全都不做 - --- start transaction; -begin; - -delete from tb_emp; - - - - table - tb_emp - view - SP_VIEW_CUSTOMQUERY - windowVerticalDividerPosition - 163 - - - encrypted - - format - connection - queryFavorites - - queryHistory - - rollback - begin; -delete from tb_emp - -- 注意事项: --- 1. 给数据库和表命名的时候尽量使用全小写 --- 2. 作为筛选条件的字符串是否区分大小看设置的校对规则 -drop database if exists hrs; -create database hrs default charset utf8 collate utf8_general_ci; -use hrs; -drop table if exists tb_emp; -drop table if exists tb_dept; --- 3. 数据库中的对象通常会用前缀加以区分 --- table / view / index / function / procedure / trigger -create table tb_dept -( -dno int not null comment '编号', -dname varchar(10) not null comment '名称', -dloc varchar(20) not null comment '所在地', -primary key (dno) -); --- 批量插入操作 -insert into tb_dept values - (10, '会计部', '北京'), - (20, '研发部', '成都'), - (30, '销售部', '重庆'), - (40, '运维部', '深圳'); -create table tb_emp -( -eno int not null comment '员工编号', -ename varchar(20) not null comment '员工姓名', -job varchar(20) not null comment '员工职位', -mgr int comment '主管编号', -sal int not null comment '员工月薪', -comm int comment '每月补贴', -dno int comment '所在部门编号', -primary key (eno) -); --- 修改表添加一个列到mgr列的后面 --- alter table tb_emp add column hiredate date after mgr; - --- 修改表添加一个自参照的外键约束 --- alter table tb_emp add constraint fk_emp_mgr foreign key (mgr) references tb_emp (eno); - --- alter table tb_emp drop foreign key fk_emp_mgr; - --- alter table tb_emp drop foreign key fk_emp_dno; - --- restrict - 不允许操作 --- cascade - 级联操作 --- set null - 设置为null -alter table tb_emp add constraint fk_emp_dno foreign key (dno) references tb_dept (dno); --- on delete restrict on update cascade; - -insert into tb_emp values - (7800, '张三丰', '总裁', null, 9000, 1200, 20), - (2056, '乔峰', '分析师', 7800, 5000, 1500, 20), - (3088, '李莫愁', '设计师', 2056, 3500, 800, 20), - (3211, '张无忌', '程序员', 2056, 3200, null, 20), - (3233, '丘处机', '程序员', 2056, 3400, null, 20), - (3251, '张翠山', '程序员', 2056, 4000, null, 20), - (5566, '宋远桥', '会计师', 7800, 4000, 1000, 10), - (5234, '郭靖', '出纳', 5566, 2000, null, 10), - (3344, '黄蓉', '销售主管', 7800, 3000, 800, 30), - (1359, '胡一刀', '销售员', 3344, 1800, 200, 30), - (4466, '苗人凤', '销售员', 3344, 2500, null, 30), - (3244, '欧阳锋', '程序员', 3088, 3200, null, 20), - (3577, '杨过', '会计', 5566, 2200, null, 10), - (3588, '朱九真', '会计', 5566, 2500, null, 10); --- 查询月薪最高的员工姓名和工资 - 子查询/嵌套查询+聚合函数 --- select ename, sal from tb_emp order by sal desc limit 1; -select ename, sal from tb_emp where sal=( - select max(sal) from tb_emp -); --- 查询员工的姓名和年薪((月薪+补贴)*12) -select ename, (sal+ifnull(comm, 0))*12 as 年薪 from tb_emp order by 年薪 desc; --- 查询有员工的部门的编号和人数 - 分组查询+聚合函数 -select dno, count(dno) from tb_emp group by dno with rollup; --- 查询所有部门的名称和人数 - 子查询+连接查询(左外) -select dname as 部门名称, ifnull(total, 0) as 人数 from tb_dept t1 left join (select dno, count(dno) as total from tb_emp group by dno) t2 on t1.dno=t2.dno; --- 查询月薪最高的员工(Boss除外)的姓名和工资 - 空值判断 -select ename, sal from tb_emp where sal=(select max(sal) from tb_emp where mgr is not null); --- 查询月薪超过平均薪水的员工的姓名和工资 -select ename, sal from tb_emp where sal>(select avg(sal) as avgsal from tb_emp); --- 查询月薪超过其所在部门平均薪水的员工的姓名、部门编号和工资 -select ename, t1.dno, sal, round(avgsal, 2) from tb_emp t1 inner join (select dno, avg(sal) as avgsal from tb_emp group by dno) t2 on t1.dno=t2.dno where sal>avgsal; --- 查询部门中薪水最高的人姓名、工资和所在部门名称 -select ename, sal, dname from (select ename, sal, t1.dno from tb_emp t1 inner join (select dno, max(sal) as maxsal from tb_emp group by dno) t2 on t1.dno=t2.dno where sal=maxsal) t3 inner join tb_dept t4 on t3.dno=t4.dno; --- 查询主管的姓名和职位 -select ename, job from tb_emp where eno in (select distinct mgr from tb_emp where mgr is not null); --- 通常不推荐使用in或者not in集合运算和distinct去重操作 --- 可以考虑用exists或not exists替代掉集合运算和去重操作 -select ename, job from tb_emp t1 where exists (select 'x' from tb_emp t2 where t1.eno=t2.mgr); --- 查询月薪排名4~6名的员工姓名和工资 -select ename, sal from tb_emp order by sal desc limit 3, 3; -select ename, sal from tb_emp order by sal desc limit 3 offset 3; --- explain生成执行计划 -explain select eno, ename from tb_emp where eno=7800; -explain select eno, ename from tb_emp where eno<>7900; -explain select eno, ename from tb_emp where ename='张三丰'; -explain select eno, ename from tb_emp where ename like '张%'; -explain select eno, ename from tb_emp where ename like '%张'; -explain select eno, ename from tb_emp where ename<>'张三丰'; --- 视图:查询的快照(简化查询操作) --- 通过视图可以将用户的访问权限限制到某些指定的列上 -create view vw_emp_dept as -select eno, ename, dname from tb_emp t1 inner join tb_dept t2 on t1.dno=t2.dno; -select ename, dname from vw_emp_dept; -drop view vw_emp_dept; --- 索引(index) --- 索引可以加速查询所以应该在经常用于查询筛选条件的列上建立索引 --- 索引会使用额外的存储空间而且会让增删改变得更慢(因为要更新索引) --- 所以不能够滥用索引 -create index idx_emp_ename on tb_emp (ename); -drop index idx_emp_ename on tb_emp; --- 创建存储过程 -create procedure sp_dept_avg_sal(deptno int, out avgsal float) -begin - select avg(sal) into avgsal from tb_emp where dno=deptno; -end; --- 调用存储过程 -call sp_dept_avg_sal(20, @a); --- 通过输出参数取出部门平均工资 -select @a; --- 删除存储过程 -drop procedure sp_dept_avg_sal; -create trigger tr_dept_update -after update on tb_dept for each row -begin - update tb_emp set dno=new.dno where dno=old.dno; -end; -drop trigger tr_dept_update; --- DCL:授予权限(grant to)和召回权限(revoke from) -create user 'hellokitty'@'%' identified by '123123'; -grant all privileges on hrs.* to 'hellokitty'@'%'; -revoke insert, delete, update on hrs.* from 'hellokitty'@'%'; -drop user 'hellokitty'@'%'; --- 事务(transaction)- 把多个增删改的操作做成不可分割的原子性操作 --- 要么全部都做,要么全都不做 - --- start transaction; --- begin; - --- delete from tb_emp; - --- commit; --- rollback; - -- 事务(transaction)- 把多个增删改的操作做成不可分割的原子性操作 --- 要么全部都做,要么全都不做 - --- start transaction; --- begin; - --- delete from tb_emp; - --- commit; --- rollback; - delete from tb_emp - alter table tb_emp drop foreign key fk_emp_mgr - begin - -- 注意事项: --- 1. 给数据库和表命名的时候尽量使用全小写 --- 2. 作为筛选条件的字符串是否区分大小看设置的校对规则 -drop database if exists hrs; -create database hrs default charset utf8 collate utf8_general_ci; -use hrs; -drop table if exists tb_emp; -drop table if exists tb_dept; --- 3. 数据库中的对象通常会用前缀加以区分 --- table / view / index / function / procedure / trigger -create table tb_dept -( -dno int not null comment '编号', -dname varchar(10) not null comment '名称', -dloc varchar(20) not null comment '所在地', -primary key (dno) -); --- 批量插入操作 -insert into tb_dept values - (10, '会计部', '北京'), - (20, '研发部', '成都'), - (30, '销售部', '重庆'), - (40, '运维部', '深圳'); -create table tb_emp -( -eno int not null comment '员工编号', -ename varchar(20) not null comment '员工姓名', -job varchar(20) not null comment '员工职位', -mgr int comment '主管编号', -sal int not null comment '员工月薪', -comm int comment '每月补贴', -dno int comment '所在部门编号', -primary key (eno) -); --- 修改表添加一个列到mgr列的后面 --- alter table tb_emp add column hiredate date after mgr; - --- 修改表添加一个自参照的外键约束 -alter table tb_emp add constraint fk_emp_mgr foreign key (mgr) references tb_emp (eno); --- alter table tb_emp drop foreign key fk_emp_dno; - --- restrict - 不允许操作 --- cascade - 级联操作 --- set null - 设置为null -alter table tb_emp add constraint fk_emp_dno foreign key (dno) references tb_dept (dno); --- on delete restrict on update cascade; - -insert into tb_emp values - (7800, '张三丰', '总裁', null, 9000, 1200, 20), - (2056, '乔峰', '分析师', 7800, 5000, 1500, 20), - (3088, '李莫愁', '设计师', 2056, 3500, 800, 20), - (3211, '张无忌', '程序员', 2056, 3200, null, 20), - (3233, '丘处机', '程序员', 2056, 3400, null, 20), - (3251, '张翠山', '程序员', 2056, 4000, null, 20), - (5566, '宋远桥', '会计师', 7800, 4000, 1000, 10), - (5234, '郭靖', '出纳', 5566, 2000, null, 10), - (3344, '黄蓉', '销售主管', 7800, 3000, 800, 30), - (1359, '胡一刀', '销售员', 3344, 1800, 200, 30), - (4466, '苗人凤', '销售员', 3344, 2500, null, 30), - (3244, '欧阳锋', '程序员', 3088, 3200, null, 20), - (3577, '杨过', '会计', 5566, 2200, null, 10), - (3588, '朱九真', '会计', 5566, 2500, null, 10); --- 查询月薪最高的员工姓名和工资 - 子查询/嵌套查询+聚合函数 --- select ename, sal from tb_emp order by sal desc limit 1; -select ename, sal from tb_emp where sal=( - select max(sal) from tb_emp -); --- 查询员工的姓名和年薪((月薪+补贴)*12) -select ename, (sal+ifnull(comm, 0))*12 as 年薪 from tb_emp order by 年薪 desc; --- 查询有员工的部门的编号和人数 - 分组查询+聚合函数 -select dno, count(dno) from tb_emp group by dno with rollup; --- 查询所有部门的名称和人数 - 子查询+连接查询(左外) -select dname as 部门名称, ifnull(total, 0) as 人数 from tb_dept t1 left join (select dno, count(dno) as total from tb_emp group by dno) t2 on t1.dno=t2.dno; --- 查询月薪最高的员工(Boss除外)的姓名和工资 - 空值判断 -select ename, sal from tb_emp where sal=(select max(sal) from tb_emp where mgr is not null); --- 查询月薪超过平均薪水的员工的姓名和工资 -select ename, sal from tb_emp where sal>(select avg(sal) as avgsal from tb_emp); --- 查询月薪超过其所在部门平均薪水的员工的姓名、部门编号和工资 -select ename, t1.dno, sal, round(avgsal, 2) from tb_emp t1 inner join (select dno, avg(sal) as avgsal from tb_emp group by dno) t2 on t1.dno=t2.dno where sal>avgsal; --- 查询部门中薪水最高的人姓名、工资和所在部门名称 -select ename, sal, dname from (select ename, sal, t1.dno from tb_emp t1 inner join (select dno, max(sal) as maxsal from tb_emp group by dno) t2 on t1.dno=t2.dno where sal=maxsal) t3 inner join tb_dept t4 on t3.dno=t4.dno; --- 查询主管的姓名和职位 -select ename, job from tb_emp where eno in (select distinct mgr from tb_emp where mgr is not null); --- 通常不推荐使用in或者not in集合运算和distinct去重操作 --- 可以考虑用exists或not exists替代掉集合运算和去重操作 -select ename, job from tb_emp t1 where exists (select 'x' from tb_emp t2 where t1.eno=t2.mgr); --- 查询月薪排名4~6名的员工姓名和工资 -select ename, sal from tb_emp order by sal desc limit 3, 3; -select ename, sal from tb_emp order by sal desc limit 3 offset 3; --- explain生成执行计划 -explain select eno, ename from tb_emp where eno=7800; -explain select eno, ename from tb_emp where eno<>7900; -explain select eno, ename from tb_emp where ename='张三丰'; -explain select eno, ename from tb_emp where ename like '张%'; -explain select eno, ename from tb_emp where ename like '%张'; -explain select eno, ename from tb_emp where ename<>'张三丰'; --- 视图:查询的快照(简化查询操作) --- 通过视图可以将用户的访问权限限制到某些指定的列上 -create view vw_emp_dept as -select eno, ename, dname from tb_emp t1 inner join tb_dept t2 on t1.dno=t2.dno; -select ename, dname from vw_emp_dept; -drop view vw_emp_dept; --- 索引(index) --- 索引可以加速查询所以应该在经常用于查询筛选条件的列上建立索引 --- 索引会使用额外的存储空间而且会让增删改变得更慢(因为要更新索引) --- 所以不能够滥用索引 -create index idx_emp_ename on tb_emp (ename); -drop index idx_emp_ename on tb_emp; --- 创建存储过程 -create procedure sp_dept_avg_sal(deptno int, out avgsal float) -begin - select avg(sal) into avgsal from tb_emp where dno=deptno; -end; --- 调用存储过程 -call sp_dept_avg_sal(20, @a); --- 通过输出参数取出部门平均工资 -select @a; --- 删除存储过程 -drop procedure sp_dept_avg_sal; -create trigger tr_dept_update -after update on tb_dept for each row -begin - update tb_emp set dno=new.dno where dno=old.dno; -end; -drop trigger tr_dept_update; --- DCL:授予权限(grant to)和召回权限(revoke from) -create user 'hellokitty'@'%' identified by '123123'; -grant all privileges on hrs.* to 'hellokitty'@'%'; -revoke insert, delete, update on hrs.* from 'hellokitty'@'%'; -drop user 'hellokitty'@'%' - drop user 'hellokitty'@'%' - -- 注意事项: --- 1. 给数据库和表命名的时候尽量使用全小写 --- 2. 作为筛选条件的字符串是否区分大小看设置的校对规则 -drop database if exists hrs; -create database hrs default charset utf8 collate utf8_general_ci; -use hrs; -drop table if exists tb_emp; -drop table if exists tb_dept; --- 3. 数据库中的对象通常会用前缀加以区分 --- table / view / index / function / procedure / trigger -create table tb_dept -( -dno int not null comment '编号', -dname varchar(10) not null comment '名称', -dloc varchar(20) not null comment '所在地', -primary key (dno) -); --- 批量插入操作 -insert into tb_dept values - (10, '会计部', '北京'), - (20, '研发部', '成都'), - (30, '销售部', '重庆'), - (40, '运维部', '深圳'); -create table tb_emp -( -eno int not null comment '员工编号', -ename varchar(20) not null comment '员工姓名', -job varchar(20) not null comment '员工职位', -mgr int comment '主管编号', -sal int not null comment '员工月薪', -comm int comment '每月补贴', -dno int comment '所在部门编号', -primary key (eno) -); --- 修改表添加一个列到mgr列的后面 --- alter table tb_emp add column hiredate date after mgr; - --- 修改表添加一个自参照的外键约束 -alter table tb_emp add constraint fk_emp_mgr foreign key (mgr) references tb_emp (eno); --- alter table tb_emp drop foreign key fk_emp_dno; - --- restrict - 不允许操作 --- cascade - 级联操作 --- set null - 设置为null -alter table tb_emp add constraint fk_emp_dno foreign key (dno) references tb_dept (dno); --- on delete restrict on update cascade; - -insert into tb_emp values - (7800, '张三丰', '总裁', null, 9000, 1200, 20), - (2056, '乔峰', '分析师', 7800, 5000, 1500, 20), - (3088, '李莫愁', '设计师', 2056, 3500, 800, 20), - (3211, '张无忌', '程序员', 2056, 3200, null, 20), - (3233, '丘处机', '程序员', 2056, 3400, null, 20), - (3251, '张翠山', '程序员', 2056, 4000, null, 20), - (5566, '宋远桥', '会计师', 7800, 4000, 1000, 10), - (5234, '郭靖', '出纳', 5566, 2000, null, 10), - (3344, '黄蓉', '销售主管', 7800, 3000, 800, 30), - (1359, '胡一刀', '销售员', 3344, 1800, 200, 30), - (4466, '苗人凤', '销售员', 3344, 2500, null, 30), - (3244, '欧阳锋', '程序员', 3088, 3200, null, 20), - (3577, '杨过', '会计', 5566, 2200, null, 10), - (3588, '朱九真', '会计', 5566, 2500, null, 10); --- 查询月薪最高的员工姓名和工资 - 子查询/嵌套查询+聚合函数 --- select ename, sal from tb_emp order by sal desc limit 1; -select ename, sal from tb_emp where sal=( - select max(sal) from tb_emp -); --- 查询员工的姓名和年薪((月薪+补贴)*12) -select ename, (sal+ifnull(comm, 0))*12 as 年薪 from tb_emp order by 年薪 desc; --- 查询有员工的部门的编号和人数 - 分组查询+聚合函数 -select dno, count(dno) from tb_emp group by dno with rollup; --- 查询所有部门的名称和人数 - 子查询+连接查询(左外) -select dname as 部门名称, ifnull(total, 0) as 人数 from tb_dept t1 left join (select dno, count(dno) as total from tb_emp group by dno) t2 on t1.dno=t2.dno; --- 查询月薪最高的员工(Boss除外)的姓名和工资 - 空值判断 -select ename, sal from tb_emp where sal=(select max(sal) from tb_emp where mgr is not null); --- 查询月薪超过平均薪水的员工的姓名和工资 -select ename, sal from tb_emp where sal>(select avg(sal) as avgsal from tb_emp); --- 查询月薪超过其所在部门平均薪水的员工的姓名、部门编号和工资 -select ename, t1.dno, sal, round(avgsal, 2) from tb_emp t1 inner join (select dno, avg(sal) as avgsal from tb_emp group by dno) t2 on t1.dno=t2.dno where sal>avgsal; --- 查询部门中薪水最高的人姓名、工资和所在部门名称 -select ename, sal, dname from (select ename, sal, t1.dno from tb_emp t1 inner join (select dno, max(sal) as maxsal from tb_emp group by dno) t2 on t1.dno=t2.dno where sal=maxsal) t3 inner join tb_dept t4 on t3.dno=t4.dno; --- 查询主管的姓名和职位 -select ename, job from tb_emp where eno in (select distinct mgr from tb_emp where mgr is not null); --- 通常不推荐使用in或者not in集合运算和distinct去重操作 --- 可以考虑用exists或not exists替代掉集合运算和去重操作 -select ename, job from tb_emp t1 where exists (select 'x' from tb_emp t2 where t1.eno=t2.mgr); --- 查询月薪排名4~6名的员工姓名和工资 -select ename, sal from tb_emp order by sal desc limit 3, 3; -select ename, sal from tb_emp order by sal desc limit 3 offset 3; --- explain生成执行计划 -explain select eno, ename from tb_emp where eno=7800; -explain select eno, ename from tb_emp where eno<>7900; -explain select eno, ename from tb_emp where ename='张三丰'; -explain select eno, ename from tb_emp where ename like '张%'; -explain select eno, ename from tb_emp where ename like '%张'; -explain select eno, ename from tb_emp where ename<>'张三丰'; --- 视图:查询的快照(简化查询操作) --- 通过视图可以将用户的访问权限限制到某些指定的列上 -create view vw_emp_dept as -select eno, ename, dname from tb_emp t1 inner join tb_dept t2 on t1.dno=t2.dno; -select ename, dname from vw_emp_dept; -drop view vw_emp_dept; --- 索引(index) --- 索引可以加速查询所以应该在经常用于查询筛选条件的列上建立索引 --- 索引会使用额外的存储空间而且会让增删改变得更慢(因为要更新索引) --- 所以不能够滥用索引 -create index idx_emp_ename on tb_emp (ename); -drop index idx_emp_ename on tb_emp; --- 创建存储过程 -create procedure sp_dept_avg_sal(deptno int, out avgsal float) -begin - select avg(sal) into avgsal from tb_emp where dno=deptno; -end; --- 调用存储过程 -call sp_dept_avg_sal(20, @a); --- 通过输出参数取出部门平均工资 -select @a; --- 删除存储过程 -drop procedure sp_dept_avg_sal; -create trigger tr_dept_update -after update on tb_dept for each row -begin - update tb_emp set dno=new.dno where dno=old.dno; -end; -drop trigger tr_dept_update; --- DCL:授予权限(grant to)和召回权限(revoke from) -create user 'hellokitty'@'%' identified by '123123' - -- 注意事项: --- 1. 给数据库和表命名的时候尽量使用全小写 --- 2. 作为筛选条件的字符串是否区分大小看设置的校对规则 -drop database if exists hrs; -create database hrs default charset utf8 collate utf8_general_ci; -use hrs; -drop table if exists tb_emp; -drop table if exists tb_dept; --- 3. 数据库中的对象通常会用前缀加以区分 --- table / view / index / function / procedure / trigger -create table tb_dept -( -dno int not null comment '编号', -dname varchar(10) not null comment '名称', -dloc varchar(20) not null comment '所在地', -primary key (dno) -); --- 批量插入操作 -insert into tb_dept values - (10, '会计部', '北京'), - (20, '研发部', '成都'), - (30, '销售部', '重庆'), - (40, '运维部', '深圳'); -create table tb_emp -( -eno int not null comment '员工编号', -ename varchar(20) not null comment '员工姓名', -job varchar(20) not null comment '员工职位', -mgr int comment '主管编号', -sal int not null comment '员工月薪', -comm int comment '每月补贴', -dno int comment '所在部门编号', -primary key (eno) -); --- 修改表添加一个列到mgr列的后面 --- alter table tb_emp add column hiredate date after mgr; - --- 修改表添加一个自参照的外键约束 -alter table tb_emp add constraint fk_emp_mgr foreign key (mgr) references tb_emp (eno); --- alter table tb_emp drop foreign key fk_emp_dno; - --- restrict - 不允许操作 --- cascade - 级联操作 --- set null - 设置为null -alter table tb_emp add constraint fk_emp_dno foreign key (dno) references tb_dept (dno); --- on delete restrict on update cascade; - -insert into tb_emp values - (7800, '张三丰', '总裁', null, 9000, 1200, 20), - (2056, '乔峰', '分析师', 7800, 5000, 1500, 20), - (3088, '李莫愁', '设计师', 2056, 3500, 800, 20), - (3211, '张无忌', '程序员', 2056, 3200, null, 20), - (3233, '丘处机', '程序员', 2056, 3400, null, 20), - (3251, '张翠山', '程序员', 2056, 4000, null, 20), - (5566, '宋远桥', '会计师', 7800, 4000, 1000, 10), - (5234, '郭靖', '出纳', 5566, 2000, null, 10), - (3344, '黄蓉', '销售主管', 7800, 3000, 800, 30), - (1359, '胡一刀', '销售员', 3344, 1800, 200, 30), - (4466, '苗人凤', '销售员', 3344, 2500, null, 30), - (3244, '欧阳锋', '程序员', 3088, 3200, null, 20), - (3577, '杨过', '会计', 5566, 2200, null, 10), - (3588, '朱九真', '会计', 5566, 2500, null, 10); --- 查询月薪最高的员工姓名和工资 - 子查询/嵌套查询+聚合函数 --- select ename, sal from tb_emp order by sal desc limit 1; -select ename, sal from tb_emp where sal=( - select max(sal) from tb_emp -); --- 查询员工的姓名和年薪((月薪+补贴)*12) -select ename, (sal+ifnull(comm, 0))*12 as 年薪 from tb_emp order by 年薪 desc; --- 查询有员工的部门的编号和人数 - 分组查询+聚合函数 -select dno, count(dno) from tb_emp group by dno with rollup; --- 查询所有部门的名称和人数 - 子查询+连接查询(左外) -select dname as 部门名称, ifnull(total, 0) as 人数 from tb_dept t1 left join (select dno, count(dno) as total from tb_emp group by dno) t2 on t1.dno=t2.dno; --- 查询月薪最高的员工(Boss除外)的姓名和工资 - 空值判断 -select ename, sal from tb_emp where sal=(select max(sal) from tb_emp where mgr is not null); --- 查询月薪超过平均薪水的员工的姓名和工资 -select ename, sal from tb_emp where sal>(select avg(sal) as avgsal from tb_emp); --- 查询月薪超过其所在部门平均薪水的员工的姓名、部门编号和工资 -select ename, t1.dno, sal, round(avgsal, 2) from tb_emp t1 inner join (select dno, avg(sal) as avgsal from tb_emp group by dno) t2 on t1.dno=t2.dno where sal>avgsal; --- 查询部门中薪水最高的人姓名、工资和所在部门名称 -select ename, sal, dname from (select ename, sal, t1.dno from tb_emp t1 inner join (select dno, max(sal) as maxsal from tb_emp group by dno) t2 on t1.dno=t2.dno where sal=maxsal) t3 inner join tb_dept t4 on t3.dno=t4.dno; --- 查询主管的姓名和职位 -select ename, job from tb_emp where eno in (select distinct mgr from tb_emp where mgr is not null); --- 通常不推荐使用in或者not in集合运算和distinct去重操作 --- 可以考虑用exists或not exists替代掉集合运算和去重操作 -select ename, job from tb_emp t1 where exists (select 'x' from tb_emp t2 where t1.eno=t2.mgr); --- 查询月薪排名4~6名的员工姓名和工资 -select ename, sal from tb_emp order by sal desc limit 3, 3; -select ename, sal from tb_emp order by sal desc limit 3 offset 3; --- explain生成执行计划 -explain select eno, ename from tb_emp where eno=7800; -explain select eno, ename from tb_emp where eno<>7900; -explain select eno, ename from tb_emp where ename='张三丰'; -explain select eno, ename from tb_emp where ename like '张%'; -explain select eno, ename from tb_emp where ename like '%张'; -explain select eno, ename from tb_emp where ename<>'张三丰'; --- 视图:查询的快照(简化查询操作) --- 通过视图可以将用户的访问权限限制到某些指定的列上 -create view vw_emp_dept as -select eno, ename, dname from tb_emp t1 inner join tb_dept t2 on t1.dno=t2.dno; -select ename, dname from vw_emp_dept; -drop view vw_emp_dept; --- 索引(index) --- 索引可以加速查询所以应该在经常用于查询筛选条件的列上建立索引 --- 索引会使用额外的存储空间而且会让增删改变得更慢(因为要更新索引) --- 所以不能够滥用索引 -create index idx_emp_ename on tb_emp (ename); -drop index idx_emp_ename on tb_emp; --- 创建存储过程 -create procedure sp_dept_avg_sal(deptno int, out avgsal float) -begin - select avg(sal) into avgsal from tb_emp where dno=deptno; -end; --- 调用存储过程 -call sp_dept_avg_sal(20, @a); --- 通过输出参数取出部门平均工资 -select @a; --- 删除存储过程 -drop procedure sp_dept_avg_sal; --- 触发器:在执行增删改操作时可以触发其他的级联操作,但是有可能导致“锁表”现象,实际开发中应该尽量避免使用触发器 -update tb_dept set dno=11 where dno=10 - -- 注意事项: --- 1. 给数据库和表命名的时候尽量使用全小写 --- 2. 作为筛选条件的字符串是否区分大小看设置的校对规则 -drop database if exists hrs; -create database hrs default charset utf8 collate utf8_general_ci; -use hrs; -drop table if exists tb_emp; -drop table if exists tb_dept; --- 3. 数据库中的对象通常会用前缀加以区分 --- table / view / index / function / procedure / trigger -create table tb_dept -( -dno int not null comment '编号', -dname varchar(10) not null comment '名称', -dloc varchar(20) not null comment '所在地', -primary key (dno) -); --- 批量插入操作 -insert into tb_dept values - (10, '会计部', '北京'), - (20, '研发部', '成都'), - (30, '销售部', '重庆'), - (40, '运维部', '深圳'); -create table tb_emp -( -eno int not null comment '员工编号', -ename varchar(20) not null comment '员工姓名', -job varchar(20) not null comment '员工职位', -mgr int comment '主管编号', -sal int not null comment '员工月薪', -comm int comment '每月补贴', -dno int comment '所在部门编号', -primary key (eno) -); --- 修改表添加一个列到mgr列的后面 --- alter table tb_emp add column hiredate date after mgr; - --- 修改表添加一个自参照的外键约束 -alter table tb_emp add constraint fk_emp_mgr foreign key (mgr) references tb_emp (eno); -alter table tb_emp drop foreign key fk_emp_dno - revoke insert, delete, update on hrs.* from 'hellokitty'@'%' - grant all privileges on hrs.* to 'hellokitty'@'%' - -- DCL:授予权限(grant to)和召回权限(revoke from) -create user 'hellokitty'@'%' identified by '123123' - delete from tb_dept where dno=11 - update tb_dept set dno=11 where dno=10 - alter table tb_emp add constraint fk_emp_dno foreign key (dno) references tb_dept (dno) on delete cascade on update cascade - alter table tb_emp drop foreign key fk_emp_dno - delete from tb_dept where dno=10 - - rdbms_type - mysql - rdbms_version - 5.5.60-MariaDB - version - 1 - - diff --git a/Day36-40/code/学校选课系统.spf b/Day36-40/code/学校选课系统.spf deleted file mode 100644 index 373bb6d..0000000 --- a/Day36-40/code/学校选课系统.spf +++ /dev/null @@ -1,376 +0,0 @@ - - - - - ContentFilters - - auto_connect - - data - - connection - - database - school - host - 120.77.222.217 - kcid - 6157604644212181126 - name - MySQL@Aliyun - rdbms_type - mysql - sslCACertFileLocation - - sslCACertFileLocationEnabled - 0 - sslCertificateFileLocation - - sslCertificateFileLocationEnabled - 0 - sslKeyFileLocation - - sslKeyFileLocationEnabled - 0 - type - SPTCPIPConnection - useSSL - 0 - user - root - - session - - connectionEncoding - utf8 - contentPageNumber - 1 - contentSelection - - YnBsaXN0MDDUAQIDBAUGOTpYJHZlcnNpb25YJG9iamVjdHNZJGFy - Y2hpdmVyVCR0b3ASAAGGoK0HCBUWFxgZHSQoLDE2VSRudWxs0wkK - CwwQFFdOUy5rZXlzWk5TLm9iamVjdHNWJGNsYXNzow0OD4ACgAOA - BKMREhOABYAGgAiADFR0eXBlVHJvd3NUa2V5c18QJlNlbGVjdGlv - bkRldGFpbFR5cGVQcmltYXJ5S2V5ZWREZXRhaWxz0wkKCxobHKCg - gAfSHh8gIVokY2xhc3NuYW1lWCRjbGFzc2VzXxATTlNNdXRhYmxl - RGljdGlvbmFyeaMgIiNcTlNEaWN0aW9uYXJ5WE5TT2JqZWN00goL - JSehJoAJgAvSCykqK1lOUy5zdHJpbmeAClRzY2lk0h4fLS5fEA9O - U011dGFibGVTdHJpbmejLS8wWE5TU3RyaW5nWE5TT2JqZWN00h4f - MjNeTlNNdXRhYmxlQXJyYXmjMjQ1V05TQXJyYXlYTlNPYmplY3TS - Hh8iN6IiOFhOU09iamVjdF8QD05TS2V5ZWRBcmNoaXZlctE7PFRk - YXRhgAEACAARABoAIwAtADIANwBFAEsAUgBaAGUAbABwAHIAdAB2 - AHoAfAB+AIAAggCHAIwAkQC6AMEAwgDDAMUAygDVAN4A9AD4AQUB - DgETARUBFwEZAR4BKAEqAS8BNAFGAUoBUwFcAWEBcAF0AXwBhQGK - AY0BlgGoAasBsAAAAAAAAAIBAAAAAAAAAD0AAAAAAAAAAAAAAAAA - AAGy - - contentSortColIsAsc - - contentViewport - {{0, 0}, {694, 448}} - isToolbarVisible - - queries - -- 如果存在名为school的数据库就删除它 -drop database if exists school; - --- 创建名为school的数据库并设置默认的字符集和排序方式 -create database school default charset utf8 collate utf8_bin; - --- 切换到school数据库上下文环境 -use school; - --- 创建学院表 -create table tb_college -( -collid int not null auto_increment comment '编号', -collname varchar(50) not null comment '名称', -collmaster varchar(20) not null comment '院长', -collweb varchar(511) default '' comment '网站', -primary key (collid) -); - --- 创建学生表 -create table tb_student -( -stuid int not null comment '学号', -stuname varchar(20) not null comment '姓名', -stusex bit default 1 comment '性别', -stubirth date not null comment '出生日期', -stuaddr varchar(255) default '' comment '籍贯', -collid int not null comment '所属学院', -primary key (stuid), -foreign key (collid) references tb_college (collid) -); - --- alter table tb_student add constraint fk_student_collid foreign key (collid) references tb_college (collid); - --- 创建教师表 -create table tb_teacher -( -teaid int not null comment '工号', -teaname varchar(20) not null comment '姓名', -teatitle varchar(10) default '助教' comment '职称', -collid int not null comment '所属学院', -primary key (teaid), -foreign key (collid) references tb_college (collid) -); - --- 创建课程表 -create table tb_course -( -couid int not null comment '编号', -couname varchar(50) not null comment '名称', -coucredit int not null comment '学分', -teaid int not null comment '授课老师', -primary key (couid), -foreign key (teaid) references tb_teacher (teaid) -); - --- 创建选课记录表 -create table tb_score -( -scid int auto_increment comment '选课记录编号', -stuid int not null comment '选课学生', -couid int not null comment '所选课程', -scdate datetime comment '选课时间日期', -scmark decimal(4,1) comment '考试成绩', -primary key (scid), -foreign key (stuid) references tb_student (stuid), -foreign key (couid) references tb_course (couid) -); - --- 添加唯一性约束(一个学生选某个课程只能选一次) -alter table tb_score add constraint uni_score_stuid_couid unique (stuid, couid); - --- 插入学院数据 -insert into tb_college (collname, collmaster, collweb) values -('计算机学院', '左冷禅', 'http://www.abc.com'), -('外国语学院', '岳不群', 'http://www.xyz.com'), -('经济管理学院', '风清扬', 'http://www.foo.com'); - --- 插入学生数据 -insert into tb_student (stuid, stuname, stusex, stubirth, stuaddr, collid) values -(1001, '杨逍', 1, '1990-3-4', '四川成都', 1), -(1002, '任我行', 1, '1992-2-2', '湖南长沙', 1), -(1033, '王语嫣', 0, '1989-12-3', '四川成都', 1), -(1572, '岳不群', 1, '1993-7-19', '陕西咸阳', 1), -(1378, '纪嫣然', 0, '1995-8-12', '四川绵阳', 1), -(1954, '林平之', 1, '1994-9-20', '福建莆田', 1), -(2035, '东方不败', 1, '1988-6-30', null, 2), -(3011, '林震南', 1, '1985-12-12', '福建莆田', 3), -(3755, '项少龙', 1, '1993-1-25', null, 3), -(3923, '杨不悔', 0, '1985-4-17', '四川成都', 3); - --- 插入老师数据 -insert into tb_teacher (teaid, teaname, teatitle, collid) values -(1122, '张三丰', '教授', 1), -(1133, '宋远桥', '副教授', 1), -(1144, '杨逍', '副教授', 1), -(2255, '范遥', '副教授', 2), -(3366, '韦一笑', '讲师', 3); - --- 插入课程数据 -insert into tb_course (couid, couname, coucredit, teaid) values -(1111, 'Python程序设计', 3, 1122), -(2222, 'Web前端开发', 2, 1122), -(3333, '操作系统', 4, 1122), -(4444, '计算机网络', 2, 1133), -(5555, '编译原理', 4, 1144), -(6666, '算法和数据结构', 3, 1144), -(7777, '经贸法语', 3, 2255), -(8888, '成本会计', 2, 3366), -(9999, '审计学', 3, 3366); - --- 插入选课数据 -insert into tb_score (stuid, couid, scdate, scmark) values -(1001, 1111, '2017-09-01', 95), -(1001, 2222, '2017-09-01', 87.5), -(1001, 3333, '2017-09-01', 100), -(1001, 4444, '2018-09-03', null), -(1001, 6666, '2017-09-02', 100), -(1002, 1111, '2017-09-03', 65), -(1002, 5555, '2017-09-01', 42), -(1033, 1111, '2017-09-03', 92.5), -(1033, 4444, '2017-09-01', 78), -(1033, 5555, '2017-09-01', 82.5), -(1572, 1111, '2017-09-02', 78), -(1378, 1111, '2017-09-05', 82), -(1378, 7777, '2017-09-02', 65.5), -(2035, 7777, '2018-09-03', 88), -(2035, 9999, curdate(), null), -(3755, 1111, date(now()), null), -(3755, 8888, date(now()), null), -(3755, 9999, '2017-09-01', 92); - --- 查询所有学生信息 -select * from tb_student; - --- 查询所有课程名称及学分(投影和别名) -select couname, coucredit from tb_course; -select couname as 课程名称, coucredit as 学分 from tb_course; - -select stuname as 姓名, case stusex when 1 then '男' else '女' end as 性别 from tb_student; -select stuname as 姓名, if(stusex, '男', '女') as 性别 from tb_student; - --- 查询所有女学生的姓名和出生日期(筛选) -select stuname, stubirth from tb_student where stusex=0; - --- 查询所有80后学生的姓名、性别和出生日期(筛选) -select stuname, stusex, stubirth from tb_student where stubirth>='1980-1-1' and stubirth<='1989-12-31'; -select stuname, stusex, stubirth from tb_student where stubirth between '1980-1-1' and '1989-12-31'; - --- 查询姓"杨"的学生姓名和性别(模糊) -select stuname, stusex from tb_student where stuname like '杨%'; - --- 查询姓"杨"名字两个字的学生姓名和性别(模糊) -select stuname, stusex from tb_student where stuname like '杨_'; - --- 查询姓"杨"名字三个字的学生姓名和性别(模糊) -select stuname, stusex from tb_student where stuname like '杨__'; - --- 查询名字中有"不"字或"嫣"字的学生的姓名(模糊) -select stuname, stusex from tb_student where stuname like '%不%' or stuname like '%嫣%'; - --- 查询没有录入家庭住址的学生姓名(空值) -select stuname from tb_student where stuaddr is null; - --- 查询录入了家庭住址的学生姓名(空值) -select stuname from tb_student where stuaddr is not null; - --- 查询学生选课的所有日期(去重) -select distinct scdate from tb_score; - --- 查询学生的家庭住址(去重) -select distinct stuaddr from tb_student where stuaddr is not null; - --- 查询男学生的姓名和生日按年龄从大到小排列(排序) --- asc - ascending - 升序(从小到大) --- desc - descending - 降序(从大到小) -select stuname as 姓名, year(now())-year(stubirth) as 年龄 from tb_student where stusex=1 order by 年龄 desc; - --- 聚合函数:max / min / count / sum / avg --- 查询年龄最大的学生的出生日期(聚合函数) -select min(stubirth) from tb_student; - --- 查询年龄最小的学生的出生日期(聚合函数) -select max(stubirth) from tb_student; - --- 查询男女学生的人数(分组和聚合函数) -select count(stuid) from tb_student; -select stusex, count(*) from tb_student group by stusex; -select stusex, min(stubirth) from tb_student group by stusex; - --- 查询课程编号为1111的课程的平均成绩(筛选和聚合函数) -select avg(scmark) from tb_score where couid=1111; -select min(scmark) from tb_score where couid=1111; -select count(scid) from tb_score where couid=1111; -select count(scmark) from tb_score where couid=1111; - --- 查询学号为1001的学生所有课程的平均分(筛选和聚合函数) -select avg(scmark) from tb_score where stuid=1001; - --- 查询每个学生的学号和平均成绩(分组和聚合函数) -select stuid as 学号, avg(scmark) as 平均分 from tb_score group by stuid; - --- 查询平均成绩大于等于90分的学生的学号和平均成绩 --- 分组以前的筛选使用where子句 --- 分组以后的筛选使用having子句 -select stuid as 学号, avg(scmark) as 平均分 from tb_score group by stuid having 平均分>=90; - --- 查询年龄最大的学生的姓名(子查询/嵌套的查询) -select stuname from tb_student where stubirth=( - select min(stubirth) from tb_student -); - --- 查询年龄最大的学生姓名和年龄(子查询+运算) -select stuname as 姓名, year(now())-year(stubirth) as 年龄 from tb_student where stubirth=( - select min(stubirth) from tb_student -); - --- 查询选了两门以上的课程的学生姓名(子查询/分组条件/集合运算) -select stuname from tb_student where stuid=( - select stuid from tb_score group by stuid having count(stuid)>2 -) - --- 查询学生姓名、课程名称以及成绩(连接查询) -select stuname, couname, scmark from tb_student t1, tb_course t2, tb_score t3 where t1.stuid=t3.stuid and t2.couid=t3.couid and scmark is not null; - -select stuname, couname, scmark from tb_student t1 inner join tb_score t3 on t1.stuid=t3.stuid inner join tb_course t2 on t2.couid=t3.couid where scmark is not null order by scmark desc limit 5 offset 10; - -select stuname, couname, scmark from tb_student t1 inner join tb_score t3 on t1.stuid=t3.stuid inner join tb_course t2 on t2.couid=t3.couid where scmark is not null order by scmark desc limit 10, 5; - --- 单表:65535TB --- 单列:4G - LONGBLOB (Binary Large OBject) / LONGTEXT --- 查询选课学生的姓名和平均成绩(子查询和连接查询) -select stuname, avgmark from tb_student t1, (select stuid, avg(scmark) as avgmark from tb_score group by stuid) t2 where t1.stuid=t2.stuid; - -select stuname, avgmark from tb_student t1 inner join -(select stuid, avg(scmark) as avgmark from tb_score group by stuid) t2 on t1.stuid=t2.stuid; - --- 内连接(inner join):只有满足连接条件的记录才会被查出来 --- 外连接(outer join):左外连接 / 右外连接 / 全外连接 --- left outer join / right outer join / full outer join --- 查询每个学生的姓名和选课数量(左外连接和子查询) -select stuname, ifnull(total, 0) from tb_student t1 left outer join (select stuid, count(stuid) as total from tb_score group by stuid) t2 on t1.stuid=t2.stuid; - - table - tb_score - view - SP_VIEW_CUSTOMQUERY - windowVerticalDividerPosition - 163 - - - encrypted - - format - connection - queryFavorites - - queryHistory - - select stuname, couname, scmark from tb_student t1 inner join tb_score t3 on t1.stuid=t3.stuid inner join tb_course t2 on t2.couid=t3.couid where scmark is not null order by scmark desc limit 5 offset 10 - select stuname, couname, scmark from tb_student t1 inner join tb_score t3 on t1.stuid=t3.stuid inner join tb_course t2 on t2.couid=t3.couid where scmark is not null order by scmark desc limit 10, 5 - select stuname, couname, scmark from tb_student t1 inner join tb_score t3 on t1.stuid=t3.stuid inner join tb_course t2 on t2.couid=t3.couid where scmark is not null order by scmark desc limit 5, 5 - select stuname, couname, scmark from tb_student t1 inner join tb_score t3 on t1.stuid=t3.stuid inner join tb_course t2 on t2.couid=t3.couid where scmark is not null order by scmark desc limit 50, 5 - select stuname, couname, scmark from tb_student t1 inner join tb_score t3 on t1.stuid=t3.stuid inner join tb_course t2 on t2.couid=t3.couid where scmark is not null order by scmark desc limit 5 offset 5 - select stuname, couname, scmark from tb_student t1 inner join tb_score t3 on t1.stuid=t3.stuid inner join tb_course t2 on t2.couid=t3.couid where scmark is not null order by scmark desc limit 5 - select stuname, couname, scmark from tb_student t1 inner join tb_score t3 on t1.stuid=t3.stuid inner join tb_course t2 on t2.couid=t3.couid where scmark is not null order by scmark desc - select stuname, couname, scmark from tb_student t1 inner join tb_score t3 on t1.stuid=t3.stuid inner join tb_course t2 on t2.couid=t3.couid where scmark is not null - select stuname, couname, scmark from tb_student t1, tb_course t2, tb_score t3 where t1.stuid=t3.stuid and t2.couid=t3.couid and scmark is not null - select stuname, couname, scmark from tb_student t1, tb_course t2, tb_score t3 where t1.stuid=t3.stuid and t2.couid=t3.couid where scmark is not null - -- 查询学生姓名、课程名称以及成绩(连接查询) -select stuname, couname, scmark from tb_student t1, tb_course t2, tb_score t3 where t1.stuid=t3.stuid and t2.couid=t3.couid where scmark is not null - select stuname, avgmark from tb_student t1 inner join -(select stuid, avg(scmark) as avgmark from tb_score group by stuid) t2 on t1.stuid=t2.stuid - select stuname, avgmark from tb_student t1, (select stuid, avg(scmark) as avgmark from tb_score group by stuid) t2 where t1.stuid=t2.stuid - select stuname, ifnull(total, 0) from tb_student t1 left outer join (select stuid, count(stuid) as total from tb_score group by stuid) t2 on t1.stuid=t2.stuid - select stuname, ifnull(total,0) from tb_student t1 left outer join (select stuid, count(stuid) as total from tb_score group by stuid) t2 on t1.stuid=t2.stuid - -- 查询选了两门以上的课程的学生姓名(子查询/分组条件/集合运算) -select stuname from tb_student where stuid=( - select stuid from tb_score group by stuid having count(stuid)>2 -) - --- 查询学生姓名、课程名称以及成绩(连接查询) - --- 查询选课学生的姓名和平均成绩(子查询和连接查询) - --- 外连接(outer join):左外连接 / 右外连接 / 全外连接 --- 查询每个学生的姓名和选课数量(左外连接和子查询) -select stuname, ifnull(total, 0) from tb_student t1 left outer join (select stuid, count(stuid) as total from tb_score group by stuid) t2 on t1.stuid=t2.stuid - select stuname, total from tb_student t1 left outer join -(select stuid, count(stuid) as total from tb_score group by stuid) t2 on t1.stuid=t2.stuid - select stuname, total from tb_student t1 inner join -(select stuid, count(stuid) as total from tb_score group by stuid) t2 on t1.stuid=t2.stuid - select stuid, count(stuid) from tb_score group by stuid - select stuname, couname, scmark from tb_student t1 inner join tb_score t3 on t1.stuid=t3.stuid inner join tb_course t2 on t2.couid=t3.couid - - rdbms_type - mysql - rdbms_version - 5.5.60-MariaDB - version - 1 - - diff --git a/Day41-55/05.Cookie和Session.md b/Day41-55/05.Cookie和Session.md deleted file mode 100644 index 2d19016..0000000 --- a/Day41-55/05.Cookie和Session.md +++ /dev/null @@ -1,4 +0,0 @@ -## Cookie和Session - -如今,一个网站如果不通过某种方式记住你是谁以及你之前在网站的活动情况,失去的就是网站的可用性和便利性,继而很有可能导致网站用户的流式,所以记住一个用户(更专业的说法叫**用户跟踪**)对绝大多数Web应用来说都是必需的功能。 - diff --git a/Day41-55/06.中间件的应用.md b/Day41-55/06.中间件的应用.md deleted file mode 100644 index 4c70b77..0000000 --- a/Day41-55/06.中间件的应用.md +++ /dev/null @@ -1,4 +0,0 @@ -## 中间件的应用 - - - diff --git a/Day41-55/07.日志和调试.md b/Day41-55/07.日志和调试.md deleted file mode 100644 index a9ad1fc..0000000 --- a/Day41-55/07.日志和调试.md +++ /dev/null @@ -1,4 +0,0 @@ -## 日志和调试 - - - diff --git a/Day41-55/01.快速上手.md b/Day41-55/41.快速上手.md similarity index 100% rename from Day41-55/01.快速上手.md rename to Day41-55/41.快速上手.md diff --git a/Day41-55/02.深入模型.md b/Day41-55/42.深入模型.md similarity index 100% rename from Day41-55/02.深入模型.md rename to Day41-55/42.深入模型.md diff --git a/Day41-55/03.静态资源和Ajax请求.md b/Day41-55/43.静态资源和Ajax请求.md similarity index 98% rename from Day41-55/03.静态资源和Ajax请求.md rename to Day41-55/43.静态资源和Ajax请求.md index c8b9e02..8ee7fb3 100644 --- a/Day41-55/03.静态资源和Ajax请求.md +++ b/Day41-55/43.静态资源和Ajax请求.md @@ -231,7 +231,7 @@ urlpatterns = [ 启动服务器运行项目,进入首页查看学科信息。 -![](/Users/Hao/Desktop/Python-100-Days/Day41-55/res/show-subjects.png) +![](./res/show-subjects.png) 点击学科查看老师信息。 @@ -301,4 +301,4 @@ def praise_or_criticize(request): ### 小结 -到此为止,这个投票项目的核心功能已然完成,在下一个章节中我们要求用户必须登录才能投票,没有账号的用户可以通过注册功能注册一个账号。 \ No newline at end of file +到此为止,这个投票项目的核心功能已然完成,在下面的章节中我们会要求用户必须登录才能投票,没有账号的用户可以通过注册功能注册一个账号。 \ No newline at end of file diff --git a/Day41-55/04.表单的应用.md b/Day41-55/44.表单的应用.md similarity index 98% rename from Day41-55/04.表单的应用.md rename to Day41-55/44.表单的应用.md index e2b6de1..8a2f9e0 100644 --- a/Day41-55/04.表单的应用.md +++ b/Day41-55/44.表单的应用.md @@ -99,7 +99,7 @@ class RegisterForm(forms.ModelForm): exclude = ('no', 'regdate') ``` -上面,我们定义了一个与User模型绑定的表单(继承自ModelForm),我们排除了用户编号(no)和注册日期(regdate)这两个属性,并添加了一个repassword属性用来接收从用户表单传给服务器的确认密码。我们在定义User模型时已经对用户名的最大长度进行了限制,上面我们又对确认密码的最小和最大长度进行了限制,但是这些都不足以完成我们对用户输入的验证。上面以`clean_`打头的方法就是我们自定义的验证规则。很明显,`clean_username`是对用户名的检查,而`clean_password`是对密码的检查。由于数据库二维表中不应该保存密码的原文,所以对密码做了一个简单的MD5摘要处理(实际开发中这样处理还不太够,因为有被实施反向查表法(利用彩虹表反向查询)破解用户密码的风险)。生成MD5摘要的代码如下所示。 +上面,我们定义了一个与User模型绑定的表单(继承自ModelForm),我们排除了用户编号(no)和注册日期(regdate)这两个属性,并添加了一个repassword属性用来接收从用户表单传给服务器的确认密码。我们在定义User模型时已经对用户名的最大长度进行了限制,上面我们又对确认密码的最小和最大长度进行了限制,但是这些都不足以完成我们对用户输入的验证。上面以`clean_`打头的方法就是我们自定义的验证规则。很明显,`clean_username`是对用户名的检查,而`clean_password`是对密码的检查。由于数据库二维表中不应该保存密码的原文,所以对密码做了一个简单的MD5摘要处理,实际开发中这样处理还不太够,因为有被实施反向查表法(利用彩虹表反向查询)破解用户密码的风险。为字符串生成MD5摘要的代码如下所示。 ```Python def to_md5_hex(message): @@ -383,6 +383,8 @@ def random_color(start=0, end=255, opacity=255): return red, green, blue, opacity ``` +> 说明:上面的代码在生成验证码图片时用到了三种字体文件,使用上面的代码时需要添加字体文件到应用目录下的fonts目录中。 + 下面的视图函数用来生成验证码并通过HttpResponse对象输出到用户浏览器中。 ```Python diff --git a/Day41-55/45.Cookie和Session.md b/Day41-55/45.Cookie和Session.md new file mode 100644 index 0000000..cf31b52 --- /dev/null +++ b/Day41-55/45.Cookie和Session.md @@ -0,0 +1,185 @@ +## Cookie和Session + +### 实现用户跟踪 + +如今,一个网站如果不通过某种方式记住你是谁以及你之前在网站的活动情况,失去的就是网站的可用性和便利性,继而很有可能导致网站用户的流式,所以记住一个用户(更专业的说法叫**用户跟踪**)对绝大多数Web应用来说都是必需的功能。 + +在服务器端,我们想记住一个用户最简单的办法就是创建一个对象,通过这个对象就可以把用户相关的信息都保存起来,这个对象就是我们常说的session(用户会话对象)。那么问题来了,HTTP本身是一个无连接(每次请求和响应的过程中,服务器一旦完成对客户端请求的响应之后就断开连接)、无状态(客户端再次发起对服务器的请求时,服务器无法得知这个客户端之前的任何信息)的协议,即便服务器通过session对象保留了用户数据,还得通过某种方式来确定当前的请求与之前保存过的哪一个session是有关联的。相信很多人都能想到,我们可以给每个session对象分配一个全局唯一的标识符来识别session对象,我们姑且称之为sessionid,每次客户端发起请求时,只要携带上这个sessionid,就有办法找到与之对应的session对象,从而实现在两次请求之间记住该用户的信息,也就是我们之前说的用户跟踪。 + +要让客户端记住并在每次请求时带上sessionid又有以下几种做法: + +1. URL重写。所谓URL重写就是在URL中携带sessionid,例如:`http://www.example.com/index.html?sessionid=123456`,服务器通过获取sessionid参数的值来取到与之对应的session对象。 + +2. 隐藏域(隐式表单域)。在提交表单的时候,可以通过在表单中设置隐藏域向服务器发送额外的数据。例如:``。 + +3. Cookie。Cookie是保存在浏览器临时文件中的数据,每次请求时,请求头中会携带本站点的cookie到服务器,那么只要将sessionid写入cookie,下次请求时服务器只要读取请求头中的cookie就能够获得这个sessionid,如下图所示: + + ![](./res/sessionid_from_cookie.png) + +需要说明的是,在HTML5时代要想在浏览器中保存数据,除了使用cookie之外,还可以使用新的本地存储API,包括localStorage、sessionStorage、IndexedDB等,如下图所示。 + +![](./res/cookie_xstorage_indexeddb.png) + +### Django框架对session的支持 + +在创建Django项目时,默认的配置文件`settings.py`文件中已经激活了一个名为`SessionMiddleware`的中间件(关于中间件的知识我们在下一个章节做详细的讲解,这里只需要知道它的存在即可),因为这个中间件的存在,我们可以直接通过请求对象的`session`属性来操作会话对象。`session`属性是一个像字典一样可以读写数据的容器对象,因此我们可以使用“键值对”的方式来保留用户数据。与此同时,`SessionMiddleware`中间件还封装了对cookie的操作,在cookie中保存了sessionid,就如同我们之前描述的那样。 + +在默认情况下,Django将session的数据序列化后保存在关系型数据库中,在Django 1.6以后的版本中,默认的序列化数据的方式是JSON序列化,而在此之前一直使用Pickle序列化。JSON序列化和Pickle序列化的差别在于前者将对象序列化为字符串(字符形式),而后者将对象序列化为字节串(二进制形式),因为安全方面的原因,JSON序列化成为了目前Django框架默认序列化数据的方式,这就要求在我们保存在session中的数据必须是能够JSON序列化的,否则就会引发异常。还有一点需要说明的是,使用关系型数据库保存session中的数据在大多数时候并不是最好的选择,因为数据库可能会承受巨大的压力而成为系统性能的瓶颈,在后面的章节中我们会告诉大家如何将session的数据保存到缓存服务中。 + +我们继续完善之前的投票应用,前一个章节中我们实现了用户的登录和注册,下面我们首先完善登录时对验证码的检查。 + +```Python +def get_captcha(request): + """验证码""" + captcha_text = random_captcha_text() + request.session['captcha'] = captcha_text + image_data = Captcha.instance().generate(captcha_text) + return HttpResponse(image_data, content_type='image/png') +``` + +注意上面代码中的第4行,我们将随机生成的验证码字符串保存到session中,稍后用户登录时,我们要将保存在session中的验证码字符串和用户输入的验证码字符串进行比对,如果用户输入了正确的验证码才能够执行后续的登录流程,代码如下所示。 + +```Python +def login(request: HttpRequest): + """登录""" + hint = '' + if request.method == 'POST': + form = LoginForm(request.POST) + if form.is_valid(): + # 对验证码的正确性进行验证 + captcha_from_user = form.cleaned_data['captcha'] + captcha_from_sess = request.session.get('captcha', '') + if captcha_from_sess.lower() != captcha_from_user.lower(): + hint = '请输入正确的验证码' + else: + username = form.cleaned_data['username'] + password = form.cleaned_data['password'] + user = User.objects.filter(username=username, password=password).first() + if user: + # 登录成功后将用户编号和用户名保存在session中 + request.session['no'] = user.no + request.session['username'] = user.username + return redirect('/') + else: + hint = '用户名或密码错误' + else: + hint = '请输入有效的登录信息' + return render(request, 'login.html', {'hint': hint}) +``` + +上面的代码中,我们设定了登录成功后会在session中保存用户的编号(`no`)和用户名(`username`),页面会重定向到首页。接下来我们可以稍微对首页的代码进行调整,在页面的右上角显示出登录用户的用户名。我们将这段代码单独写成了一个名为header.html的HTML文件,首页中可以通过在``标签中添加`{% include 'header.html' %}`来包含这个页面,代码如下所示。 + +```HTML +
+ {% if request.session.no %} + {{ request.session.username }} + 注销 + {% else %} + 登录   + {% endif %} + 注册 +
+``` + +如果用户没有登录,页面会显示登录和注册的超链接;而用户登录成功后,页面上会显示用户名和注销的链接,注销链接对应的视图函数如下所示。 + +```Python +def logout(request): + """注销""" + request.session.flush() + return redirect('/') +``` + +上面的代码通过session对象`flush`方法来销毁session,一方面清除了服务器上session对象保存的用户数据,一方面将保存在浏览器cookie中的sessionid删除掉,稍后我们会对如何读写cookie的操作加以说明。 + +我们可以通过项目使用的数据库中名为`django_session` 的表来找到所有的session,该表的结构如下所示: + +| session_key | session_data | expire_date | +| -------------------------------- | ------------------------------- | -------------------------- | +| c9g2gt5cxo0k2evykgpejhic5ae7bfpl | MmI4YzViYjJhOGMyMDJkY2M5Yzg3... | 2019-05-25 23:16:13.898522 | + +其中,第1列就是浏览器cookie中保存的sessionid;第2列是经过BASE64编码后的session中的数据,如果使用Python的`base64`对其进行解码,解码的过程和结果如下所示。 + +```Python +>>> import base64 +>>> base64.b64decode('MmI4YzViYjJhOGMyMDJkY2M5Yzg3ZWIyZGViZmUzYmYxNzdlNDdmZjp7ImNhcHRjaGEiOiJzS3d0Iiwibm8iOjEsInVzZXJuYW1lIjoiamFja2ZydWVkIn0=') +'2b8c5bb2a8c202dcc9c87eb2debfe3bf177e47ff:{"captcha":"sKwt","no":1,"username":"jackfrued"}' +``` + +第3列是session的过期时间,session过期后浏览器保存的cookie中的sessionid就会失效,但是数据库中的这条对应的记录仍然会存在,如果想清除过期的数据,可以使用下面的命令。 + +```Shell +python manage.py clearsessions +``` + +Django框架默认的session过期时间为两周(1209600秒),如果想修改这个时间,可以在项目的配置文件中添加如下所示的代码。 + +```Python +# 配置会话的超时时间为1天(86400秒) +SESSION_COOKIE_AGE = 86400 +``` + +有很多对安全性要求较高的应用都必须在关闭浏览器窗口时让会话过期,不再保留用户的任何信息,如果希望在关闭浏览器窗口时就让会话过期(cookie中的sessionid失效),可以加入如下所示的配置。 + +```Python +# 设置为True在关闭浏览器窗口时session就过期 +SESSION_EXPIRE_AT_BROWSER_CLOSE = True +``` + +如果不希望将session的数据保存在数据库中,可以将其放入缓存中,对应的配置如下所示,缓存的配置和使用我们在后面讲解。 + +```Python +# 配置将会话对象放到缓存中存储 +SESSION_ENGINE = 'django.contrib.sessions.backends.cache' +# 配置使用哪一组缓存来保存会话 +SESSION_CACHE_ALIAS = 'default' +``` + +如果要修改session数据默认的序列化方式,可以将默认的`JSONSerializer`修改为`PickleSerializer`。 + +```Python +SESSION_SERIALIZER = 'django.contrib.sessions.serializers.PickleSerializer' +``` + +### 在视图函数中读写cookie + +Django封装的`HttpRequest`和`HttpResponse`对象分别提供了读写cookie的操作。 + +HttpRequest封装的属性和方法: + +1. `COOKIES`属性 - 该属性包含了HTTP请求携带的所有cookie。 +2. `get_signed_cookie`方法 - 获取带签名的cookie,如果签名验证失败,会产生`BadSignature`异常。 + +HttpResponse封装的方法: + +1. `set_cookie`方法 - 该方法可以设置一组键值对并将其最终将写入浏览器。 +2. `set_signed_cookie`方法 - 跟上面的方法作用相似,但是会对cookie进行签名来达到防篡改的作用。因为如果篡改了cookie中的数据,在不知道[密钥]()和[盐]()的情况下是无法生成有效的签名,这样服务器在读取cookie时会发现数据与签名不一致从而产生`BadSignature`异常。需要说明的是,这里所说的密钥就是我们在Django项目配置文件中指定的`SECRET_KEY`,而盐是程序中设定的一个字符串,你愿意设定为什么都可以,只要是一个有效的字符串。 + +上面提到的方法,如果不清楚它们的具体用法,可以自己查阅一下Django的[官方文档](),没有什么资料比官方文档能够更清楚的告诉你这些方法到底如何使用。 + +刚才我们说过了,激活`SessionMiddleware`之后,每个`HttpRequest`对象都会绑定一个session属性,它是一个类似字典的对象,除了保存用户数据之外还提供了检测浏览器是否支持cookie的方法,包括: + +1. `set_test_cookie`方法 - 设置用于测试的cookie。 +2. `test_cookie_worked`方法 - 检测测试cookie是否工作。 +3. `delete_test_cookie`方法 - 删除用于测试的cookie。 +4. `set_expiry`方法 - 设置会话的过期时间。 +5. `get_expire_age`/`get_expire_date`方法 - 获取会话的过期时间。 +6. `clear_expired`方法 - 清理过期的会话。 + +下面是在执行登录之前检查浏览器是否支持cookie的代码。 + +```Python +def login(request): + if request.method == 'POST': + if request.session.test_cookie_worked(): + request.session.delete_test_cookie() + # Add your code to perform login process here + else: + return HttpResponse("Please enable cookies and try again.") + request.session.set_test_cookie() + return render_to_response('login.html') +``` + +### Cookie的替代品 + +之前我们说过了,cookie的名声一直都不怎么好,当然我们在实际开发中是不会在cookie中保存用户的敏感信息(如用户的密码、信用卡的账号等)的,而且保存在cookie中的数据一般也会做好编码和签名的工作。即便如此,HTML5中还是给出了用于替代cookie的技术方案,其中使用得最为广泛的就是localStorage和sessionStorage,相信从名字上你就能听出二者的差别,存储在`localStorage`的数据可以长期保留;而存储在`sessionStorage`的数据会在浏览器关闭时会被清除 。关于这些cookie替代品的用法,建议大家查阅[MDN]()来进行了解。 \ No newline at end of file diff --git a/Day41-55/46.中间件的应用.md b/Day41-55/46.中间件的应用.md new file mode 100644 index 0000000..fb39beb --- /dev/null +++ b/Day41-55/46.中间件的应用.md @@ -0,0 +1,135 @@ +## 中间件的应用 + +我们继续来完善投票应用。在上一个章节中,我们在用户登录成功后通过session保留了用户信息,接下来我们可以应用做一些调整,要求在为老师投票时必须要先登录,登录过的用户可以投票,否则就将用户引导到登录页面,为此我们可以这样修改视图函数。 + +```Python +def praise_or_criticize(request: HttpRequest): + """投票""" + if 'username' in request.session: + try: + tno = int(request.GET.get('tno', '0')) + teacher = Teacher.objects.get(no=tno) + if request.path.startswith('/vote/praise'): + teacher.good_count += 1 + else: + teacher.bad_count += 1 + teacher.save() + data = {'code': 200, 'message': '操作成功'} + except (ValueError, Teacher.DoesNotExist): + data = {'code': 404, 'message': '操作失败'} + else: + data = {'code': 401, 'message': '请先登录'} + return JsonResponse(data) +``` + +前端页面在收到`{'code': 401, 'message': '请先登录'}`后,可以将用户引导到登录页面,修改后的teacher.html页面的JavaScript代码部门如下所示。 + +```HTML + +``` + +> 注意:为了在登录成功之后能够回到刚才投票的页面,我们在跳转登录时设置了一个`backurl`参数,把当前浏览器中的URL作为返回的页面地址。 + +这样我们已经实现了用户必须登录才能投票的限制,但是一个新的问题来了。如果我们的应用中有很多功能都需要用户先登录才能执行,那么我们是不是需要在每个视图函数中添加代码来检查session中是否包含了登录用户的信息呢?答案是否定的,如果这样做了,我们的视图函数中必然会充斥着大量的重复代码。编程大师*Martin Fowler*曾经说过:**代码有很多种坏味道,重复是最坏的一种**。我们可以把验证用户是否登录这样的代码放到Django的中间件中。 + +### Django中间件概述 + +中间件是安插在Web应用请求和响应过程之间的组件,它在整个Web应用中扮演了拦截过滤器的角色,通过中间件可以拦截请求和响应,并对请求和响应进行过滤(简单的说就是执行额外的处理)。通常,一个中间件组件只专注于完成一件特定的事,例如:Django框架通过`SessionMiddleware`中间件实现了对session的支持,又通过`AuthenticationMiddleware`中间件实现了基于session的请求认证。通过把多个中间件组合在一起,我们可以完成更为复杂的任务,Django框架就是这么做的。 + +Django项目的配置文件中就包含了对中间件的配置,代码如下所示。 + +```Python +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] +``` + +我们稍微为大家解释一下这些中间件的作用: + +1. CommonMiddleware - 基础设置中间件,可以处理以下一些配置参数。 + - DISALLOWED_USER_AGENTS - 不被允许的用户代理(浏览器) + - APPEND_SLASH - 是否追加`/` + - USE_ETAG - 浏览器缓存相关 +2. SecurityMiddleware - 安全相关中间件,可以处理和安全相关的配置项。 + - SECURE_HSTS_SECONDS - 强制使用HTTPS的时间 + - SECURE_HSTS_INCLUDE_SUBDOMAINS - HTTPS是否覆盖子域名 + - SECURE_CONTENT_TYPE_NOSNIFF - 是否允许浏览器推断内容类型 + - SECURE_BROWSER_XSS_FILTER - 是否启用跨站脚本攻击过滤器 + - SECURE_SSL_REDIRECT - 是否重定向到HTTPS连接 + - SECURE_REDIRECT_EXEMPT - 免除重定向到HTTPS +3. SessionMiddleware - 会话中间件。 +4. CsrfViewMiddleware - 通过生成令牌,防范跨请求份伪的造中间件。 +5. XFrameOptionsMiddleware - 通过设置请求头参数,防范点击劫持攻击的中间件。 + +在请求的过程中,上面的中间件会按照书写的顺序从上到下执行,然后是URL解析,最后请求才会来到视图函数;在响应的过程中,上面的中间件会按照书写的顺序从下到上执行,与请求时中间件执行的顺序正好相反。 + +### 自定义中间件 + +Django中的中间件有两种实现方式:基于类的实现方式和基于函数的实现方式,后者更接近于装饰器的写法。装饰器实际上是代理模式的应用,将横切关注功能(与正常业务逻辑没有必然联系的功能,例如:身份认证、日志记录、编码转换之类的功能)置于代理中,由代理对象来完成被代理对象的行为并添加额外的功能。中间件对用户请求和响应进行拦截过滤并增加额外的处理,在这一点上它跟装饰器是完全一致的,所以基于函数的写法来实现中间件就跟装饰器的写法几乎一模一样。下面我们用自定义的中间件来实现对用户进行登录验证的功能。 + +```Python +""" +middlewares.py +""" +from django.shortcuts import redirect + + +def check_login_middleware(func): + + def wrapper(request, *args, **kwargs): + # 获取请求的资源路径 + path = request.path + # 如果请求的资源路径在设定的元组中就表示需要登录验证 + if path in ('/vote/praise/', '/vote/criticize/'): + if 'username' not in request.session: + # session中没有username就重定向到登录页 + return redirect('login') + return func(request, *args, **kwargs) + + return wrapper +``` + +修改配置文件,激活中间件使其生效。 + +```Python +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', + 'debug_toolbar.middleware.DebugToolbarMiddleware', + 'vote.middlewares.check_login_middleware', +] +``` + diff --git a/Day41-55/47.日志和调试.md b/Day41-55/47.日志和调试.md new file mode 100644 index 0000000..7c509ac --- /dev/null +++ b/Day41-55/47.日志和调试.md @@ -0,0 +1,3 @@ +## 日志和调试 + +在项目开发阶段,显示足够的调试信息以辅助开发人员调试代码还是非常必要的. diff --git a/Day41-55/08.文件上传和富文本编辑.md b/Day41-55/48.文件上传和富文本编辑.md similarity index 100% rename from Day41-55/08.文件上传和富文本编辑.md rename to Day41-55/48.文件上传和富文本编辑.md diff --git a/Day41-55/09.文件下载和报表.md b/Day41-55/49.文件下载和报表.md similarity index 100% rename from Day41-55/09.文件下载和报表.md rename to Day41-55/49.文件下载和报表.md diff --git a/Day41-55/10.RESTful架构和DRF入门.md b/Day41-55/50.RESTful架构和DRF入门.md similarity index 100% rename from Day41-55/10.RESTful架构和DRF入门.md rename to Day41-55/50.RESTful架构和DRF入门.md diff --git a/Day41-55/11.RESTful架构和DRF进阶.md b/Day41-55/51.RESTful架构和DRF进阶.md similarity index 100% rename from Day41-55/11.RESTful架构和DRF进阶.md rename to Day41-55/51.RESTful架构和DRF进阶.md diff --git a/Day41-55/12.使用缓存.md b/Day41-55/52.使用缓存.md similarity index 100% rename from Day41-55/12.使用缓存.md rename to Day41-55/52.使用缓存.md diff --git a/Day41-55/13.短信和邮件.md b/Day41-55/53.短信和邮件.md similarity index 100% rename from Day41-55/13.短信和邮件.md rename to Day41-55/53.短信和邮件.md diff --git a/Day41-55/14.异步任务和定时任务.md b/Day41-55/54.异步任务和定时任务.md similarity index 100% rename from Day41-55/14.异步任务和定时任务.md rename to Day41-55/54.异步任务和定时任务.md diff --git a/Day41-55/15.单元测试和项目上线.md b/Day41-55/55.单元测试和项目上线.md similarity index 100% rename from Day41-55/15.单元测试和项目上线.md rename to Day41-55/55.单元测试和项目上线.md diff --git a/Day41-55/res/cookie_xstorage_indexeddb.png b/Day41-55/res/cookie_xstorage_indexeddb.png new file mode 100644 index 0000000..a08c257 Binary files /dev/null and b/Day41-55/res/cookie_xstorage_indexeddb.png differ diff --git a/Day41-55/res/sessionid_from_cookie.png b/Day41-55/res/sessionid_from_cookie.png new file mode 100644 index 0000000..6dfc76e Binary files /dev/null and b/Day41-55/res/sessionid_from_cookie.png differ diff --git a/Day56-60/01.Flask入门.md b/Day56-60/56.Flask入门.md similarity index 100% rename from Day56-60/01.Flask入门.md rename to Day56-60/56.Flask入门.md diff --git a/Day56-60/02.模板的使用.md b/Day56-60/57.模板的使用.md similarity index 100% rename from Day56-60/02.模板的使用.md rename to Day56-60/57.模板的使用.md diff --git a/Day56-60/03.表单的处理.md b/Day56-60/58.表单的处理.md similarity index 100% rename from Day56-60/03.表单的处理.md rename to Day56-60/58.表单的处理.md diff --git a/Day56-60/04.数据库操作.md b/Day56-60/59.数据库操作.md similarity index 100% rename from Day56-60/04.数据库操作.md rename to Day56-60/59.数据库操作.md diff --git a/Day56-60/05.项目实战.md b/Day56-60/60.项目实战.md similarity index 100% rename from Day56-60/05.项目实战.md rename to Day56-60/60.项目实战.md diff --git a/Day61-65/01.预备知识.md b/Day61-65/61.预备知识.md similarity index 100% rename from Day61-65/01.预备知识.md rename to Day61-65/61.预备知识.md diff --git a/Day61-65/02.Tornado入门.md b/Day61-65/62.Tornado入门.md similarity index 100% rename from Day61-65/02.Tornado入门.md rename to Day61-65/62.Tornado入门.md diff --git a/Day61-65/03.异步化.md b/Day61-65/63.异步化.md similarity index 100% rename from Day61-65/03.异步化.md rename to Day61-65/63.异步化.md diff --git a/Day61-65/04.WebSocket的应用.md b/Day61-65/64.WebSocket的应用.md similarity index 100% rename from Day61-65/04.WebSocket的应用.md rename to Day61-65/64.WebSocket的应用.md diff --git a/Day61-65/05.项目实战.md b/Day61-65/65.项目实战.md similarity index 100% rename from Day61-65/05.项目实战.md rename to Day61-65/65.项目实战.md diff --git a/Day66-75/01.网络爬虫和相关工具.md b/Day66-75/66.网络爬虫和相关工具.md similarity index 100% rename from Day66-75/01.网络爬虫和相关工具.md rename to Day66-75/66.网络爬虫和相关工具.md diff --git a/Day66-75/02.数据采集和解析.md b/Day66-75/67.数据采集和解析.md similarity index 100% rename from Day66-75/02.数据采集和解析.md rename to Day66-75/67.数据采集和解析.md diff --git a/Day66-75/03.存储数据.md b/Day66-75/68.存储数据.md similarity index 100% rename from Day66-75/03.存储数据.md rename to Day66-75/68.存储数据.md diff --git a/Day66-75/04.并发下载.md b/Day66-75/69.并发下载.md similarity index 100% rename from Day66-75/04.并发下载.md rename to Day66-75/69.并发下载.md diff --git a/Day66-75/05.解析动态内容.md b/Day66-75/70.解析动态内容.md similarity index 100% rename from Day66-75/05.解析动态内容.md rename to Day66-75/70.解析动态内容.md diff --git a/Day66-75/06.表单交互和验证码处理.md b/Day66-75/71.表单交互和验证码处理.md similarity index 100% rename from Day66-75/06.表单交互和验证码处理.md rename to Day66-75/71.表单交互和验证码处理.md diff --git a/Day66-75/07.Scrapy入门.md b/Day66-75/72.Scrapy入门.md similarity index 100% rename from Day66-75/07.Scrapy入门.md rename to Day66-75/72.Scrapy入门.md diff --git a/Day66-75/08.Scrapy高级应用.md b/Day66-75/73.Scrapy高级应用.md similarity index 100% rename from Day66-75/08.Scrapy高级应用.md rename to Day66-75/73.Scrapy高级应用.md diff --git a/Day66-75/09.Scrapy分布式实现.md b/Day66-75/74.Scrapy分布式实现.md similarity index 100% rename from Day66-75/09.Scrapy分布式实现.md rename to Day66-75/74.Scrapy分布式实现.md diff --git a/Day66-75/10.爬虫项目实战.md b/Day66-75/75.爬虫项目实战.md similarity index 100% rename from Day66-75/10.爬虫项目实战.md rename to Day66-75/75.爬虫项目实战.md diff --git a/Day76-90/02.Pandas的应用.md b/Day76-90/02.Pandas的应用.md deleted file mode 100644 index 9a101cf..0000000 --- a/Day76-90/02.Pandas的应用.md +++ /dev/null @@ -1,2 +0,0 @@ -## Pandas的应用 - diff --git a/Day76-90/01.机器学习基础.md b/Day76-90/76.机器学习基础.md similarity index 100% rename from Day76-90/01.机器学习基础.md rename to Day76-90/76.机器学习基础.md diff --git a/Day76-90/77.Pandas的应用.md b/Day76-90/77.Pandas的应用.md new file mode 100644 index 0000000..c7139de --- /dev/null +++ b/Day76-90/77.Pandas的应用.md @@ -0,0 +1,25 @@ +## Pandas的应用 + +### 1、Pandas入门 + +### 2、Pandas索引 + +### 3、Pandas数据清洗之空数据 + +[数据挖掘之空数据处理(有史以来最全面)]: https://blog.csdn.net/Soft_Po/article/details/89302887 + +### 4、Pandas多层索引 + +### 5、Pandas多层索引计算 + +### 6、Pandas数据集成concat + +### 7、Pandas数据集成merge + +### 8、Pandas分组聚合操作 + +### 9、Pandas数据集成实战 + +### 10、美国大选项目 + +[2012美国大选政治献金项目数据分析(有史以来最全面)]: https://blog.csdn.net/Soft_Po/article/details/89283382 \ No newline at end of file diff --git a/Day76-90/03.NumPy和SciPy的应用.md b/Day76-90/78.NumPy和SciPy的应用.md similarity index 100% rename from Day76-90/03.NumPy和SciPy的应用.md rename to Day76-90/78.NumPy和SciPy的应用.md diff --git a/Day76-90/04.Matplotlib和数据可视化.md b/Day76-90/79.Matplotlib和数据可视化.md similarity index 100% rename from Day76-90/04.Matplotlib和数据可视化.md rename to Day76-90/79.Matplotlib和数据可视化.md diff --git a/Day76-90/05.k最近邻分类.md b/Day76-90/80.k最近邻分类.md similarity index 100% rename from Day76-90/05.k最近邻分类.md rename to Day76-90/80.k最近邻分类.md diff --git a/Day76-90/06.决策树.md b/Day76-90/81.决策树.md similarity index 100% rename from Day76-90/06.决策树.md rename to Day76-90/81.决策树.md diff --git a/Day76-90/07.贝叶斯分类.md b/Day76-90/82.贝叶斯分类.md similarity index 100% rename from Day76-90/07.贝叶斯分类.md rename to Day76-90/82.贝叶斯分类.md diff --git a/Day76-90/08.支持向量机.md b/Day76-90/83.支持向量机.md similarity index 100% rename from Day76-90/08.支持向量机.md rename to Day76-90/83.支持向量机.md diff --git a/Day76-90/09.K-均值聚类.md b/Day76-90/84.K-均值聚类.md similarity index 100% rename from Day76-90/09.K-均值聚类.md rename to Day76-90/84.K-均值聚类.md diff --git a/Day76-90/10.回归分析.md b/Day76-90/85.回归分析.md similarity index 100% rename from Day76-90/10.回归分析.md rename to Day76-90/85.回归分析.md diff --git a/Day76-90/11.大数据分析入门.md b/Day76-90/86.大数据分析入门.md similarity index 100% rename from Day76-90/11.大数据分析入门.md rename to Day76-90/86.大数据分析入门.md diff --git a/Day76-90/12.大数据分析进阶.md b/Day76-90/87.大数据分析进阶.md similarity index 100% rename from Day76-90/12.大数据分析进阶.md rename to Day76-90/87.大数据分析进阶.md diff --git a/Day76-90/13.Tensorflow入门.md b/Day76-90/88.Tensorflow入门.md similarity index 100% rename from Day76-90/13.Tensorflow入门.md rename to Day76-90/88.Tensorflow入门.md diff --git a/Day76-90/14.Tensorflow实战.md b/Day76-90/89.Tensorflow实战.md similarity index 100% rename from Day76-90/14.Tensorflow实战.md rename to Day76-90/89.Tensorflow实战.md diff --git a/Day76-90/15.推荐系统实战.md b/Day76-90/90.推荐系统实战.md similarity index 100% rename from Day76-90/15.推荐系统实战.md rename to Day76-90/90.推荐系统实战.md diff --git a/Day76-90/code/1-pandas入门.ipynb b/Day76-90/code/1-pandas入门.ipynb new file mode 100644 index 0000000..2f25670 --- /dev/null +++ b/Day76-90/code/1-pandas入门.ipynb @@ -0,0 +1,631 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "\n", + "import pandas as pd" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "from pandas import Series,DataFrame" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "scrolled": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Math 120\n", + "Python 136\n", + "En 128\n", + "Chinese 99\n", + "dtype: int64" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 创建\n", + "# Series是一维的数据\n", + "s = Series(data = [120,136,128,99],index = ['Math','Python','En','Chinese'])\n", + "s" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(4,)" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([120, 136, 128, 99], dtype=int64)" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "v = s.values\n", + "v" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "numpy.ndarray" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(v)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "120.75" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s.mean()" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "136" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s.max()" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "15.903353943953666" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s.std()" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Math 14400\n", + "Python 18496\n", + "En 16384\n", + "Chinese 9801\n", + "dtype: int64" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s.pow(2)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonEnMath
a11311675
b1914523
c57107113
d95366
e28121120
f14185132
h1243910
i803517
j689931
k741211
\n", + "
" + ], + "text/plain": [ + " Python En Math\n", + "a 113 116 75\n", + "b 19 145 23\n", + "c 57 107 113\n", + "d 95 3 66\n", + "e 28 121 120\n", + "f 141 85 132\n", + "h 124 39 10\n", + "i 80 35 17\n", + "j 68 99 31\n", + "k 74 12 11" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# DataFrame是二维的数据\n", + "# excel就非诚相似\n", + "# 所有进行数据分析,数据挖掘的工具最基础的结果:行和列,行表示样本,列表示的是属性\n", + "df = DataFrame(data = np.random.randint(0,150,size = (10,3)),index = list('abcdefhijk'),columns=['Python','En','Math'])\n", + "df" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(10, 3)" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[113, 116, 75],\n", + " [ 19, 145, 23],\n", + " [ 57, 107, 113],\n", + " [ 95, 3, 66],\n", + " [ 28, 121, 120],\n", + " [141, 85, 132],\n", + " [124, 39, 10],\n", + " [ 80, 35, 17],\n", + " [ 68, 99, 31],\n", + " [ 74, 12, 11]])" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "v = df.values\n", + "v" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Python 79.9\n", + "En 76.2\n", + "Math 59.8\n", + "dtype: float64" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.mean()" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Python 141\n", + "En 145\n", + "Math 132\n", + "dtype: int32" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.max()" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonEnMath
a11311675
b1914523
c57107113
d95366
e28121120
f14185132
h1243910
i803517
j689931
k741211
\n", + "
" + ], + "text/plain": [ + " Python En Math\n", + "a 113 116 75\n", + "b 19 145 23\n", + "c 57 107 113\n", + "d 95 3 66\n", + "e 28 121 120\n", + "f 141 85 132\n", + "h 124 39 10\n", + "i 80 35 17\n", + "j 68 99 31\n", + "k 74 12 11" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Python 79.9\n", + "En 76.2\n", + "Math 59.8\n", + "dtype: float64" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.mean(axis = 0)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "a 101.333333\n", + "b 62.333333\n", + "c 92.333333\n", + "d 54.666667\n", + "e 89.666667\n", + "f 119.333333\n", + "h 57.666667\n", + "i 44.000000\n", + "j 66.000000\n", + "k 32.333333\n", + "dtype: float64" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.mean(axis = 1)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Day76-90/code/2-pandas-索引.ipynb b/Day76-90/code/2-pandas-索引.ipynb new file mode 100644 index 0000000..ddbde0a --- /dev/null +++ b/Day76-90/code/2-pandas-索引.ipynb @@ -0,0 +1,2082 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "\n", + "import pandas as pd\n", + "\n", + "from pandas import Series,DataFrame" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "10 34\n", + "11 111\n", + "12 113\n", + "13 103\n", + "14 147\n", + "15 63\n", + "16 11\n", + "17 130\n", + "18 38\n", + "19 17\n", + "20 32\n", + "21 112\n", + "22 75\n", + "23 68\n", + "24 124\n", + "25 138\n", + "26 56\n", + "27 1\n", + "28 88\n", + "29 113\n", + "30 63\n", + "31 42\n", + "32 65\n", + "33 104\n", + "34 105\n", + "35 0\n", + "36 95\n", + "37 119\n", + "38 86\n", + "39 124\n", + " ... \n", + "80 127\n", + "81 139\n", + "82 110\n", + "83 65\n", + "84 127\n", + "85 108\n", + "86 33\n", + "87 91\n", + "88 134\n", + "89 65\n", + "90 110\n", + "91 144\n", + "92 40\n", + "93 3\n", + "94 3\n", + "95 59\n", + "96 97\n", + "97 64\n", + "98 126\n", + "99 94\n", + "100 20\n", + "101 107\n", + "102 59\n", + "103 146\n", + "104 83\n", + "105 59\n", + "106 25\n", + "107 0\n", + "108 78\n", + "109 93\n", + "Name: Python, Length: 100, dtype: int16" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s = Series(np.random.randint(0,150,size = 100),index = np.arange(10,110),dtype=np.int16,name = 'Python')\n", + "s" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "ename": "KeyError", + "evalue": "0", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# 索引操作\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0ms\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;32md:\\python36\\lib\\site-packages\\pandas\\core\\series.py\u001b[0m in \u001b[0;36m__getitem__\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m 866\u001b[0m \u001b[0mkey\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mapply_if_callable\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 867\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 868\u001b[1;33m \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_value\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 869\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 870\u001b[0m \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mis_scalar\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32md:\\python36\\lib\\site-packages\\pandas\\core\\indexes\\base.py\u001b[0m in \u001b[0;36mget_value\u001b[1;34m(self, series, key)\u001b[0m\n\u001b[0;32m 4373\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4374\u001b[0m return self._engine.get_value(s, k,\n\u001b[1;32m-> 4375\u001b[1;33m tz=getattr(series.dtype, 'tz', None))\n\u001b[0m\u001b[0;32m 4376\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0me1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4377\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m>\u001b[0m \u001b[1;36m0\u001b[0m \u001b[1;32mand\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mholds_integer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mis_boolean\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32mpandas\\_libs\\index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_value\u001b[1;34m()\u001b[0m\n", + "\u001b[1;32mpandas\\_libs\\index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_value\u001b[1;34m()\u001b[0m\n", + "\u001b[1;32mpandas\\_libs\\index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[1;34m()\u001b[0m\n", + "\u001b[1;32mpandas\\_libs\\hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.Int64HashTable.get_item\u001b[1;34m()\u001b[0m\n", + "\u001b[1;32mpandas\\_libs\\hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.Int64HashTable.get_item\u001b[1;34m()\u001b[0m\n", + "\u001b[1;31mKeyError\u001b[0m: 0" + ] + } + ], + "source": [ + "# 索引操作\n", + "s[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "34" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s[10]" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "10 34\n", + "20 32\n", + "Name: Python, dtype: int16" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s[[10,20]]" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "20 32\n", + "21 112\n", + "22 75\n", + "23 68\n", + "24 124\n", + "25 138\n", + "26 56\n", + "27 1\n", + "28 88\n", + "29 113\n", + "Name: Python, dtype: int16" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 切片操作\n", + "s[10:20]" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "10 34\n", + "12 113\n", + "14 147\n", + "16 11\n", + "18 38\n", + "20 32\n", + "22 75\n", + "24 124\n", + "26 56\n", + "28 88\n", + "30 63\n", + "32 65\n", + "34 105\n", + "36 95\n", + "38 86\n", + "40 6\n", + "42 57\n", + "44 72\n", + "46 43\n", + "48 87\n", + "50 83\n", + "52 99\n", + "54 132\n", + "56 17\n", + "58 116\n", + "60 33\n", + "62 51\n", + "64 80\n", + "66 121\n", + "68 81\n", + "70 0\n", + "72 50\n", + "74 31\n", + "76 114\n", + "78 60\n", + "80 127\n", + "82 110\n", + "84 127\n", + "86 33\n", + "88 134\n", + "90 110\n", + "92 40\n", + "94 3\n", + "96 97\n", + "98 126\n", + "100 20\n", + "102 59\n", + "104 83\n", + "106 25\n", + "108 78\n", + "Name: Python, dtype: int16" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s[::2]" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "109 93\n", + "107 0\n", + "105 59\n", + "103 146\n", + "101 107\n", + "99 94\n", + "97 64\n", + "95 59\n", + "93 3\n", + "91 144\n", + "89 65\n", + "87 91\n", + "85 108\n", + "83 65\n", + "81 139\n", + "79 14\n", + "77 96\n", + "75 76\n", + "73 29\n", + "71 68\n", + "69 4\n", + "67 57\n", + "65 58\n", + "63 106\n", + "61 42\n", + "59 135\n", + "57 56\n", + "55 12\n", + "53 135\n", + "51 74\n", + "49 129\n", + "47 110\n", + "45 1\n", + "43 90\n", + "41 120\n", + "39 124\n", + "37 119\n", + "35 0\n", + "33 104\n", + "31 42\n", + "29 113\n", + "27 1\n", + "25 138\n", + "23 68\n", + "21 112\n", + "19 17\n", + "17 130\n", + "15 63\n", + "13 103\n", + "11 111\n", + "Name: Python, dtype: int16" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s[::-2]" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "34" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 可以使用pandas为开发者提供方法,去进行检索\n", + "s.loc[10]" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "10 34\n", + "20 32\n", + "Name: Python, dtype: int16" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s.loc[[10,20]]" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "10 34\n", + "11 111\n", + "12 113\n", + "13 103\n", + "14 147\n", + "15 63\n", + "16 11\n", + "17 130\n", + "18 38\n", + "19 17\n", + "20 32\n", + "Name: Python, dtype: int16" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s.loc[10:20]" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "10 34\n", + "12 113\n", + "14 147\n", + "16 11\n", + "18 38\n", + "20 32\n", + "22 75\n", + "24 124\n", + "26 56\n", + "28 88\n", + "30 63\n", + "32 65\n", + "34 105\n", + "36 95\n", + "38 86\n", + "40 6\n", + "42 57\n", + "44 72\n", + "46 43\n", + "48 87\n", + "50 83\n", + "52 99\n", + "54 132\n", + "56 17\n", + "58 116\n", + "60 33\n", + "62 51\n", + "64 80\n", + "66 121\n", + "68 81\n", + "70 0\n", + "72 50\n", + "74 31\n", + "76 114\n", + "78 60\n", + "80 127\n", + "82 110\n", + "84 127\n", + "86 33\n", + "88 134\n", + "90 110\n", + "92 40\n", + "94 3\n", + "96 97\n", + "98 126\n", + "100 20\n", + "102 59\n", + "104 83\n", + "106 25\n", + "108 78\n", + "Name: Python, dtype: int16" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s.loc[::2]" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "109 93\n", + "107 0\n", + "105 59\n", + "103 146\n", + "101 107\n", + "99 94\n", + "97 64\n", + "95 59\n", + "93 3\n", + "91 144\n", + "89 65\n", + "87 91\n", + "85 108\n", + "83 65\n", + "81 139\n", + "79 14\n", + "77 96\n", + "75 76\n", + "73 29\n", + "71 68\n", + "69 4\n", + "67 57\n", + "65 58\n", + "63 106\n", + "61 42\n", + "59 135\n", + "57 56\n", + "55 12\n", + "53 135\n", + "51 74\n", + "49 129\n", + "47 110\n", + "45 1\n", + "43 90\n", + "41 120\n", + "39 124\n", + "37 119\n", + "35 0\n", + "33 104\n", + "31 42\n", + "29 113\n", + "27 1\n", + "25 138\n", + "23 68\n", + "21 112\n", + "19 17\n", + "17 130\n", + "15 63\n", + "13 103\n", + "11 111\n", + "Name: Python, dtype: int16" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s.loc[::-2]" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Int64Index([ 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,\n", + " 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,\n", + " 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,\n", + " 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,\n", + " 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74,\n", + " 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87,\n", + " 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100,\n", + " 101, 102, 103, 104, 105, 106, 107, 108, 109],\n", + " dtype='int64')" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s.index" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "34" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# iloc 索引从0开始,数字化自然索引\n", + "s.iloc[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10 34\n", + "20 32\n", + "Name: Python, dtype: int16" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s.iloc[[0,10]]" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "10 34\n", + "11 111\n", + "12 113\n", + "13 103\n", + "14 147\n", + "15 63\n", + "16 11\n", + "17 130\n", + "18 38\n", + "19 17\n", + "20 32\n", + "21 112\n", + "22 75\n", + "23 68\n", + "24 124\n", + "25 138\n", + "26 56\n", + "27 1\n", + "28 88\n", + "29 113\n", + "Name: Python, dtype: int16" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s.iloc[0:20]" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "109 93\n", + "107 0\n", + "105 59\n", + "103 146\n", + "101 107\n", + "99 94\n", + "97 64\n", + "95 59\n", + "93 3\n", + "91 144\n", + "89 65\n", + "87 91\n", + "85 108\n", + "83 65\n", + "81 139\n", + "79 14\n", + "77 96\n", + "75 76\n", + "73 29\n", + "71 68\n", + "69 4\n", + "67 57\n", + "65 58\n", + "63 106\n", + "61 42\n", + "59 135\n", + "57 56\n", + "55 12\n", + "53 135\n", + "51 74\n", + "49 129\n", + "47 110\n", + "45 1\n", + "43 90\n", + "41 120\n", + "39 124\n", + "37 119\n", + "35 0\n", + "33 104\n", + "31 42\n", + "29 113\n", + "27 1\n", + "25 138\n", + "23 68\n", + "21 112\n", + "19 17\n", + "17 130\n", + "15 63\n", + "13 103\n", + "11 111\n", + "Name: Python, dtype: int16" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s.iloc[::-2]" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonEnMath
A1035698
B13565135
C1379146
D4724145
E899716
F6426109
H4846111
I164997
J122126100
K6013662
\n", + "
" + ], + "text/plain": [ + " Python En Math\n", + "A 103 56 98\n", + "B 135 65 135\n", + "C 13 79 146\n", + "D 47 24 145\n", + "E 89 97 16\n", + "F 64 26 109\n", + "H 48 46 111\n", + "I 16 49 97\n", + "J 122 126 100\n", + "K 60 136 62" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# DataFrame是二维,索引大同小异,\n", + "df = DataFrame(data = np.random.randint(0,150,size= (10,3)),index=list('ABCDEFHIJK'),columns=['Python','En','Math'])\n", + "\n", + "df" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "ename": "KeyError", + "evalue": "'A'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32md:\\python36\\lib\\site-packages\\pandas\\core\\indexes\\base.py\u001b[0m in \u001b[0;36mget_loc\u001b[1;34m(self, key, method, tolerance)\u001b[0m\n\u001b[0;32m 2656\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 2657\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_engine\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2658\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32mpandas\\_libs\\index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[1;34m()\u001b[0m\n", + "\u001b[1;32mpandas\\_libs\\index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[1;34m()\u001b[0m\n", + "\u001b[1;32mpandas\\_libs\\hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[1;34m()\u001b[0m\n", + "\u001b[1;32mpandas\\_libs\\hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[1;34m()\u001b[0m\n", + "\u001b[1;31mKeyError\u001b[0m: 'A'", + "\nDuring handling of the above exception, another exception occurred:\n", + "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mdf\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'A'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;32md:\\python36\\lib\\site-packages\\pandas\\core\\frame.py\u001b[0m in \u001b[0;36m__getitem__\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m 2925\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnlevels\u001b[0m \u001b[1;33m>\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2926\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_getitem_multilevel\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 2927\u001b[1;33m \u001b[0mindexer\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2928\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mis_integer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mindexer\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2929\u001b[0m \u001b[0mindexer\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[0mindexer\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32md:\\python36\\lib\\site-packages\\pandas\\core\\indexes\\base.py\u001b[0m in \u001b[0;36mget_loc\u001b[1;34m(self, key, method, tolerance)\u001b[0m\n\u001b[0;32m 2657\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_engine\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2658\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 2659\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_engine\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_maybe_cast_indexer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2660\u001b[0m \u001b[0mindexer\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_indexer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmethod\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mmethod\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtolerance\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mtolerance\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2661\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mindexer\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mndim\u001b[0m \u001b[1;33m>\u001b[0m \u001b[1;36m1\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0mindexer\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msize\u001b[0m \u001b[1;33m>\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32mpandas\\_libs\\index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[1;34m()\u001b[0m\n", + "\u001b[1;32mpandas\\_libs\\index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[1;34m()\u001b[0m\n", + "\u001b[1;32mpandas\\_libs\\hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[1;34m()\u001b[0m\n", + "\u001b[1;32mpandas\\_libs\\hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[1;34m()\u001b[0m\n", + "\u001b[1;31mKeyError\u001b[0m: 'A'" + ] + } + ], + "source": [ + "df['A']" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "A 103\n", + "B 135\n", + "C 13\n", + "D 47\n", + "E 89\n", + "F 64\n", + "H 48\n", + "I 16\n", + "J 122\n", + "K 60\n", + "Name: Python, dtype: int32" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df['Python']" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonEn
A10356
B13565
C1379
D4724
E8997
F6426
H4846
I1649
J122126
K60136
\n", + "
" + ], + "text/plain": [ + " Python En\n", + "A 103 56\n", + "B 135 65\n", + "C 13 79\n", + "D 47 24\n", + "E 89 97\n", + "F 64 26\n", + "H 48 46\n", + "I 16 49\n", + "J 122 126\n", + "K 60 136" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df[['Python','En']]" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonEnMath
\n", + "
" + ], + "text/plain": [ + "Empty DataFrame\n", + "Columns: [Python, En, Math]\n", + "Index: []" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df['Python':'Math']" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonEnMath
A1035698
B13565135
C1379146
D4724145
\n", + "
" + ], + "text/plain": [ + " Python En Math\n", + "A 103 56 98\n", + "B 135 65 135\n", + "C 13 79 146\n", + "D 47 24 145" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df['A':'D']" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "ename": "KeyError", + "evalue": "'Python'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32md:\\python36\\lib\\site-packages\\pandas\\core\\indexes\\base.py\u001b[0m in \u001b[0;36mget_loc\u001b[1;34m(self, key, method, tolerance)\u001b[0m\n\u001b[0;32m 2656\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 2657\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_engine\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2658\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32mpandas\\_libs\\index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[1;34m()\u001b[0m\n", + "\u001b[1;32mpandas\\_libs\\index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[1;34m()\u001b[0m\n", + "\u001b[1;32mpandas\\_libs\\hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[1;34m()\u001b[0m\n", + "\u001b[1;32mpandas\\_libs\\hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[1;34m()\u001b[0m\n", + "\u001b[1;31mKeyError\u001b[0m: 'Python'", + "\nDuring handling of the above exception, another exception occurred:\n", + "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mdf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mloc\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'Python'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;32md:\\python36\\lib\\site-packages\\pandas\\core\\indexing.py\u001b[0m in \u001b[0;36m__getitem__\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m 1498\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1499\u001b[0m \u001b[0mmaybe_callable\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mapply_if_callable\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mobj\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1500\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_getitem_axis\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmaybe_callable\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 1501\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1502\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0m_is_scalar_access\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32md:\\python36\\lib\\site-packages\\pandas\\core\\indexing.py\u001b[0m in \u001b[0;36m_getitem_axis\u001b[1;34m(self, key, axis)\u001b[0m\n\u001b[0;32m 1911\u001b[0m \u001b[1;31m# fall thru to straight lookup\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1912\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_validate_key\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1913\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_get_label\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 1914\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1915\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32md:\\python36\\lib\\site-packages\\pandas\\core\\indexing.py\u001b[0m in \u001b[0;36m_get_label\u001b[1;34m(self, label, axis)\u001b[0m\n\u001b[0;32m 139\u001b[0m \u001b[1;32mraise\u001b[0m \u001b[0mIndexingError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'no slices here, handle elsewhere'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 140\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 141\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mobj\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_xs\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlabel\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 142\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 143\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0m_get_loc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32md:\\python36\\lib\\site-packages\\pandas\\core\\generic.py\u001b[0m in \u001b[0;36mxs\u001b[1;34m(self, key, axis, level, drop_level)\u001b[0m\n\u001b[0;32m 3583\u001b[0m drop_level=drop_level)\n\u001b[0;32m 3584\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 3585\u001b[1;33m \u001b[0mloc\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3586\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3587\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mloc\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mndarray\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32md:\\python36\\lib\\site-packages\\pandas\\core\\indexes\\base.py\u001b[0m in \u001b[0;36mget_loc\u001b[1;34m(self, key, method, tolerance)\u001b[0m\n\u001b[0;32m 2657\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_engine\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2658\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 2659\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_engine\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_maybe_cast_indexer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2660\u001b[0m \u001b[0mindexer\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_indexer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmethod\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mmethod\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtolerance\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mtolerance\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2661\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mindexer\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mndim\u001b[0m \u001b[1;33m>\u001b[0m \u001b[1;36m1\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0mindexer\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msize\u001b[0m \u001b[1;33m>\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32mpandas\\_libs\\index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[1;34m()\u001b[0m\n", + "\u001b[1;32mpandas\\_libs\\index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[1;34m()\u001b[0m\n", + "\u001b[1;32mpandas\\_libs\\hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[1;34m()\u001b[0m\n", + "\u001b[1;32mpandas\\_libs\\hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[1;34m()\u001b[0m\n", + "\u001b[1;31mKeyError\u001b[0m: 'Python'" + ] + } + ], + "source": [ + "df.loc['Python']" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Python 103\n", + "En 56\n", + "Math 98\n", + "Name: A, dtype: int32" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.loc['A']" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonEnMath
A1035698
H4846111
\n", + "
" + ], + "text/plain": [ + " Python En Math\n", + "A 103 56 98\n", + "H 48 46 111" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.loc[['A','H']]" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonEnMath
A1035698
B13565135
C1379146
D4724145
E899716
\n", + "
" + ], + "text/plain": [ + " Python En Math\n", + "A 103 56 98\n", + "B 135 65 135\n", + "C 13 79 146\n", + "D 47 24 145\n", + "E 89 97 16" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.loc['A':'E']" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonEnMath
A1035698
C1379146
E899716
H4846111
J122126100
\n", + "
" + ], + "text/plain": [ + " Python En Math\n", + "A 103 56 98\n", + "C 13 79 146\n", + "E 89 97 16\n", + "H 48 46 111\n", + "J 122 126 100" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.loc[::2]" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonEnMath
K6013662
I164997
F6426109
D4724145
B13565135
\n", + "
" + ], + "text/plain": [ + " Python En Math\n", + "K 60 136 62\n", + "I 16 49 97\n", + "F 64 26 109\n", + "D 47 24 145\n", + "B 135 65 135" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.loc[::-2]" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "ename": "TypeError", + "evalue": "Cannot index by location index with a non-integer key", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mdf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0miloc\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'A'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;32md:\\python36\\lib\\site-packages\\pandas\\core\\indexing.py\u001b[0m in \u001b[0;36m__getitem__\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m 1498\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1499\u001b[0m \u001b[0mmaybe_callable\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mapply_if_callable\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mobj\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1500\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_getitem_axis\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmaybe_callable\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 1501\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1502\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0m_is_scalar_access\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32md:\\python36\\lib\\site-packages\\pandas\\core\\indexing.py\u001b[0m in \u001b[0;36m_getitem_axis\u001b[1;34m(self, key, axis)\u001b[0m\n\u001b[0;32m 2224\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2225\u001b[0m \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mis_integer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 2226\u001b[1;33m raise TypeError(\"Cannot index by location index with a \"\n\u001b[0m\u001b[0;32m 2227\u001b[0m \"non-integer key\")\n\u001b[0;32m 2228\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mTypeError\u001b[0m: Cannot index by location index with a non-integer key" + ] + } + ], + "source": [ + "df.iloc['A']" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Python 103\n", + "En 56\n", + "Math 98\n", + "Name: A, dtype: int32" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.iloc[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonEnMath
A1035698
F6426109
\n", + "
" + ], + "text/plain": [ + " Python En Math\n", + "A 103 56 98\n", + "F 64 26 109" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.iloc[[0,5]]" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonEnMath
A1035698
B13565135
C1379146
D4724145
E899716
\n", + "
" + ], + "text/plain": [ + " Python En Math\n", + "A 103 56 98\n", + "B 135 65 135\n", + "C 13 79 146\n", + "D 47 24 145\n", + "E 89 97 16" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.iloc[0:5]" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonEnMath
K6013662
I164997
F6426109
D4724145
B13565135
\n", + "
" + ], + "text/plain": [ + " Python En Math\n", + "K 60 136 62\n", + "I 16 49 97\n", + "F 64 26 109\n", + "D 47 24 145\n", + "B 135 65 135" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.iloc[::-2]" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonEnMath
A1035698
B13565135
C1379146
D4724145
E899716
F6426109
H4846111
I164997
J122126100
K6013662
\n", + "
" + ], + "text/plain": [ + " Python En Math\n", + "A 103 56 98\n", + "B 135 65 135\n", + "C 13 79 146\n", + "D 47 24 145\n", + "E 89 97 16\n", + "F 64 26 109\n", + "H 48 46 111\n", + "I 16 49 97\n", + "J 122 126 100\n", + "K 60 136 62" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
EnMath
A5698
C79146
E9716
H46111
J126100
\n", + "
" + ], + "text/plain": [ + " En Math\n", + "A 56 98\n", + "C 79 146\n", + "E 97 16\n", + "H 46 111\n", + "J 126 100" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.iloc[::2,1:]" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Day76-90/code/3-pandas数据清洗之空数据.ipynb b/Day76-90/code/3-pandas数据清洗之空数据.ipynb new file mode 100644 index 0000000..dde9ec6 --- /dev/null +++ b/Day76-90/code/3-pandas数据清洗之空数据.ipynb @@ -0,0 +1,6285 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "\n", + "import pandas as pd\n", + "\n", + "from pandas import Series,DataFrame" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonEnMathPhysicChem
1001221052857
101741291611426
102971211222965
103141731201471
1041261328611617
1058534212166
10614265112483
10713614112286113
1081537124110102
1096330446958
110593811310916
1115518758126
1125397763745
1134214810797143
114701386968134
115471361132294
1163113762028
117148741344124
1181028113812832
11927111137022
1202893121684
12113643259719
12211170123858
12396103147868
12410104663149
1257759710831
12688614511655
12733741065046
12874282610076
1297618101126133
..................
170144124779282
1713698484380
17251143683474
17314911718141120
1748139146112122
17511510164629
17610714045148
17765436810918
1783110011049123
1792946695790
18014686182246
18171504090140
1824100147116110
1835587937834
18451091248782
185101181395051
18632127136124
187941613813149
1886510112312886
18943941029132
190681359428125
1913060982715
1928916101354
193104139972917
194529419991
195191021354140
19658100708264
19784971297613
19813115744114
199793795128116
\n", + "

100 rows × 5 columns

\n", + "
" + ], + "text/plain": [ + " Python En Math Physic Chem\n", + "100 122 10 5 28 57\n", + "101 74 129 16 114 26\n", + "102 97 121 122 29 65\n", + "103 141 73 120 147 1\n", + "104 126 132 86 116 17\n", + "105 85 3 42 121 66\n", + "106 142 65 1 124 83\n", + "107 136 141 122 86 113\n", + "108 15 37 124 110 102\n", + "109 63 30 44 69 58\n", + "110 59 38 113 109 16\n", + "111 5 51 87 58 126\n", + "112 53 97 76 37 45\n", + "113 42 148 107 97 143\n", + "114 70 138 69 68 134\n", + "115 47 136 113 22 94\n", + "116 31 137 6 20 28\n", + "117 148 74 134 4 124\n", + "118 102 81 138 128 32\n", + "119 27 111 13 70 22\n", + "120 28 93 121 68 4\n", + "121 136 43 25 97 19\n", + "122 111 70 12 38 58\n", + "123 96 103 147 86 8\n", + "124 10 10 46 63 149\n", + "125 7 75 97 108 31\n", + "126 88 6 145 116 55\n", + "127 33 74 106 50 46\n", + "128 74 28 26 100 76\n", + "129 76 18 101 126 133\n", + ".. ... ... ... ... ...\n", + "170 144 124 77 92 82\n", + "171 36 98 48 43 80\n", + "172 51 143 68 34 74\n", + "173 149 117 18 141 120\n", + "174 8 139 146 112 122\n", + "175 115 101 64 62 9\n", + "176 10 7 140 45 148\n", + "177 65 43 68 109 18\n", + "178 31 100 110 49 123\n", + "179 29 46 69 57 90\n", + "180 146 86 18 22 46\n", + "181 71 50 40 90 140\n", + "182 4 100 147 116 110\n", + "183 55 87 93 78 34\n", + "184 5 109 124 87 82\n", + "185 10 118 139 50 51\n", + "186 32 12 71 36 124\n", + "187 94 16 138 13 149\n", + "188 65 101 123 128 86\n", + "189 43 94 10 29 132\n", + "190 68 135 94 28 125\n", + "191 30 60 98 27 15\n", + "192 89 16 10 135 4\n", + "193 104 139 97 29 17\n", + "194 5 29 41 99 91\n", + "195 19 102 135 41 40\n", + "196 58 100 70 82 64\n", + "197 84 97 129 76 13\n", + "198 131 15 7 44 114\n", + "199 79 37 95 128 116\n", + "\n", + "[100 rows x 5 columns]" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df = DataFrame(np.random.randint(0,150,size = (100,5)),index = np.arange(100,200),columns=['Python','En','Math','Physic','Chem'])\n", + "df" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Python False\n", + "En False\n", + "Math False\n", + "Physic False\n", + "Chem False\n", + "dtype: bool" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 判断DataFrame是否存在空数据\n", + "df.isnull().any()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Python True\n", + "En True\n", + "Math True\n", + "Physic True\n", + "Chem True\n", + "dtype: bool" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.notnull().all()" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "500" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "100*5" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "for i in range(50):\n", + " # 行索引\n", + " index = np.random.randint(100,200,size =1)[0]\n", + "\n", + " cols = df.columns\n", + "\n", + " # 列索引\n", + " col = np.random.choice(cols)\n", + "\n", + " df.loc[index,col] = None" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "for i in range(20):\n", + " # 行索引\n", + " index = np.random.randint(100,200,size =1)[0]\n", + "\n", + " cols = df.columns\n", + "\n", + " # 列索引\n", + " col = np.random.choice(cols)\n", + "\n", + "# not a number 不是一个数\n", + " df.loc[index,col] = np.NAN" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonEnMathPhysicChem
100122.010.05.028.057.0
101NaN129.016.0114.026.0
10297.0121.0122.029.065.0
103141.073.0120.0147.01.0
104126.0NaN86.0116.017.0
10585.0NaN42.0121.066.0
106142.065.01.0124.083.0
107136.0141.0NaN86.0113.0
10815.037.0124.0110.0102.0
10963.030.0NaN69.058.0
110NaNNaN113.0109.016.0
1115.051.087.058.0126.0
11253.097.076.037.045.0
11342.0148.0NaN97.0NaN
11470.0138.069.068.0134.0
115NaN136.0113.022.094.0
11631.0137.06.020.028.0
117148.074.0134.04.0124.0
118102.081.0138.0128.032.0
11927.0111.013.0NaN22.0
12028.093.0121.0NaN4.0
121136.0NaN25.097.019.0
122111.070.012.038.058.0
123NaN103.0147.086.08.0
12410.010.046.063.0149.0
1257.075.097.0108.031.0
12688.06.0NaNNaN55.0
12733.074.0106.050.046.0
12874.028.026.0100.076.0
12976.018.0101.0NaNNaN
..................
170144.0124.077.092.082.0
17136.098.0NaN43.080.0
17251.0NaN68.034.074.0
173149.0NaN18.0141.0NaN
1748.0139.0146.0112.0NaN
175115.0NaN64.062.09.0
176NaN7.0140.045.0148.0
177NaN43.068.0109.018.0
17831.0100.0NaN49.0123.0
17929.046.069.057.090.0
180146.086.018.022.046.0
18171.050.040.0NaN140.0
1824.0100.0147.0116.0110.0
18355.087.093.0NaN34.0
184NaN109.0124.087.082.0
18510.0118.0139.050.051.0
18632.012.071.036.0NaN
18794.0NaN138.013.0149.0
18865.0101.0123.0128.086.0
18943.094.0NaN29.0132.0
19068.0135.094.028.0125.0
19130.060.098.0NaN15.0
19289.016.010.0135.04.0
193104.0139.097.029.017.0
1945.029.041.099.0NaN
19519.0102.0135.041.040.0
19658.0NaN70.082.064.0
197NaN97.0129.076.013.0
198131.015.0NaN44.0114.0
19979.0NaN95.0128.0NaN
\n", + "

100 rows × 5 columns

\n", + "
" + ], + "text/plain": [ + " Python En Math Physic Chem\n", + "100 122.0 10.0 5.0 28.0 57.0\n", + "101 NaN 129.0 16.0 114.0 26.0\n", + "102 97.0 121.0 122.0 29.0 65.0\n", + "103 141.0 73.0 120.0 147.0 1.0\n", + "104 126.0 NaN 86.0 116.0 17.0\n", + "105 85.0 NaN 42.0 121.0 66.0\n", + "106 142.0 65.0 1.0 124.0 83.0\n", + "107 136.0 141.0 NaN 86.0 113.0\n", + "108 15.0 37.0 124.0 110.0 102.0\n", + "109 63.0 30.0 NaN 69.0 58.0\n", + "110 NaN NaN 113.0 109.0 16.0\n", + "111 5.0 51.0 87.0 58.0 126.0\n", + "112 53.0 97.0 76.0 37.0 45.0\n", + "113 42.0 148.0 NaN 97.0 NaN\n", + "114 70.0 138.0 69.0 68.0 134.0\n", + "115 NaN 136.0 113.0 22.0 94.0\n", + "116 31.0 137.0 6.0 20.0 28.0\n", + "117 148.0 74.0 134.0 4.0 124.0\n", + "118 102.0 81.0 138.0 128.0 32.0\n", + "119 27.0 111.0 13.0 NaN 22.0\n", + "120 28.0 93.0 121.0 NaN 4.0\n", + "121 136.0 NaN 25.0 97.0 19.0\n", + "122 111.0 70.0 12.0 38.0 58.0\n", + "123 NaN 103.0 147.0 86.0 8.0\n", + "124 10.0 10.0 46.0 63.0 149.0\n", + "125 7.0 75.0 97.0 108.0 31.0\n", + "126 88.0 6.0 NaN NaN 55.0\n", + "127 33.0 74.0 106.0 50.0 46.0\n", + "128 74.0 28.0 26.0 100.0 76.0\n", + "129 76.0 18.0 101.0 NaN NaN\n", + ".. ... ... ... ... ...\n", + "170 144.0 124.0 77.0 92.0 82.0\n", + "171 36.0 98.0 NaN 43.0 80.0\n", + "172 51.0 NaN 68.0 34.0 74.0\n", + "173 149.0 NaN 18.0 141.0 NaN\n", + "174 8.0 139.0 146.0 112.0 NaN\n", + "175 115.0 NaN 64.0 62.0 9.0\n", + "176 NaN 7.0 140.0 45.0 148.0\n", + "177 NaN 43.0 68.0 109.0 18.0\n", + "178 31.0 100.0 NaN 49.0 123.0\n", + "179 29.0 46.0 69.0 57.0 90.0\n", + "180 146.0 86.0 18.0 22.0 46.0\n", + "181 71.0 50.0 40.0 NaN 140.0\n", + "182 4.0 100.0 147.0 116.0 110.0\n", + "183 55.0 87.0 93.0 NaN 34.0\n", + "184 NaN 109.0 124.0 87.0 82.0\n", + "185 10.0 118.0 139.0 50.0 51.0\n", + "186 32.0 12.0 71.0 36.0 NaN\n", + "187 94.0 NaN 138.0 13.0 149.0\n", + "188 65.0 101.0 123.0 128.0 86.0\n", + "189 43.0 94.0 NaN 29.0 132.0\n", + "190 68.0 135.0 94.0 28.0 125.0\n", + "191 30.0 60.0 98.0 NaN 15.0\n", + "192 89.0 16.0 10.0 135.0 4.0\n", + "193 104.0 139.0 97.0 29.0 17.0\n", + "194 5.0 29.0 41.0 99.0 NaN\n", + "195 19.0 102.0 135.0 41.0 40.0\n", + "196 58.0 NaN 70.0 82.0 64.0\n", + "197 NaN 97.0 129.0 76.0 13.0\n", + "198 131.0 15.0 NaN 44.0 114.0\n", + "199 79.0 NaN 95.0 128.0 NaN\n", + "\n", + "[100 rows x 5 columns]" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Python True\n", + "En True\n", + "Math True\n", + "Physic True\n", + "Chem True\n", + "dtype: bool" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.isnull().any()" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Python 14\n", + "En 14\n", + "Math 15\n", + "Physic 11\n", + "Chem 13\n", + "dtype: int64" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.isnull().sum()" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [], + "source": [ + "df2 = df.copy()" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Python 14\n", + "En 14\n", + "Math 15\n", + "Physic 11\n", + "Chem 13\n", + "dtype: int64" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df2.isnull().sum()" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonEnMathPhysicChem
100122.010.05.028.057.0
101100.0129.016.0114.026.0
10297.0121.0122.029.065.0
103141.073.0120.0147.01.0
104126.0100.086.0116.017.0
10585.0100.042.0121.066.0
106142.065.01.0124.083.0
107136.0141.0100.086.0113.0
10815.037.0124.0110.0102.0
10963.030.0100.069.058.0
110100.0100.0113.0109.016.0
1115.051.087.058.0126.0
11253.097.076.037.045.0
11342.0148.0100.097.0100.0
11470.0138.069.068.0134.0
115100.0136.0113.022.094.0
11631.0137.06.020.028.0
117148.074.0134.04.0124.0
118102.081.0138.0128.032.0
11927.0111.013.0100.022.0
12028.093.0121.0100.04.0
121136.0100.025.097.019.0
122111.070.012.038.058.0
123100.0103.0147.086.08.0
12410.010.046.063.0149.0
1257.075.097.0108.031.0
12688.06.0100.0100.055.0
12733.074.0106.050.046.0
12874.028.026.0100.076.0
12976.018.0101.0100.0100.0
..................
170144.0124.077.092.082.0
17136.098.0100.043.080.0
17251.0100.068.034.074.0
173149.0100.018.0141.0100.0
1748.0139.0146.0112.0100.0
175115.0100.064.062.09.0
176100.07.0140.045.0148.0
177100.043.068.0109.018.0
17831.0100.0100.049.0123.0
17929.046.069.057.090.0
180146.086.018.022.046.0
18171.050.040.0100.0140.0
1824.0100.0147.0116.0110.0
18355.087.093.0100.034.0
184100.0109.0124.087.082.0
18510.0118.0139.050.051.0
18632.012.071.036.0100.0
18794.0100.0138.013.0149.0
18865.0101.0123.0128.086.0
18943.094.0100.029.0132.0
19068.0135.094.028.0125.0
19130.060.098.0100.015.0
19289.016.010.0135.04.0
193104.0139.097.029.017.0
1945.029.041.099.0100.0
19519.0102.0135.041.040.0
19658.0100.070.082.064.0
197100.097.0129.076.013.0
198131.015.0100.044.0114.0
19979.0100.095.0128.0100.0
\n", + "

100 rows × 5 columns

\n", + "
" + ], + "text/plain": [ + " Python En Math Physic Chem\n", + "100 122.0 10.0 5.0 28.0 57.0\n", + "101 100.0 129.0 16.0 114.0 26.0\n", + "102 97.0 121.0 122.0 29.0 65.0\n", + "103 141.0 73.0 120.0 147.0 1.0\n", + "104 126.0 100.0 86.0 116.0 17.0\n", + "105 85.0 100.0 42.0 121.0 66.0\n", + "106 142.0 65.0 1.0 124.0 83.0\n", + "107 136.0 141.0 100.0 86.0 113.0\n", + "108 15.0 37.0 124.0 110.0 102.0\n", + "109 63.0 30.0 100.0 69.0 58.0\n", + "110 100.0 100.0 113.0 109.0 16.0\n", + "111 5.0 51.0 87.0 58.0 126.0\n", + "112 53.0 97.0 76.0 37.0 45.0\n", + "113 42.0 148.0 100.0 97.0 100.0\n", + "114 70.0 138.0 69.0 68.0 134.0\n", + "115 100.0 136.0 113.0 22.0 94.0\n", + "116 31.0 137.0 6.0 20.0 28.0\n", + "117 148.0 74.0 134.0 4.0 124.0\n", + "118 102.0 81.0 138.0 128.0 32.0\n", + "119 27.0 111.0 13.0 100.0 22.0\n", + "120 28.0 93.0 121.0 100.0 4.0\n", + "121 136.0 100.0 25.0 97.0 19.0\n", + "122 111.0 70.0 12.0 38.0 58.0\n", + "123 100.0 103.0 147.0 86.0 8.0\n", + "124 10.0 10.0 46.0 63.0 149.0\n", + "125 7.0 75.0 97.0 108.0 31.0\n", + "126 88.0 6.0 100.0 100.0 55.0\n", + "127 33.0 74.0 106.0 50.0 46.0\n", + "128 74.0 28.0 26.0 100.0 76.0\n", + "129 76.0 18.0 101.0 100.0 100.0\n", + ".. ... ... ... ... ...\n", + "170 144.0 124.0 77.0 92.0 82.0\n", + "171 36.0 98.0 100.0 43.0 80.0\n", + "172 51.0 100.0 68.0 34.0 74.0\n", + "173 149.0 100.0 18.0 141.0 100.0\n", + "174 8.0 139.0 146.0 112.0 100.0\n", + "175 115.0 100.0 64.0 62.0 9.0\n", + "176 100.0 7.0 140.0 45.0 148.0\n", + "177 100.0 43.0 68.0 109.0 18.0\n", + "178 31.0 100.0 100.0 49.0 123.0\n", + "179 29.0 46.0 69.0 57.0 90.0\n", + "180 146.0 86.0 18.0 22.0 46.0\n", + "181 71.0 50.0 40.0 100.0 140.0\n", + "182 4.0 100.0 147.0 116.0 110.0\n", + "183 55.0 87.0 93.0 100.0 34.0\n", + "184 100.0 109.0 124.0 87.0 82.0\n", + "185 10.0 118.0 139.0 50.0 51.0\n", + "186 32.0 12.0 71.0 36.0 100.0\n", + "187 94.0 100.0 138.0 13.0 149.0\n", + "188 65.0 101.0 123.0 128.0 86.0\n", + "189 43.0 94.0 100.0 29.0 132.0\n", + "190 68.0 135.0 94.0 28.0 125.0\n", + "191 30.0 60.0 98.0 100.0 15.0\n", + "192 89.0 16.0 10.0 135.0 4.0\n", + "193 104.0 139.0 97.0 29.0 17.0\n", + "194 5.0 29.0 41.0 99.0 100.0\n", + "195 19.0 102.0 135.0 41.0 40.0\n", + "196 58.0 100.0 70.0 82.0 64.0\n", + "197 100.0 97.0 129.0 76.0 13.0\n", + "198 131.0 15.0 100.0 44.0 114.0\n", + "199 79.0 100.0 95.0 128.0 100.0\n", + "\n", + "[100 rows x 5 columns]" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 固定值填充\n", + "df2.fillna(value=100)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Python 71.662791\n", + "En 75.627907\n", + "Math 77.929412\n", + "Physic 73.471910\n", + "Chem 69.080460\n", + "dtype: float64" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df2.mean()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonEnMathPhysicChem
1001221052857
101711291611426
102971211222965
103141731201471
104126758611617
10585754212166
10614265112483
1071361417786113
1081537124110102
1096330776958
110717511310916
1115518758126
1125397763745
11342148779769
114701386968134
115711361132294
1163113762028
117148741344124
1181028113812832
11927111137322
1202893121734
12113675259719
12211170123858
12371103147868
12410104663149
1257759710831
126886777355
12733741065046
12874282610076
12976181017369
..................
170144124779282
1713698774380
1725175683474
173149751814169
174813914611269
1751157564629
17671714045148
17771436810918
178311007749123
1792946695790
18014686182246
18171504073140
1824100147116110
1835587937334
184711091248782
185101181395051
1863212713669
187947513813149
1886510112312886
18943947729132
190681359428125
1913060987315
1928916101354
193104139972917
194529419969
195191021354140
1965875708264
19771971297613
198131157744114
19979759512869
\n", + "

100 rows × 5 columns

\n", + "
" + ], + "text/plain": [ + " Python En Math Physic Chem\n", + "100 122 10 5 28 57\n", + "101 71 129 16 114 26\n", + "102 97 121 122 29 65\n", + "103 141 73 120 147 1\n", + "104 126 75 86 116 17\n", + "105 85 75 42 121 66\n", + "106 142 65 1 124 83\n", + "107 136 141 77 86 113\n", + "108 15 37 124 110 102\n", + "109 63 30 77 69 58\n", + "110 71 75 113 109 16\n", + "111 5 51 87 58 126\n", + "112 53 97 76 37 45\n", + "113 42 148 77 97 69\n", + "114 70 138 69 68 134\n", + "115 71 136 113 22 94\n", + "116 31 137 6 20 28\n", + "117 148 74 134 4 124\n", + "118 102 81 138 128 32\n", + "119 27 111 13 73 22\n", + "120 28 93 121 73 4\n", + "121 136 75 25 97 19\n", + "122 111 70 12 38 58\n", + "123 71 103 147 86 8\n", + "124 10 10 46 63 149\n", + "125 7 75 97 108 31\n", + "126 88 6 77 73 55\n", + "127 33 74 106 50 46\n", + "128 74 28 26 100 76\n", + "129 76 18 101 73 69\n", + ".. ... ... ... ... ...\n", + "170 144 124 77 92 82\n", + "171 36 98 77 43 80\n", + "172 51 75 68 34 74\n", + "173 149 75 18 141 69\n", + "174 8 139 146 112 69\n", + "175 115 75 64 62 9\n", + "176 71 7 140 45 148\n", + "177 71 43 68 109 18\n", + "178 31 100 77 49 123\n", + "179 29 46 69 57 90\n", + "180 146 86 18 22 46\n", + "181 71 50 40 73 140\n", + "182 4 100 147 116 110\n", + "183 55 87 93 73 34\n", + "184 71 109 124 87 82\n", + "185 10 118 139 50 51\n", + "186 32 12 71 36 69\n", + "187 94 75 138 13 149\n", + "188 65 101 123 128 86\n", + "189 43 94 77 29 132\n", + "190 68 135 94 28 125\n", + "191 30 60 98 73 15\n", + "192 89 16 10 135 4\n", + "193 104 139 97 29 17\n", + "194 5 29 41 99 69\n", + "195 19 102 135 41 40\n", + "196 58 75 70 82 64\n", + "197 71 97 129 76 13\n", + "198 131 15 77 44 114\n", + "199 79 75 95 128 69\n", + "\n", + "[100 rows x 5 columns]" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 均值\n", + "df3 = df2.fillna(value=df2.mean())\n", + "df3.astype(np.int16)" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 6, 18, 1, 17, 19, 5, 17, 16, 13, 3])" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "nd = np.random.randint(0,20,size = 10)\n", + "nd" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 1, 3, 5, 6, 13, 16, 17, 17, 18, 19])" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "nd.sort()\n", + "nd" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "14.5" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(13 + 16)/2" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "14.5" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.median(nd)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonEnMathPhysicChem
100122.010.05.028.057.0
10168.0129.016.0114.026.0
10297.0121.0122.029.065.0
103141.073.0120.0147.01.0
104126.082.586.0116.017.0
10585.082.542.0121.066.0
106142.065.01.0124.083.0
107136.0141.086.086.0113.0
10815.037.0124.0110.0102.0
10963.030.086.069.058.0
11068.082.5113.0109.016.0
1115.051.087.058.0126.0
11253.097.076.037.045.0
11342.0148.086.097.065.0
11470.0138.069.068.0134.0
11568.0136.0113.022.094.0
11631.0137.06.020.028.0
117148.074.0134.04.0124.0
118102.081.0138.0128.032.0
11927.0111.013.069.022.0
12028.093.0121.069.04.0
121136.082.525.097.019.0
122111.070.012.038.058.0
12368.0103.0147.086.08.0
12410.010.046.063.0149.0
1257.075.097.0108.031.0
12688.06.086.069.055.0
12733.074.0106.050.046.0
12874.028.026.0100.076.0
12976.018.0101.069.065.0
..................
170144.0124.077.092.082.0
17136.098.086.043.080.0
17251.082.568.034.074.0
173149.082.518.0141.065.0
1748.0139.0146.0112.065.0
175115.082.564.062.09.0
17668.07.0140.045.0148.0
17768.043.068.0109.018.0
17831.0100.086.049.0123.0
17929.046.069.057.090.0
180146.086.018.022.046.0
18171.050.040.069.0140.0
1824.0100.0147.0116.0110.0
18355.087.093.069.034.0
18468.0109.0124.087.082.0
18510.0118.0139.050.051.0
18632.012.071.036.065.0
18794.082.5138.013.0149.0
18865.0101.0123.0128.086.0
18943.094.086.029.0132.0
19068.0135.094.028.0125.0
19130.060.098.069.015.0
19289.016.010.0135.04.0
193104.0139.097.029.017.0
1945.029.041.099.065.0
19519.0102.0135.041.040.0
19658.082.570.082.064.0
19768.097.0129.076.013.0
198131.015.086.044.0114.0
19979.082.595.0128.065.0
\n", + "

100 rows × 5 columns

\n", + "
" + ], + "text/plain": [ + " Python En Math Physic Chem\n", + "100 122.0 10.0 5.0 28.0 57.0\n", + "101 68.0 129.0 16.0 114.0 26.0\n", + "102 97.0 121.0 122.0 29.0 65.0\n", + "103 141.0 73.0 120.0 147.0 1.0\n", + "104 126.0 82.5 86.0 116.0 17.0\n", + "105 85.0 82.5 42.0 121.0 66.0\n", + "106 142.0 65.0 1.0 124.0 83.0\n", + "107 136.0 141.0 86.0 86.0 113.0\n", + "108 15.0 37.0 124.0 110.0 102.0\n", + "109 63.0 30.0 86.0 69.0 58.0\n", + "110 68.0 82.5 113.0 109.0 16.0\n", + "111 5.0 51.0 87.0 58.0 126.0\n", + "112 53.0 97.0 76.0 37.0 45.0\n", + "113 42.0 148.0 86.0 97.0 65.0\n", + "114 70.0 138.0 69.0 68.0 134.0\n", + "115 68.0 136.0 113.0 22.0 94.0\n", + "116 31.0 137.0 6.0 20.0 28.0\n", + "117 148.0 74.0 134.0 4.0 124.0\n", + "118 102.0 81.0 138.0 128.0 32.0\n", + "119 27.0 111.0 13.0 69.0 22.0\n", + "120 28.0 93.0 121.0 69.0 4.0\n", + "121 136.0 82.5 25.0 97.0 19.0\n", + "122 111.0 70.0 12.0 38.0 58.0\n", + "123 68.0 103.0 147.0 86.0 8.0\n", + "124 10.0 10.0 46.0 63.0 149.0\n", + "125 7.0 75.0 97.0 108.0 31.0\n", + "126 88.0 6.0 86.0 69.0 55.0\n", + "127 33.0 74.0 106.0 50.0 46.0\n", + "128 74.0 28.0 26.0 100.0 76.0\n", + "129 76.0 18.0 101.0 69.0 65.0\n", + ".. ... ... ... ... ...\n", + "170 144.0 124.0 77.0 92.0 82.0\n", + "171 36.0 98.0 86.0 43.0 80.0\n", + "172 51.0 82.5 68.0 34.0 74.0\n", + "173 149.0 82.5 18.0 141.0 65.0\n", + "174 8.0 139.0 146.0 112.0 65.0\n", + "175 115.0 82.5 64.0 62.0 9.0\n", + "176 68.0 7.0 140.0 45.0 148.0\n", + "177 68.0 43.0 68.0 109.0 18.0\n", + "178 31.0 100.0 86.0 49.0 123.0\n", + "179 29.0 46.0 69.0 57.0 90.0\n", + "180 146.0 86.0 18.0 22.0 46.0\n", + "181 71.0 50.0 40.0 69.0 140.0\n", + "182 4.0 100.0 147.0 116.0 110.0\n", + "183 55.0 87.0 93.0 69.0 34.0\n", + "184 68.0 109.0 124.0 87.0 82.0\n", + "185 10.0 118.0 139.0 50.0 51.0\n", + "186 32.0 12.0 71.0 36.0 65.0\n", + "187 94.0 82.5 138.0 13.0 149.0\n", + "188 65.0 101.0 123.0 128.0 86.0\n", + "189 43.0 94.0 86.0 29.0 132.0\n", + "190 68.0 135.0 94.0 28.0 125.0\n", + "191 30.0 60.0 98.0 69.0 15.0\n", + "192 89.0 16.0 10.0 135.0 4.0\n", + "193 104.0 139.0 97.0 29.0 17.0\n", + "194 5.0 29.0 41.0 99.0 65.0\n", + "195 19.0 102.0 135.0 41.0 40.0\n", + "196 58.0 82.5 70.0 82.0 64.0\n", + "197 68.0 97.0 129.0 76.0 13.0\n", + "198 131.0 15.0 86.0 44.0 114.0\n", + "199 79.0 82.5 95.0 128.0 65.0\n", + "\n", + "[100 rows x 5 columns]" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 中位数填充\n", + "df2.median()\n", + "df4 = df2.fillna(df2.median())\n", + "df4" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonEnMathPhysicChem
100122.010.05.028.057.0
101NaN129.016.0114.026.0
10297.0121.0122.029.065.0
103141.073.0120.0147.01.0
104126.0NaN86.0116.017.0
10585.0NaN42.0121.066.0
106142.065.01.0124.083.0
107136.0141.0NaN86.0113.0
10815.037.0124.0110.0102.0
10963.030.0NaN69.058.0
110NaNNaN113.0109.016.0
1115.051.087.058.0126.0
11253.097.076.037.045.0
11342.0148.0NaN97.0NaN
11470.0138.069.068.0134.0
115NaN136.0113.022.094.0
11631.0137.06.020.028.0
117148.074.0134.04.0124.0
118102.081.0138.0128.032.0
11927.0111.013.0NaN22.0
12028.093.0121.0NaN4.0
121136.0NaN25.097.019.0
122111.070.012.038.058.0
123NaN103.0147.086.08.0
12410.010.046.063.0149.0
1257.075.097.0108.031.0
12688.06.0NaNNaN55.0
12733.074.0106.050.046.0
12874.028.026.0100.076.0
12976.018.0101.0NaNNaN
..................
170144.0124.077.092.082.0
17136.098.0NaN43.080.0
17251.0NaN68.034.074.0
173149.0NaN18.0141.0NaN
1748.0139.0146.0112.0NaN
175115.0NaN64.062.09.0
176NaN7.0140.045.0148.0
177NaN43.068.0109.018.0
17831.0100.0NaN49.0123.0
17929.046.069.057.090.0
180146.086.018.022.046.0
18171.050.040.0NaN140.0
1824.0100.0147.0116.0110.0
18355.087.093.0NaN34.0
184NaN109.0124.087.082.0
18510.0118.0139.050.051.0
18632.012.071.036.0NaN
18794.0NaN138.013.0149.0
18865.0101.0123.0128.086.0
18943.094.0NaN29.0132.0
19068.0135.094.028.0125.0
19130.060.098.0NaN15.0
19289.016.010.0135.04.0
193104.0139.097.029.017.0
1945.029.041.099.0NaN
19519.0102.0135.041.040.0
19658.0NaN70.082.064.0
197NaN97.0129.076.013.0
198131.015.0NaN44.0114.0
19979.0NaN95.0128.0NaN
\n", + "

100 rows × 5 columns

\n", + "
" + ], + "text/plain": [ + " Python En Math Physic Chem\n", + "100 122.0 10.0 5.0 28.0 57.0\n", + "101 NaN 129.0 16.0 114.0 26.0\n", + "102 97.0 121.0 122.0 29.0 65.0\n", + "103 141.0 73.0 120.0 147.0 1.0\n", + "104 126.0 NaN 86.0 116.0 17.0\n", + "105 85.0 NaN 42.0 121.0 66.0\n", + "106 142.0 65.0 1.0 124.0 83.0\n", + "107 136.0 141.0 NaN 86.0 113.0\n", + "108 15.0 37.0 124.0 110.0 102.0\n", + "109 63.0 30.0 NaN 69.0 58.0\n", + "110 NaN NaN 113.0 109.0 16.0\n", + "111 5.0 51.0 87.0 58.0 126.0\n", + "112 53.0 97.0 76.0 37.0 45.0\n", + "113 42.0 148.0 NaN 97.0 NaN\n", + "114 70.0 138.0 69.0 68.0 134.0\n", + "115 NaN 136.0 113.0 22.0 94.0\n", + "116 31.0 137.0 6.0 20.0 28.0\n", + "117 148.0 74.0 134.0 4.0 124.0\n", + "118 102.0 81.0 138.0 128.0 32.0\n", + "119 27.0 111.0 13.0 NaN 22.0\n", + "120 28.0 93.0 121.0 NaN 4.0\n", + "121 136.0 NaN 25.0 97.0 19.0\n", + "122 111.0 70.0 12.0 38.0 58.0\n", + "123 NaN 103.0 147.0 86.0 8.0\n", + "124 10.0 10.0 46.0 63.0 149.0\n", + "125 7.0 75.0 97.0 108.0 31.0\n", + "126 88.0 6.0 NaN NaN 55.0\n", + "127 33.0 74.0 106.0 50.0 46.0\n", + "128 74.0 28.0 26.0 100.0 76.0\n", + "129 76.0 18.0 101.0 NaN NaN\n", + ".. ... ... ... ... ...\n", + "170 144.0 124.0 77.0 92.0 82.0\n", + "171 36.0 98.0 NaN 43.0 80.0\n", + "172 51.0 NaN 68.0 34.0 74.0\n", + "173 149.0 NaN 18.0 141.0 NaN\n", + "174 8.0 139.0 146.0 112.0 NaN\n", + "175 115.0 NaN 64.0 62.0 9.0\n", + "176 NaN 7.0 140.0 45.0 148.0\n", + "177 NaN 43.0 68.0 109.0 18.0\n", + "178 31.0 100.0 NaN 49.0 123.0\n", + "179 29.0 46.0 69.0 57.0 90.0\n", + "180 146.0 86.0 18.0 22.0 46.0\n", + "181 71.0 50.0 40.0 NaN 140.0\n", + "182 4.0 100.0 147.0 116.0 110.0\n", + "183 55.0 87.0 93.0 NaN 34.0\n", + "184 NaN 109.0 124.0 87.0 82.0\n", + "185 10.0 118.0 139.0 50.0 51.0\n", + "186 32.0 12.0 71.0 36.0 NaN\n", + "187 94.0 NaN 138.0 13.0 149.0\n", + "188 65.0 101.0 123.0 128.0 86.0\n", + "189 43.0 94.0 NaN 29.0 132.0\n", + "190 68.0 135.0 94.0 28.0 125.0\n", + "191 30.0 60.0 98.0 NaN 15.0\n", + "192 89.0 16.0 10.0 135.0 4.0\n", + "193 104.0 139.0 97.0 29.0 17.0\n", + "194 5.0 29.0 41.0 99.0 NaN\n", + "195 19.0 102.0 135.0 41.0 40.0\n", + "196 58.0 NaN 70.0 82.0 64.0\n", + "197 NaN 97.0 129.0 76.0 13.0\n", + "198 131.0 15.0 NaN 44.0 114.0\n", + "199 79.0 NaN 95.0 128.0 NaN\n", + "\n", + "[100 rows x 5 columns]" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 众数填充,数量最多的那个数\n", + "df2" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonEnMathPhysicChem
100828999101125
101431109325
10256103566190
1034710014713899
1043846827544
10518111223126
106562610614139
10731377567144
10835471026063
109861265788149
11019140303533
11176151133
11231549111969
1136437502321
11472571381521
115551201043225
11696248922146
11763086489
11828461258274
119853970132111
12010990447439
121214810311465
12211029998057
123109888113571
12470103134121121
12551921172743
1266929759105
12765905214822
12841291711913
1292410010728139
..................
207012777241631
2071936192822
20721166154861
207347214011234
2074261081233233
207546130135124113
207633181363820
20771071112954119
207884551293787
20799550451984
2080124746514053
20812635149145127
20821921101389
20838410131714
208428741056889
20852393849788
2086861332612513
208721124401155
20882015353137
208996123123564
20902243927860
20911631176058
20926518131334
209369491094058
2094128461082111
20952659854149
209611147909266
209759773140104
2098102675119
209997197714348
\n", + "

2000 rows × 5 columns

\n", + "
" + ], + "text/plain": [ + " Python En Math Physic Chem\n", + "100 82 89 99 101 125\n", + "101 4 31 109 32 5\n", + "102 56 103 56 61 90\n", + "103 47 100 147 138 99\n", + "104 38 46 82 75 44\n", + "105 18 11 122 3 126\n", + "106 56 26 106 14 139\n", + "107 3 137 75 67 144\n", + "108 35 47 102 60 63\n", + "109 86 126 57 88 149\n", + "110 19 140 30 35 33\n", + "111 76 1 5 11 33\n", + "112 31 54 91 119 69\n", + "113 64 37 50 23 21\n", + "114 72 57 138 15 21\n", + "115 55 120 104 32 25\n", + "116 96 24 89 22 146\n", + "117 63 0 8 64 89\n", + "118 28 46 125 82 74\n", + "119 85 39 70 132 111\n", + "120 109 90 44 74 39\n", + "121 2 148 103 114 65\n", + "122 110 29 99 80 57\n", + "123 109 88 81 135 71\n", + "124 70 103 134 121 121\n", + "125 51 92 117 27 43\n", + "126 6 92 97 59 105\n", + "127 65 90 52 148 22\n", + "128 4 129 17 119 13\n", + "129 24 100 107 28 139\n", + "... ... ... ... ... ...\n", + "2070 127 77 24 16 31\n", + "2071 93 61 9 28 22\n", + "2072 116 61 54 8 61\n", + "2073 4 72 140 112 34\n", + "2074 26 108 123 32 33\n", + "2075 46 130 135 124 113\n", + "2076 33 18 136 38 20\n", + "2077 107 11 129 54 119\n", + "2078 84 55 129 37 87\n", + "2079 95 50 45 19 84\n", + "2080 124 74 65 140 53\n", + "2081 26 35 149 145 127\n", + "2082 19 21 101 3 89\n", + "2083 84 10 131 71 4\n", + "2084 28 74 105 68 89\n", + "2085 23 93 84 97 88\n", + "2086 86 133 26 125 13\n", + "2087 21 124 40 115 5\n", + "2088 20 15 35 31 37\n", + "2089 96 123 123 5 64\n", + "2090 22 43 92 78 60\n", + "2091 16 31 17 60 58\n", + "2092 65 18 13 13 34\n", + "2093 69 49 109 40 58\n", + "2094 128 46 10 82 111\n", + "2095 26 59 8 54 149\n", + "2096 111 47 90 92 66\n", + "2097 5 97 73 140 104\n", + "2098 102 6 7 5 119\n", + "2099 97 19 77 143 48\n", + "\n", + "[2000 rows x 5 columns]" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df = DataFrame(np.random.randint(0,150,size = (2000,5)),index = np.arange(100,2100),columns=['Python','En','Math','Physic','Chem'])\n", + "df" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [], + "source": [ + "for i in range(1000):\n", + " # 行索引\n", + " index = np.random.randint(100,2100,size =1)[0]\n", + "\n", + " cols = df.columns\n", + "\n", + " # 列索引\n", + " col = np.random.choice(cols)\n", + "\n", + " df.loc[index,col] = None" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Python 190\n", + "En 200\n", + "Math 194\n", + "Physic 189\n", + "Chem 181\n", + "dtype: int64" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.isnull().sum()" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonEnMathPhysicChem
10082.089.099.0101.0125.0
1014.031.0109.032.05.0
10256.0103.056.0NaN90.0
10347.0100.0147.0138.099.0
10438.046.0NaN75.044.0
\n", + "
" + ], + "text/plain": [ + " Python En Math Physic Chem\n", + "100 82.0 89.0 99.0 101.0 125.0\n", + "101 4.0 31.0 109.0 32.0 5.0\n", + "102 56.0 103.0 56.0 NaN 90.0\n", + "103 47.0 100.0 147.0 138.0 99.0\n", + "104 38.0 46.0 NaN 75.0 44.0" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonEnMathPhysicChem
209526.059.08.054.0149.0
2096NaN47.090.092.066.0
20975.097.073.0140.0104.0
2098102.06.07.05.0119.0
209997.019.077.0NaN48.0
\n", + "
" + ], + "text/plain": [ + " Python En Math Physic Chem\n", + "2095 26.0 59.0 8.0 54.0 149.0\n", + "2096 NaN 47.0 90.0 92.0 66.0\n", + "2097 5.0 97.0 73.0 140.0 104.0\n", + "2098 102.0 6.0 7.0 5.0 119.0\n", + "2099 97.0 19.0 77.0 NaN 48.0" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.tail()" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 82., 4., 56., 47., 38., 18., 3., 35., 86., 19., 76.,\n", + " 31., 64., 72., 55., 96., 63., 28., 85., 109., 2., 110.,\n", + " 70., 51., 6., 65., 24., 48., 44., 11., 114., 129., 87.,\n", + " 108., 125., nan, 140., 132., 91., 34., 54., 30., 12., 98.,\n", + " 142., 79., 13., 77., 40., 139., 39., 81., 112., 36., 22.,\n", + " 5., 120., 17., 127., 119., 59., 146., 89., 103., 8., 97.,\n", + " 130., 73., 83., 122., 95., 100., 41., 21., 136., 80., 101.,\n", + " 50., 27., 71., 16., 141., 126., 102., 145., 15., 52., 94.,\n", + " 10., 33., 137., 9., 128., 88., 26., 84., 93., 1., 7.,\n", + " 131., 107., 148., 0., 105., 66., 32., 115., 118., 58., 53.,\n", + " 29., 42., 57., 62., 25., 60., 69., 133., 68., 20., 106.,\n", + " 147., 78., 90., 124., 149., 92., 75., 117., 143., 99., 37.,\n", + " 123., 45., 61., 121., 135., 138., 116., 14., 104., 74., 46.,\n", + " 111., 23., 43., 49., 144., 113., 67., 134.])" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 去重之后的数据\n", + "df['Python'].unique()" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "143.0 20\n", + "136.0 20\n", + "102.0 19\n", + "105.0 19\n", + "26.0 19\n", + "69.0 19\n", + "31.0 18\n", + "148.0 18\n", + "75.0 18\n", + "139.0 18\n", + "1.0 18\n", + "35.0 17\n", + "140.0 17\n", + "110.0 17\n", + "125.0 17\n", + "146.0 17\n", + "141.0 17\n", + "64.0 16\n", + "30.0 16\n", + "79.0 16\n", + "73.0 16\n", + "40.0 16\n", + "10.0 15\n", + "6.0 15\n", + "65.0 15\n", + "81.0 15\n", + "28.0 15\n", + "48.0 15\n", + "92.0 15\n", + "103.0 15\n", + " ..\n", + "104.0 9\n", + "12.0 9\n", + "116.0 9\n", + "13.0 9\n", + "59.0 9\n", + "93.0 9\n", + "124.0 9\n", + "85.0 8\n", + "135.0 8\n", + "131.0 8\n", + "68.0 8\n", + "66.0 8\n", + "62.0 8\n", + "120.0 8\n", + "17.0 8\n", + "25.0 8\n", + "145.0 7\n", + "58.0 7\n", + "134.0 7\n", + "113.0 7\n", + "123.0 7\n", + "39.0 7\n", + "34.0 7\n", + "43.0 7\n", + "74.0 6\n", + "144.0 6\n", + "132.0 6\n", + "142.0 5\n", + "67.0 5\n", + "49.0 5\n", + "Name: Python, Length: 150, dtype: int64" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df['Python'].value_counts()" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "8.0 21\n", + "96.0 19\n", + "118.0 19\n", + "24.0 19\n", + "43.0 19\n", + "27.0 19\n", + "19.0 19\n", + "41.0 18\n", + "0.0 18\n", + "3.0 18\n", + "52.0 18\n", + "4.0 17\n", + "137.0 17\n", + "1.0 17\n", + "101.0 17\n", + "51.0 17\n", + "39.0 17\n", + "100.0 17\n", + "127.0 17\n", + "115.0 16\n", + "33.0 16\n", + "112.0 16\n", + "92.0 16\n", + "126.0 16\n", + "133.0 15\n", + "32.0 15\n", + "89.0 15\n", + "95.0 15\n", + "36.0 15\n", + "93.0 15\n", + " ..\n", + "12.0 9\n", + "28.0 9\n", + "106.0 9\n", + "45.0 9\n", + "80.0 9\n", + "84.0 9\n", + "58.0 9\n", + "79.0 9\n", + "71.0 9\n", + "83.0 9\n", + "142.0 9\n", + "7.0 9\n", + "6.0 8\n", + "61.0 8\n", + "149.0 8\n", + "34.0 8\n", + "20.0 8\n", + "38.0 8\n", + "130.0 8\n", + "104.0 7\n", + "120.0 7\n", + "56.0 7\n", + "146.0 7\n", + "98.0 7\n", + "134.0 6\n", + "123.0 6\n", + "35.0 6\n", + "87.0 5\n", + "42.0 5\n", + "119.0 4\n", + "Name: En, Length: 150, dtype: int64" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "en = df['En'].value_counts()\n", + "en" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "8.0" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "en.index[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Python 75.0\n", + "En 74.0\n", + "Math 77.5\n", + "Physic 73.0\n", + "Chem 72.0\n", + "dtype: float64 \n" + ] + } + ], + "source": [ + "s = df.median()\n", + "print(s,type(s))" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [], + "source": [ + "zhongshu = []\n", + "for col in df.columns:\n", + " zhongshu.append(df[col].value_counts().index[0])" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Python 143.0\n", + "En 8.0\n", + "Math 80.0\n", + "Physic 31.0\n", + "Chem 125.0\n", + "dtype: float64" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s = Series(zhongshu,index = df.columns)\n", + "s" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonEnMathPhysicChem
10082.089.099.0101.0125.0
1014.031.0109.032.05.0
10256.0103.056.031.090.0
10347.0100.0147.0138.099.0
10438.046.080.075.044.0
10518.011.0122.03.0126.0
10656.026.0106.014.0139.0
1073.0137.075.067.0144.0
10835.047.0102.060.063.0
10986.0126.080.088.0149.0
11019.0140.080.035.033.0
11176.08.05.011.033.0
11231.054.091.0119.069.0
11364.037.050.023.021.0
11472.057.0138.015.021.0
11555.0120.0104.032.025.0
11696.024.089.031.0146.0
11763.08.08.064.089.0
11828.08.0125.082.074.0
11985.039.070.0132.0111.0
120109.090.080.074.039.0
1212.08.0103.0114.065.0
122110.029.099.080.057.0
123109.088.081.0135.071.0
12470.0103.0134.0121.0121.0
12551.092.0117.031.043.0
1266.092.097.059.0105.0
12765.090.052.0148.022.0
1284.0129.017.0119.013.0
12924.0100.0107.028.0139.0
..................
2070127.077.024.016.0125.0
207193.061.09.028.022.0
2072116.061.054.08.061.0
20734.072.0140.031.034.0
2074143.0108.0123.032.033.0
207546.08.0135.0124.0113.0
2076143.018.0136.038.0125.0
2077143.011.0129.054.0119.0
207884.055.0129.037.087.0
207995.050.045.019.084.0
2080124.074.065.031.053.0
208126.035.0149.0145.0127.0
208219.021.0101.03.089.0
208384.08.0131.071.04.0
208428.074.0105.068.089.0
208523.093.084.097.088.0
208686.0133.026.0125.013.0
208721.0124.040.031.05.0
208820.015.035.031.037.0
208996.0123.0123.05.064.0
209022.043.092.078.060.0
209116.031.017.060.058.0
209265.018.013.013.034.0
209369.049.0109.040.058.0
2094128.046.010.082.0111.0
209526.059.08.054.0149.0
2096143.047.090.092.066.0
20975.097.073.0140.0104.0
2098102.06.07.05.0119.0
209997.019.077.031.048.0
\n", + "

2000 rows × 5 columns

\n", + "
" + ], + "text/plain": [ + " Python En Math Physic Chem\n", + "100 82.0 89.0 99.0 101.0 125.0\n", + "101 4.0 31.0 109.0 32.0 5.0\n", + "102 56.0 103.0 56.0 31.0 90.0\n", + "103 47.0 100.0 147.0 138.0 99.0\n", + "104 38.0 46.0 80.0 75.0 44.0\n", + "105 18.0 11.0 122.0 3.0 126.0\n", + "106 56.0 26.0 106.0 14.0 139.0\n", + "107 3.0 137.0 75.0 67.0 144.0\n", + "108 35.0 47.0 102.0 60.0 63.0\n", + "109 86.0 126.0 80.0 88.0 149.0\n", + "110 19.0 140.0 80.0 35.0 33.0\n", + "111 76.0 8.0 5.0 11.0 33.0\n", + "112 31.0 54.0 91.0 119.0 69.0\n", + "113 64.0 37.0 50.0 23.0 21.0\n", + "114 72.0 57.0 138.0 15.0 21.0\n", + "115 55.0 120.0 104.0 32.0 25.0\n", + "116 96.0 24.0 89.0 31.0 146.0\n", + "117 63.0 8.0 8.0 64.0 89.0\n", + "118 28.0 8.0 125.0 82.0 74.0\n", + "119 85.0 39.0 70.0 132.0 111.0\n", + "120 109.0 90.0 80.0 74.0 39.0\n", + "121 2.0 8.0 103.0 114.0 65.0\n", + "122 110.0 29.0 99.0 80.0 57.0\n", + "123 109.0 88.0 81.0 135.0 71.0\n", + "124 70.0 103.0 134.0 121.0 121.0\n", + "125 51.0 92.0 117.0 31.0 43.0\n", + "126 6.0 92.0 97.0 59.0 105.0\n", + "127 65.0 90.0 52.0 148.0 22.0\n", + "128 4.0 129.0 17.0 119.0 13.0\n", + "129 24.0 100.0 107.0 28.0 139.0\n", + "... ... ... ... ... ...\n", + "2070 127.0 77.0 24.0 16.0 125.0\n", + "2071 93.0 61.0 9.0 28.0 22.0\n", + "2072 116.0 61.0 54.0 8.0 61.0\n", + "2073 4.0 72.0 140.0 31.0 34.0\n", + "2074 143.0 108.0 123.0 32.0 33.0\n", + "2075 46.0 8.0 135.0 124.0 113.0\n", + "2076 143.0 18.0 136.0 38.0 125.0\n", + "2077 143.0 11.0 129.0 54.0 119.0\n", + "2078 84.0 55.0 129.0 37.0 87.0\n", + "2079 95.0 50.0 45.0 19.0 84.0\n", + "2080 124.0 74.0 65.0 31.0 53.0\n", + "2081 26.0 35.0 149.0 145.0 127.0\n", + "2082 19.0 21.0 101.0 3.0 89.0\n", + "2083 84.0 8.0 131.0 71.0 4.0\n", + "2084 28.0 74.0 105.0 68.0 89.0\n", + "2085 23.0 93.0 84.0 97.0 88.0\n", + "2086 86.0 133.0 26.0 125.0 13.0\n", + "2087 21.0 124.0 40.0 31.0 5.0\n", + "2088 20.0 15.0 35.0 31.0 37.0\n", + "2089 96.0 123.0 123.0 5.0 64.0\n", + "2090 22.0 43.0 92.0 78.0 60.0\n", + "2091 16.0 31.0 17.0 60.0 58.0\n", + "2092 65.0 18.0 13.0 13.0 34.0\n", + "2093 69.0 49.0 109.0 40.0 58.0\n", + "2094 128.0 46.0 10.0 82.0 111.0\n", + "2095 26.0 59.0 8.0 54.0 149.0\n", + "2096 143.0 47.0 90.0 92.0 66.0\n", + "2097 5.0 97.0 73.0 140.0 104.0\n", + "2098 102.0 6.0 7.0 5.0 119.0\n", + "2099 97.0 19.0 77.0 31.0 48.0\n", + "\n", + "[2000 rows x 5 columns]" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df2 = df.fillna(s)\n", + "df2" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Python 0\n", + "En 0\n", + "Math 0\n", + "Physic 0\n", + "Chem 0\n", + "dtype: int64" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df2.isnull().sum()" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Python 190\n", + "En 200\n", + "Math 194\n", + "Physic 189\n", + "Chem 181\n", + "dtype: int64" + ] + }, + "execution_count": 62, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.isnull().sum()" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonEnMathPhysicChem
10082.089.099.0101.0125.0
1014.031.0109.032.05.0
10256.0103.056.0NaN90.0
10347.0100.0147.0138.099.0
10438.046.0NaN75.044.0
10518.011.0122.03.0126.0
10656.026.0106.014.0139.0
1073.0137.075.067.0144.0
10835.047.0102.060.063.0
10986.0126.0NaN88.0149.0
11019.0140.0NaN35.033.0
11176.0NaN5.011.033.0
11231.054.091.0119.069.0
11364.037.050.023.021.0
11472.057.0138.015.021.0
11555.0120.0104.032.025.0
11696.024.089.0NaN146.0
11763.0NaN8.064.089.0
11828.0NaN125.082.074.0
11985.039.070.0132.0111.0
\n", + "
" + ], + "text/plain": [ + " Python En Math Physic Chem\n", + "100 82.0 89.0 99.0 101.0 125.0\n", + "101 4.0 31.0 109.0 32.0 5.0\n", + "102 56.0 103.0 56.0 NaN 90.0\n", + "103 47.0 100.0 147.0 138.0 99.0\n", + "104 38.0 46.0 NaN 75.0 44.0\n", + "105 18.0 11.0 122.0 3.0 126.0\n", + "106 56.0 26.0 106.0 14.0 139.0\n", + "107 3.0 137.0 75.0 67.0 144.0\n", + "108 35.0 47.0 102.0 60.0 63.0\n", + "109 86.0 126.0 NaN 88.0 149.0\n", + "110 19.0 140.0 NaN 35.0 33.0\n", + "111 76.0 NaN 5.0 11.0 33.0\n", + "112 31.0 54.0 91.0 119.0 69.0\n", + "113 64.0 37.0 50.0 23.0 21.0\n", + "114 72.0 57.0 138.0 15.0 21.0\n", + "115 55.0 120.0 104.0 32.0 25.0\n", + "116 96.0 24.0 89.0 NaN 146.0\n", + "117 63.0 NaN 8.0 64.0 89.0\n", + "118 28.0 NaN 125.0 82.0 74.0\n", + "119 85.0 39.0 70.0 132.0 111.0" + ] + }, + "execution_count": 65, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df3 = df.iloc[:20]\n", + "df3" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonEnMathPhysicChem
10082.089.099.0101.0125.0
1014.031.0109.032.05.0
10256.0103.056.090.090.0
10347.0100.0147.0138.099.0
10438.046.075.075.044.0
10518.011.0122.03.0126.0
10656.026.0106.014.0139.0
1073.0137.075.067.0144.0
10835.047.0102.060.063.0
10986.0126.088.088.0149.0
11019.0140.035.035.033.0
11176.05.05.011.033.0
11231.054.091.0119.069.0
11364.037.050.023.021.0
11472.057.0138.015.021.0
11555.0120.0104.032.025.0
11696.024.089.0146.0146.0
11763.08.08.064.089.0
11828.0125.0125.082.074.0
11985.039.070.0132.0111.0
\n", + "
" + ], + "text/plain": [ + " Python En Math Physic Chem\n", + "100 82.0 89.0 99.0 101.0 125.0\n", + "101 4.0 31.0 109.0 32.0 5.0\n", + "102 56.0 103.0 56.0 90.0 90.0\n", + "103 47.0 100.0 147.0 138.0 99.0\n", + "104 38.0 46.0 75.0 75.0 44.0\n", + "105 18.0 11.0 122.0 3.0 126.0\n", + "106 56.0 26.0 106.0 14.0 139.0\n", + "107 3.0 137.0 75.0 67.0 144.0\n", + "108 35.0 47.0 102.0 60.0 63.0\n", + "109 86.0 126.0 88.0 88.0 149.0\n", + "110 19.0 140.0 35.0 35.0 33.0\n", + "111 76.0 5.0 5.0 11.0 33.0\n", + "112 31.0 54.0 91.0 119.0 69.0\n", + "113 64.0 37.0 50.0 23.0 21.0\n", + "114 72.0 57.0 138.0 15.0 21.0\n", + "115 55.0 120.0 104.0 32.0 25.0\n", + "116 96.0 24.0 89.0 146.0 146.0\n", + "117 63.0 8.0 8.0 64.0 89.0\n", + "118 28.0 125.0 125.0 82.0 74.0\n", + "119 85.0 39.0 70.0 132.0 111.0" + ] + }, + "execution_count": 70, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'''method : {'backfill', 'bfill', 'pad', 'ffill', None}, default None\n", + " Method to use for filling holes in reindexed Series\n", + " pad / ffill: propagate last valid observation forward to next valid\n", + " backfill / bfill: use NEXT valid observation to fill gap'''\n", + "df3.fillna(method='bfill',axis = 1)" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(2000, 5)" + ] + }, + "execution_count": 71, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#数据量足够大,空数据比较少,直接删除\n", + "df.shape" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "df.dro" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Day76-90/code/4-pandas多层索引.ipynb b/Day76-90/code/4-pandas多层索引.ipynb new file mode 100644 index 0000000..01f9dd6 --- /dev/null +++ b/Day76-90/code/4-pandas多层索引.ipynb @@ -0,0 +1,568 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "\n", + "import pandas as pd\n", + "# 数据分析BI-------->人工智能AI\n", + "# 数据分析和数据挖掘一个意思,\n", + "# 工具和软件:Excel 免费版\n", + "# SPSS(一人一年10000)、SAS(一人一年5000)、Matlab 收费\n", + "# R、Python(全方位语言,流行) 免费\n", + "# Python + numpy + scipy + pandas + matplotlib + seaborn + pyEcharts + sklearn + kereas(Tensorflow)+…… \n", + "# 代码,自动化(数据输入----输出结果)\n", + "from pandas import Series,DataFrame" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "a 63\n", + "b 107\n", + "c 16\n", + "d 35\n", + "e 140\n", + "f 83\n", + "dtype: int32" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 多层索引,行列\n", + "# 单层索引\n", + "s = Series(np.random.randint(0,150,size = 6),index=list('abcdef'))\n", + "s" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "张三 期中 114\n", + " 期末 131\n", + "李四 期中 3\n", + " 期末 63\n", + "王五 期中 107\n", + " 期末 34\n", + "dtype: int32" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 多层索引,两层,三层以上(规则一样)\n", + "s2 = Series(np.random.randint(0,150,size = 6),index = pd.MultiIndex.from_product([['张三','李四','王五'],['期中','期末']]))\n", + "s2" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonEnMath
张三期中73525
期末373656
李四期中14981142
期末711380
王五期中1194103
期末2512183
\n", + "
" + ], + "text/plain": [ + " Python En Math\n", + "张三 期中 73 5 25\n", + " 期末 37 36 56\n", + "李四 期中 149 81 142\n", + " 期末 71 138 0\n", + "王五 期中 11 94 103\n", + " 期末 25 121 83" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df = DataFrame(np.random.randint(0,150,size = (6,3)),columns=['Python','En','Math'],index =pd.MultiIndex.from_product([['张三','李四','王五'],['期中','期末']]) )\n", + "\n", + "df" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonEnMath
张三期中A153117
B8256123
期末A14278
B695017
李四期中A9187143
B12011839
期末A567655
B11105121
王五期中A147781
B128126146
期末A4945114
B1212677
\n", + "
" + ], + "text/plain": [ + " Python En Math\n", + "张三 期中 A 15 31 17\n", + " B 82 56 123\n", + " 期末 A 14 2 78\n", + " B 69 50 17\n", + "李四 期中 A 91 87 143\n", + " B 120 118 39\n", + " 期末 A 56 76 55\n", + " B 11 105 121\n", + "王五 期中 A 147 78 1\n", + " B 128 126 146\n", + " 期末 A 49 45 114\n", + " B 121 26 77" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 三层索引\n", + "df3 = DataFrame(np.random.randint(0,150,size = (12,3)),columns=['Python','En','Math'],index =pd.MultiIndex.from_product([['张三','李四','王五'],['期中','期末'],['A','B']]) )\n", + "\n", + "df3" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "73" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 先获取列后获取行\n", + "df['Python']['张三']['期中']" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "df2 = df.copy()" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonEnMath
张三期中73525
期末373656
李四期中14981142
期末711380
王五期中1194103
期末2512183
\n", + "
" + ], + "text/plain": [ + " Python En Math\n", + "张三 期中 73 5 25\n", + " 期末 37 36 56\n", + "李四 期中 149 81 142\n", + " 期末 71 138 0\n", + "王五 期中 11 94 103\n", + " 期末 25 121 83" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df2.sort_index()" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "73" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 先获取行,后获取列\n", + "df.loc['张三'].loc['期中']['Python']" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonEnMath
张三期中73525
期末373656
\n", + "
" + ], + "text/plain": [ + " Python En Math\n", + "张三 期中 73 5 25\n", + " 期末 37 36 56" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.iloc[[0,1]]" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Day76-90/code/5-pandas多层索引计算.ipynb b/Day76-90/code/5-pandas多层索引计算.ipynb new file mode 100644 index 0000000..22e1c8e --- /dev/null +++ b/Day76-90/code/5-pandas多层索引计算.ipynb @@ -0,0 +1,1000 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "\n", + "import pandas as pd\n", + "\n", + "from pandas import Series,DataFrame" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonEnMath
期中期末期中期末期中期末
A1311011731517
B6234531012457
C247636117123105
D11246794246122
E661131044510108
F11110844113221
\n", + "
" + ], + "text/plain": [ + " Python En Math \n", + " 期中 期末 期中 期末 期中 期末\n", + "A 131 101 1 73 15 17\n", + "B 62 34 53 101 24 57\n", + "C 24 76 36 117 123 105\n", + "D 112 46 79 42 46 122\n", + "E 66 113 104 45 10 108\n", + "F 111 108 4 41 132 21" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 多层列索引\n", + "df = DataFrame(np.random.randint(0,150,size = (6,6)),index = list('ABCDEF'),\n", + " columns=pd.MultiIndex.from_product([['Python','En','Math'],['期中','期末']]))\n", + "df" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "scrolled": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Python 期中 84.3\n", + " 期末 79.7\n", + "En 期中 46.2\n", + " 期末 69.8\n", + "Math 期中 58.3\n", + " 期末 71.7\n", + "dtype: float64" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# round保留2位小数\n", + "df.mean().round(1)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonEnMath
期中期末期中期末期中期末
A1311011731517
B6234531012457
C247636117123105
D11246794246122
E661131044510108
F11110844113221
\n", + "
" + ], + "text/plain": [ + " Python En Math \n", + " 期中 期末 期中 期末 期中 期末\n", + "A 131 101 1 73 15 17\n", + "B 62 34 53 101 24 57\n", + "C 24 76 36 117 123 105\n", + "D 112 46 79 42 46 122\n", + "E 66 113 104 45 10 108\n", + "F 111 108 4 41 132 21" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonEnMath
A116.037.016.0
B48.077.040.5
C50.076.5114.0
D79.060.584.0
E89.574.559.0
F109.522.576.5
\n", + "
" + ], + "text/plain": [ + " Python En Math\n", + "A 116.0 37.0 16.0\n", + "B 48.0 77.0 40.5\n", + "C 50.0 76.5 114.0\n", + "D 79.0 60.5 84.0\n", + "E 89.5 74.5 59.0\n", + "F 109.5 22.5 76.5" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# axis = 0代表行\n", + "# axis = 1代表列\n", + "df.mean(axis = 1,level = 0)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
期中期末
A49.063.7
B46.364.0
C61.099.3
D79.070.0
E60.088.7
F82.356.7
\n", + "
" + ], + "text/plain": [ + " 期中 期末\n", + "A 49.0 63.7\n", + "B 46.3 64.0\n", + "C 61.0 99.3\n", + "D 79.0 70.0\n", + "E 60.0 88.7\n", + "F 82.3 56.7" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.mean(axis = 1,level = 1).round(1)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonEnMath
期中期末期中期末期中期末
A1311011731517
B6234531012457
C247636117123105
D11246794246122
E661131044510108
F11110844113221
\n", + "
" + ], + "text/plain": [ + " Python En Math \n", + " 期中 期末 期中 期末 期中 期末\n", + "A 131 101 1 73 15 17\n", + "B 62 34 53 101 24 57\n", + "C 24 76 36 117 123 105\n", + "D 112 46 79 42 46 122\n", + "E 66 113 104 45 10 108\n", + "F 111 108 4 41 132 21" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
EnMathPython
A期中115131
期末7317101
B期中532462
期末1015734
C期中3612324
期末11710576
D期中7946112
期末4212246
E期中1041066
期末45108113
F期中4132111
期末4121108
\n", + "
" + ], + "text/plain": [ + " En Math Python\n", + "A 期中 1 15 131\n", + " 期末 73 17 101\n", + "B 期中 53 24 62\n", + " 期末 101 57 34\n", + "C 期中 36 123 24\n", + " 期末 117 105 76\n", + "D 期中 79 46 112\n", + " 期末 42 122 46\n", + "E 期中 104 10 66\n", + " 期末 45 108 113\n", + "F 期中 4 132 111\n", + " 期末 41 21 108" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 行和列的多层索引,进行转换\n", + "# Stack the prescribed level(s) from columns to index.\n", + "# 从列变成行\n", + "df2 = df.stack(level = 1)\n", + "df2" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
EnMathPython
ABCDEFABCDEFABCDEF
期中1533679104415241234610132131622411266111
期末73101117424541175710512210821101347646113108
\n", + "
" + ], + "text/plain": [ + " En Math Python \n", + " A B C D E F A B C D E F A B C D E F\n", + "期中 1 53 36 79 104 4 15 24 123 46 10 132 131 62 24 112 66 111\n", + "期末 73 101 117 42 45 41 17 57 105 122 108 21 101 34 76 46 113 108" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 从行变成列\n", + "df2.unstack(level= 0 )" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
EnMathPython
期中期末期中期末期中期末
A1731517131101
B5310124576234
C361171231052476
D79424612211246
E104451010866113
F44113221111108
\n", + "
" + ], + "text/plain": [ + " En Math Python \n", + " 期中 期末 期中 期末 期中 期末\n", + "A 1 73 15 17 131 101\n", + "B 53 101 24 57 62 34\n", + "C 36 117 123 105 24 76\n", + "D 79 42 46 122 112 46\n", + "E 104 45 10 108 66 113\n", + "F 4 41 132 21 111 108" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df2.unstack(level = 1)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Day76-90/code/6-pandas数据集成.ipynb b/Day76-90/code/6-pandas数据集成.ipynb new file mode 100644 index 0000000..e128ee4 --- /dev/null +++ b/Day76-90/code/6-pandas数据集成.ipynb @@ -0,0 +1,1209 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "\n", + "import pandas as pd\n", + "from pandas import Series,DataFrame" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 数据分析数据挖掘\n", + "# 有数据情况下:\n", + "# 数据预处理\n", + "# 数据清洗(空数据,异常值)\n", + "# 数据集成(多个数据合并到一起,级联)数据可能存放在多个表中\n", + "# 数据转化\n", + "# 数据规约(属性减少(不重要的属性删除),数据减少去重操作)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 5, 12, 67, 29, 46, 103, 53, 53, 139, 87],\n", + " [126, 33, 55, 104, 45, 70, 96, 133, 116, 43],\n", + " [ 84, 45, 17, 42, 19, 11, 125, 43, 54, 39],\n", + " [ 97, 68, 99, 90, 28, 60, 135, 84, 111, 63],\n", + " [114, 56, 30, 81, 48, 73, 119, 65, 20, 22]])" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "array([[115, 128, 122, 127, 4, 135, 26, 25, 131, 139],\n", + " [ 66, 119, 37, 136, 101, 40, 102, 127, 148, 127],\n", + " [ 89, 80, 140, 133, 51, 142, 47, 27, 54, 23],\n", + " [ 64, 127, 33, 128, 60, 106, 67, 94, 110, 76],\n", + " [ 6, 21, 23, 96, 10, 62, 26, 79, 149, 43],\n", + " [116, 143, 132, 118, 68, 21, 57, 133, 124, 124]])" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# 首先看numpy数组的集成\n", + "nd1 = np.random.randint(0,150,size = (5,10))\n", + "\n", + "nd2 = np.random.randint(0,150,size = (6,10))\n", + "display(nd1,nd2)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 5, 12, 67, 29, 46, 103, 53, 53, 139, 87],\n", + " [126, 33, 55, 104, 45, 70, 96, 133, 116, 43],\n", + " [ 84, 45, 17, 42, 19, 11, 125, 43, 54, 39],\n", + " [ 97, 68, 99, 90, 28, 60, 135, 84, 111, 63],\n", + " [114, 56, 30, 81, 48, 73, 119, 65, 20, 22],\n", + " [115, 128, 122, 127, 4, 135, 26, 25, 131, 139],\n", + " [ 66, 119, 37, 136, 101, 40, 102, 127, 148, 127],\n", + " [ 89, 80, 140, 133, 51, 142, 47, 27, 54, 23],\n", + " [ 64, 127, 33, 128, 60, 106, 67, 94, 110, 76],\n", + " [ 6, 21, 23, 96, 10, 62, 26, 79, 149, 43],\n", + " [116, 143, 132, 118, 68, 21, 57, 133, 124, 124]])" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 原来数据一个5行,一个是6行,级联之后变成了11行\n", + "nd3 = np.concatenate([nd1,nd2],axis = 0)\n", + "nd3" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[110, 38, 144, 92, 38, 2, 67, 2, 103, 81],\n", + " [ 56, 61, 61, 22, 108, 145, 95, 44, 40, 100],\n", + " [ 65, 74, 85, 123, 47, 117, 35, 55, 120, 20],\n", + " [ 15, 9, 4, 84, 71, 133, 140, 13, 71, 91],\n", + " [ 94, 31, 41, 5, 7, 32, 50, 24, 18, 120]])" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "array([[ 65, 149, 86, 138, 98],\n", + " [136, 49, 102, 45, 140],\n", + " [ 13, 124, 94, 81, 73],\n", + " [ 82, 38, 0, 75, 94],\n", + " [146, 28, 143, 61, 49]])" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "nd1 = np.random.randint(0,150,size = (5,10))\n", + "\n", + "nd2 = np.random.randint(0,150,size = (5,5))\n", + "display(nd1,nd2)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[110, 38, 144, 92, 38, 2, 67, 2, 103, 81, 65, 149, 86,\n", + " 138, 98],\n", + " [ 56, 61, 61, 22, 108, 145, 95, 44, 40, 100, 136, 49, 102,\n", + " 45, 140],\n", + " [ 65, 74, 85, 123, 47, 117, 35, 55, 120, 20, 13, 124, 94,\n", + " 81, 73],\n", + " [ 15, 9, 4, 84, 71, 133, 140, 13, 71, 91, 82, 38, 0,\n", + " 75, 94],\n", + " [ 94, 31, 41, 5, 7, 32, 50, 24, 18, 120, 146, 28, 143,\n", + " 61, 49]])" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# axis = 0行级联(第一维度的级联),axis = 1(第二个维度的级联,列的级联)\n", + "np.concatenate((nd1,nd2),axis = 1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# pandas级联操作,pandas基于numpy\n", + "# pandas的级联类似" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonMathEn
A1135380
B1354052
C1441864
\n", + "
" + ], + "text/plain": [ + " Python Math En\n", + "A 113 53 80\n", + "B 135 40 52\n", + "C 144 18 64" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonMathEn
D126118146
E1478127
F87631
G359533
H13011791
I12498122
\n", + "
" + ], + "text/plain": [ + " Python Math En\n", + "D 126 118 146\n", + "E 147 81 27\n", + "F 87 63 1\n", + "G 35 95 33\n", + "H 130 117 91\n", + "I 124 98 122" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "df1 = DataFrame(np.random.randint(0,150,size = (3,3)),index = list('ABC'),columns=['Python','Math','En'])\n", + "\n", + "df2 = DataFrame(np.random.randint(0,150,size = (6,3)),index = list('DEFGHI'),columns=['Python','Math','En'])\n", + "\n", + "display(df1,df2)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonMathEn
A1135380
B1354052
C1441864
D126118146
E1478127
F87631
G359533
H13011791
I12498122
\n", + "
" + ], + "text/plain": [ + " Python Math En\n", + "A 113 53 80\n", + "B 135 40 52\n", + "C 144 18 64\n", + "D 126 118 146\n", + "E 147 81 27\n", + "F 87 63 1\n", + "G 35 95 33\n", + "H 130 117 91\n", + "I 124 98 122" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# pandas汇总数据,数据集成\n", + "df1.append(df2)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonMathEn
A1135380
B1354052
C1441864
D126118146
E1478127
F87631
G359533
H13011791
I12498122
\n", + "
" + ], + "text/plain": [ + " Python Math En\n", + "A 113 53 80\n", + "B 135 40 52\n", + "C 144 18 64\n", + "D 126 118 146\n", + "E 147 81 27\n", + "F 87 63 1\n", + "G 35 95 33\n", + "H 130 117 91\n", + "I 124 98 122" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pd.concat([df1,df2])" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "d:\\python36\\lib\\site-packages\\ipykernel_launcher.py:1: FutureWarning: Sorting because non-concatenation axis is not aligned. A future version\n", + "of pandas will change to not sort by default.\n", + "\n", + "To accept the future behavior, pass 'sort=False'.\n", + "\n", + "To retain the current behavior and silence the warning, pass 'sort=True'.\n", + "\n", + " \"\"\"Entry point for launching an IPython kernel.\n" + ] + }, + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonMathEnPythonMathEn
A113.053.080.0NaNNaNNaN
B135.040.052.0NaNNaNNaN
C144.018.064.0NaNNaNNaN
DNaNNaNNaN126.0118.0146.0
ENaNNaNNaN147.081.027.0
FNaNNaNNaN87.063.01.0
GNaNNaNNaN35.095.033.0
HNaNNaNNaN130.0117.091.0
INaNNaNNaN124.098.0122.0
\n", + "
" + ], + "text/plain": [ + " Python Math En Python Math En\n", + "A 113.0 53.0 80.0 NaN NaN NaN\n", + "B 135.0 40.0 52.0 NaN NaN NaN\n", + "C 144.0 18.0 64.0 NaN NaN NaN\n", + "D NaN NaN NaN 126.0 118.0 146.0\n", + "E NaN NaN NaN 147.0 81.0 27.0\n", + "F NaN NaN NaN 87.0 63.0 1.0\n", + "G NaN NaN NaN 35.0 95.0 33.0\n", + "H NaN NaN NaN 130.0 117.0 91.0\n", + "I NaN NaN NaN 124.0 98.0 122.0" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pd.concat([df1,df2],axis = 1,ignore_index = False)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonMathEn
A225813
B995735
C512824
E560111
F13723121
G4978115
\n", + "
" + ], + "text/plain": [ + " Python Math En\n", + "A 22 58 13\n", + "B 99 57 35\n", + "C 51 28 24\n", + "E 5 60 111\n", + "F 137 23 121\n", + "G 49 78 115" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonMathEn
A11811381
B5122126
C0115128
E10013094
F4993140
G705994
\n", + "
" + ], + "text/plain": [ + " Python Math En\n", + "A 118 113 81\n", + "B 51 22 126\n", + "C 0 115 128\n", + "E 100 130 94\n", + "F 49 93 140\n", + "G 70 59 94" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# 期中\n", + "df1 = DataFrame(np.random.randint(0,150,size = (6,3)),index = list('ABCEFG'),columns=['Python','Math','En'])\n", + "\n", + "# 期末\n", + "df2 = DataFrame(np.random.randint(0,150,size = (6,3)),index = list('ABCEFG'),columns=['Python','Math','En'])\n", + "\n", + "display(df1,df2)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonMathEn
期中A225813
B995735
C512824
E560111
F13723121
G4978115
期末A11811381
B5122126
C0115128
E10013094
F4993140
G705994
\n", + "
" + ], + "text/plain": [ + " Python Math En\n", + "期中 A 22 58 13\n", + " B 99 57 35\n", + " C 51 28 24\n", + " E 5 60 111\n", + " F 137 23 121\n", + " G 49 78 115\n", + "期末 A 118 113 81\n", + " B 51 22 126\n", + " C 0 115 128\n", + " E 100 130 94\n", + " F 49 93 140\n", + " G 70 59 94" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df3 = pd.concat([df1,df2],axis = 0,keys = ['期中','期末'])\n", + "df3" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonMathEn
A期中225813
期末11811381
B期中995735
期末5122126
C期中512824
期末0115128
E期中560111
期末10013094
F期中13723121
期末4993140
G期中4978115
期末705994
\n", + "
" + ], + "text/plain": [ + " Python Math En\n", + "A 期中 22 58 13\n", + " 期末 118 113 81\n", + "B 期中 99 57 35\n", + " 期末 51 22 126\n", + "C 期中 51 28 24\n", + " 期末 0 115 128\n", + "E 期中 5 60 111\n", + " 期末 100 130 94\n", + "F 期中 137 23 121\n", + " 期末 49 93 140\n", + "G 期中 49 78 115\n", + " 期末 70 59 94" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df3.unstack(level = 0).stack()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Day76-90/code/7-pandas数据集成merge.ipynb b/Day76-90/code/7-pandas数据集成merge.ipynb new file mode 100644 index 0000000..8b2eefc --- /dev/null +++ b/Day76-90/code/7-pandas数据集成merge.ipynb @@ -0,0 +1,1272 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "\n", + "import pandas as pd\n", + "from pandas import Series,DataFrame" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 上一讲,append,concat数据集成方法\n", + "# merge融合,根据某一共同属性进行级联,高级用法" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
namesexid
0A1
1B2
2C3
3D4
4E5
5F6
\n", + "
" + ], + "text/plain": [ + " name sex id\n", + "0 A 男 1\n", + "1 B 女 2\n", + "2 C 女 3\n", + "3 D 女 4\n", + "4 E 男 5\n", + "5 F 男 6" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df1 = DataFrame({'name':['A','B','C','D','E','F'],\n", + " 'sex':['男','女','女','女','男','男'],\n", + " 'id':[1,2,3,4,5,6]})\n", + "df1" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
agesalaryid
022120001
125150002
227200003
321300004
418100005
52980007
\n", + "
" + ], + "text/plain": [ + " age salary id\n", + "0 22 12000 1\n", + "1 25 15000 2\n", + "2 27 20000 3\n", + "3 21 30000 4\n", + "4 18 10000 5\n", + "5 29 8000 7" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df2 = DataFrame({'age':[22,25,27,21,18,29],'salary':[12000,15000,20000,30000,10000,8000],'id':[1,2,3,4,5,7]})\n", + "df2" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "d:\\python36\\lib\\site-packages\\pandas\\core\\frame.py:6692: FutureWarning: Sorting because non-concatenation axis is not aligned. A future version\n", + "of pandas will change to not sort by default.\n", + "\n", + "To accept the future behavior, pass 'sort=False'.\n", + "\n", + "To retain the current behavior and silence the warning, pass 'sort=True'.\n", + "\n", + " sort=sort)\n" + ] + }, + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
ageidnamesalarysex
0NaN1ANaN
1NaN2BNaN
2NaN3CNaN
3NaN4DNaN
4NaN5ENaN
5NaN6FNaN
022.01NaN12000.0NaN
125.02NaN15000.0NaN
227.03NaN20000.0NaN
321.04NaN30000.0NaN
418.05NaN10000.0NaN
529.07NaN8000.0NaN
\n", + "
" + ], + "text/plain": [ + " age id name salary sex\n", + "0 NaN 1 A NaN 男\n", + "1 NaN 2 B NaN 女\n", + "2 NaN 3 C NaN 女\n", + "3 NaN 4 D NaN 女\n", + "4 NaN 5 E NaN 男\n", + "5 NaN 6 F NaN 男\n", + "0 22.0 1 NaN 12000.0 NaN\n", + "1 25.0 2 NaN 15000.0 NaN\n", + "2 27.0 3 NaN 20000.0 NaN\n", + "3 21.0 4 NaN 30000.0 NaN\n", + "4 18.0 5 NaN 10000.0 NaN\n", + "5 29.0 7 NaN 8000.0 NaN" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df1.append(df2)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
namesexidagesalaryid
0A122120001
1B225150002
2C327200003
3D421300004
4E518100005
5F62980007
\n", + "
" + ], + "text/plain": [ + " name sex id age salary id\n", + "0 A 男 1 22 12000 1\n", + "1 B 女 2 25 15000 2\n", + "2 C 女 3 27 20000 3\n", + "3 D 女 4 21 30000 4\n", + "4 E 男 5 18 10000 5\n", + "5 F 男 6 29 8000 7" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pd.concat([df1,df2],axis = 1)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
namesexidagesalary
0A12212000
1B22515000
2C32720000
3D42130000
4E51810000
\n", + "
" + ], + "text/plain": [ + " name sex id age salary\n", + "0 A 男 1 22 12000\n", + "1 B 女 2 25 15000\n", + "2 C 女 3 27 20000\n", + "3 D 女 4 21 30000\n", + "4 E 男 5 18 10000" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df1.merge(df2)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
namesexidagesalary
0A122.012000.0
1B225.015000.0
2C327.020000.0
3D421.030000.0
4E518.010000.0
5F6NaNNaN
6NaNNaN729.08000.0
\n", + "
" + ], + "text/plain": [ + " name sex id age salary\n", + "0 A 男 1 22.0 12000.0\n", + "1 B 女 2 25.0 15000.0\n", + "2 C 女 3 27.0 20000.0\n", + "3 D 女 4 21.0 30000.0\n", + "4 E 男 5 18.0 10000.0\n", + "5 F 男 6 NaN NaN\n", + "6 NaN NaN 7 29.0 8000.0" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df1.merge(df2,how = 'outer')" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonMathEn
A401590
B595283
C14138137
D897853
E811013
F757986
\n", + "
" + ], + "text/plain": [ + " Python Math En\n", + "A 40 15 90\n", + "B 59 52 83\n", + "C 14 138 137\n", + "D 89 78 53\n", + "E 81 101 3\n", + "F 75 79 86" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df = DataFrame(np.random.randint(0,150,size = (6,3)),index = list('ABCDEF'),columns=['Python','Math','En'])\n", + "df" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "scrolled": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Python 59.7\n", + "Math 77.2\n", + "En 75.3\n", + "dtype: float64" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s = df.mean().round(1)\n", + "s" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
score_mean
Python59.7
Math77.2
En75.3
\n", + "
" + ], + "text/plain": [ + " score_mean\n", + "Python 59.7\n", + "Math 77.2\n", + "En 75.3" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df2 = DataFrame(s)\n", + "df2.columns = ['score_mean']\n", + "df2" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonMathEn
score_mean59.777.275.3
\n", + "
" + ], + "text/plain": [ + " Python Math En\n", + "score_mean 59.7 77.2 75.3" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df3 = df2.T\n", + "df3" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonMathEn
A40.015.090.0
B59.052.083.0
C14.0138.0137.0
D89.078.053.0
E81.0101.03.0
F75.079.086.0
score_mean59.777.275.3
\n", + "
" + ], + "text/plain": [ + " Python Math En\n", + "A 40.0 15.0 90.0\n", + "B 59.0 52.0 83.0\n", + "C 14.0 138.0 137.0\n", + "D 89.0 78.0 53.0\n", + "E 81.0 101.0 3.0\n", + "F 75.0 79.0 86.0\n", + "score_mean 59.7 77.2 75.3" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df4 = df.append(df3)\n", + "df4" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
score_mean
A48.3
B64.7
C96.3
D73.3
E61.7
F80.0
score_mean70.7
\n", + "
" + ], + "text/plain": [ + " score_mean\n", + "A 48.3\n", + "B 64.7\n", + "C 96.3\n", + "D 73.3\n", + "E 61.7\n", + "F 80.0\n", + "score_mean 70.7" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df5 = DataFrame(df4.mean(axis = 1).round(1))\n", + "df5.columns = ['score_mean']\n", + "df5" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PythonMathEnscore_mean
A40.015.090.048.3
B59.052.083.064.7
C14.0138.0137.096.3
D89.078.053.073.3
E81.0101.03.061.7
F75.079.086.080.0
score_mean59.777.275.370.7
\n", + "
" + ], + "text/plain": [ + " Python Math En score_mean\n", + "A 40.0 15.0 90.0 48.3\n", + "B 59.0 52.0 83.0 64.7\n", + "C 14.0 138.0 137.0 96.3\n", + "D 89.0 78.0 53.0 73.3\n", + "E 81.0 101.0 3.0 61.7\n", + "F 75.0 79.0 86.0 80.0\n", + "score_mean 59.7 77.2 75.3 70.7" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df4.merge(df5,left_index=True,right_index=True)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Day76-90/code/8-pandas分组聚合操作.ipynb b/Day76-90/code/8-pandas分组聚合操作.ipynb new file mode 100644 index 0000000..7c16aff --- /dev/null +++ b/Day76-90/code/8-pandas分组聚合操作.ipynb @@ -0,0 +1,877 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "# 分组聚合透视\n", + "# 很多时候属性是相似的\n", + "\n", + "import numpy as np\n", + "\n", + "import pandas as pd\n", + "\n", + "from pandas import Series,DataFrame" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
HandSmokesexweightIQ
0rightyesmale80100
1leftyesfemale50120
2leftnofemale4890
3rightnomale75130
4rightyesmale68140
5rightnomale10080
6rightnofemale4094
7rightnofemale90110
8leftnomale88100
9rightyesfemale76160
\n", + "
" + ], + "text/plain": [ + " Hand Smoke sex weight IQ\n", + "0 right yes male 80 100\n", + "1 left yes female 50 120\n", + "2 left no female 48 90\n", + "3 right no male 75 130\n", + "4 right yes male 68 140\n", + "5 right no male 100 80\n", + "6 right no female 40 94\n", + "7 right no female 90 110\n", + "8 left no male 88 100\n", + "9 right yes female 76 160" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 走右手习惯,是否抽烟,性别,对体重,智商,有一定影响\n", + "\n", + "df = DataFrame({'Hand':['right','left','left','right','right','right','right','right','left','right'],\n", + " 'Smoke':['yes','yes','no','no','yes','no','no','no','no','yes'],\n", + " 'sex':['male','female','female','male','male','male','female','female','male','female'],\n", + " 'weight':[80,50,48,75,68,100,40,90,88,76],\n", + " 'IQ':[100,120,90,130,140,80,94,110,100,160]})\n", + "df" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 分组聚合查看规律,某一条件下规律" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
weightIQ
Hand
left62.0103.3
right75.6116.3
\n", + "
" + ], + "text/plain": [ + " weight IQ\n", + "Hand \n", + "left 62.0 103.3\n", + "right 75.6 116.3" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data = df.groupby(by = ['Hand'])[['weight','IQ']].mean().round(1)\n", + "data" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
weight
Hand
left62.0
right75.6
\n", + "
" + ], + "text/plain": [ + " weight\n", + "Hand \n", + "left 62.0\n", + "right 75.6" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.groupby(by = ['Hand'])[['weight']].apply(np.mean).round(1)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "df2 = df.groupby(by = ['Hand'])[['weight']].transform(np.mean).round(1)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
weight_mean
075.6
162.0
262.0
375.6
475.6
575.6
675.6
775.6
862.0
975.6
\n", + "
" + ], + "text/plain": [ + " weight_mean\n", + "0 75.6\n", + "1 62.0\n", + "2 62.0\n", + "3 75.6\n", + "4 75.6\n", + "5 75.6\n", + "6 75.6\n", + "7 75.6\n", + "8 62.0\n", + "9 75.6" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df2 = df2.add_suffix('_mean')\n", + "df2" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
HandSmokesexweightIQweight_mean
0rightyesmale8010075.6
1leftyesfemale5012062.0
2leftnofemale489062.0
3rightnomale7513075.6
4rightyesmale6814075.6
5rightnomale1008075.6
6rightnofemale409475.6
7rightnofemale9011075.6
8leftnomale8810062.0
9rightyesfemale7616075.6
\n", + "
" + ], + "text/plain": [ + " Hand Smoke sex weight IQ weight_mean\n", + "0 right yes male 80 100 75.6\n", + "1 left yes female 50 120 62.0\n", + "2 left no female 48 90 62.0\n", + "3 right no male 75 130 75.6\n", + "4 right yes male 68 140 75.6\n", + "5 right no male 100 80 75.6\n", + "6 right no female 40 94 75.6\n", + "7 right no female 90 110 75.6\n", + "8 left no male 88 100 62.0\n", + "9 right yes female 76 160 75.6" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df3 = df.merge(df2,left_index=True,right_index=True)\n", + "df3" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "Hand\n", + "left ([3, 3], [62.0, 103.3])\n", + "right ([7, 7], [75.6, 116.3])\n", + "dtype: object" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def count(x):\n", + " \n", + " return (x.count(),x.mean().round(1))\n", + "\n", + "df.groupby(by = ['Hand'])[['weight','IQ']].apply(count)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
IQ
Handsex
leftfemale120
male100
rightfemale160
male140
\n", + "
" + ], + "text/plain": [ + " IQ\n", + "Hand sex \n", + "left female 120\n", + " male 100\n", + "right female 160\n", + " male 140" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.groupby(by = ['Hand','sex'])[['IQ']].max()" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data = df.groupby(by = ['Hand'])['IQ','weight']\n", + "data" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
IQweight
maxmeanmaxmean
Hand
left120103.38862.0
right160116.310075.6
\n", + "
" + ], + "text/plain": [ + " IQ weight \n", + " max mean max mean\n", + "Hand \n", + "left 120 103.3 88 62.0\n", + "right 160 116.3 100 75.6" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data.agg(['max','mean']).round(1)" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
IQweight
Hand
left12062.0
right16075.6
\n", + "
" + ], + "text/plain": [ + " IQ weight\n", + "Hand \n", + "left 120 62.0\n", + "right 160 75.6" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data.agg({'IQ':'max','weight':'mean'}).round(1)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Day76-90/code/9-pandas数据集成实战.ipynb b/Day76-90/code/9-pandas数据集成实战.ipynb new file mode 100644 index 0000000..063a939 --- /dev/null +++ b/Day76-90/code/9-pandas数据集成实战.ipynb @@ -0,0 +1,6213 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "\n", + "import pandas as pd\n", + "\n", + "from pandas import Series,DataFrame" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# csv类型文件呢,文本文件,excel打开,格式化的文件,所以excel可以直接读取成表格\n", + "# 美国人口的一些情况\n", + "# pandas分析一下美国人口数据" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
statearea (sq. mi)
0Alabama52423
1Alaska656425
2Arizona114006
3Arkansas53182
4California163707
5Colorado104100
6Connecticut5544
7Delaware1954
8Florida65758
9Georgia59441
10Hawaii10932
11Idaho83574
12Illinois57918
13Indiana36420
14Iowa56276
15Kansas82282
16Kentucky40411
17Louisiana51843
18Maine35387
19Maryland12407
20Massachusetts10555
21Michigan96810
22Minnesota86943
23Mississippi48434
24Missouri69709
25Montana147046
26Nebraska77358
27Nevada110567
28New Hampshire9351
29New Jersey8722
30New Mexico121593
31New York54475
32North Carolina53821
33North Dakota70704
34Ohio44828
35Oklahoma69903
36Oregon98386
37Pennsylvania46058
38Rhode Island1545
39South Carolina32007
40South Dakota77121
41Tennessee42146
42Texas268601
43Utah84904
44Vermont9615
45Virginia42769
46Washington71303
47West Virginia24231
48Wisconsin65503
49Wyoming97818
50District of Columbia68
51Puerto Rico3515
\n", + "
" + ], + "text/plain": [ + " state area (sq. mi)\n", + "0 Alabama 52423\n", + "1 Alaska 656425\n", + "2 Arizona 114006\n", + "3 Arkansas 53182\n", + "4 California 163707\n", + "5 Colorado 104100\n", + "6 Connecticut 5544\n", + "7 Delaware 1954\n", + "8 Florida 65758\n", + "9 Georgia 59441\n", + "10 Hawaii 10932\n", + "11 Idaho 83574\n", + "12 Illinois 57918\n", + "13 Indiana 36420\n", + "14 Iowa 56276\n", + "15 Kansas 82282\n", + "16 Kentucky 40411\n", + "17 Louisiana 51843\n", + "18 Maine 35387\n", + "19 Maryland 12407\n", + "20 Massachusetts 10555\n", + "21 Michigan 96810\n", + "22 Minnesota 86943\n", + "23 Mississippi 48434\n", + "24 Missouri 69709\n", + "25 Montana 147046\n", + "26 Nebraska 77358\n", + "27 Nevada 110567\n", + "28 New Hampshire 9351\n", + "29 New Jersey 8722\n", + "30 New Mexico 121593\n", + "31 New York 54475\n", + "32 North Carolina 53821\n", + "33 North Dakota 70704\n", + "34 Ohio 44828\n", + "35 Oklahoma 69903\n", + "36 Oregon 98386\n", + "37 Pennsylvania 46058\n", + "38 Rhode Island 1545\n", + "39 South Carolina 32007\n", + "40 South Dakota 77121\n", + "41 Tennessee 42146\n", + "42 Texas 268601\n", + "43 Utah 84904\n", + "44 Vermont 9615\n", + "45 Virginia 42769\n", + "46 Washington 71303\n", + "47 West Virginia 24231\n", + "48 Wisconsin 65503\n", + "49 Wyoming 97818\n", + "50 District of Columbia 68\n", + "51 Puerto Rico 3515" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 美国各州的面积\n", + "areas = pd.read_csv('./state-areas.csv')\n", + "areas" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(52, 2)" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "areas.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
stateabbreviation
0AlabamaAL
1AlaskaAK
2ArizonaAZ
3ArkansasAR
4CaliforniaCA
5ColoradoCO
6ConnecticutCT
7DelawareDE
8District of ColumbiaDC
9FloridaFL
10GeorgiaGA
11HawaiiHI
12IdahoID
13IllinoisIL
14IndianaIN
15IowaIA
16KansasKS
17KentuckyKY
18LouisianaLA
19MaineME
20MontanaMT
21NebraskaNE
22NevadaNV
23New HampshireNH
24New JerseyNJ
25New MexicoNM
26New YorkNY
27North CarolinaNC
28North DakotaND
29OhioOH
30OklahomaOK
31OregonOR
32MarylandMD
33MassachusettsMA
34MichiganMI
35MinnesotaMN
36MississippiMS
37MissouriMO
38PennsylvaniaPA
39Rhode IslandRI
40South CarolinaSC
41South DakotaSD
42TennesseeTN
43TexasTX
44UtahUT
45VermontVT
46VirginiaVA
47WashingtonWA
48West VirginiaWV
49WisconsinWI
50WyomingWY
\n", + "
" + ], + "text/plain": [ + " state abbreviation\n", + "0 Alabama AL\n", + "1 Alaska AK\n", + "2 Arizona AZ\n", + "3 Arkansas AR\n", + "4 California CA\n", + "5 Colorado CO\n", + "6 Connecticut CT\n", + "7 Delaware DE\n", + "8 District of Columbia DC\n", + "9 Florida FL\n", + "10 Georgia GA\n", + "11 Hawaii HI\n", + "12 Idaho ID\n", + "13 Illinois IL\n", + "14 Indiana IN\n", + "15 Iowa IA\n", + "16 Kansas KS\n", + "17 Kentucky KY\n", + "18 Louisiana LA\n", + "19 Maine ME\n", + "20 Montana MT\n", + "21 Nebraska NE\n", + "22 Nevada NV\n", + "23 New Hampshire NH\n", + "24 New Jersey NJ\n", + "25 New Mexico NM\n", + "26 New York NY\n", + "27 North Carolina NC\n", + "28 North Dakota ND\n", + "29 Ohio OH\n", + "30 Oklahoma OK\n", + "31 Oregon OR\n", + "32 Maryland MD\n", + "33 Massachusetts MA\n", + "34 Michigan MI\n", + "35 Minnesota MN\n", + "36 Mississippi MS\n", + "37 Missouri MO\n", + "38 Pennsylvania PA\n", + "39 Rhode Island RI\n", + "40 South Carolina SC\n", + "41 South Dakota SD\n", + "42 Tennessee TN\n", + "43 Texas TX\n", + "44 Utah UT\n", + "45 Vermont VT\n", + "46 Virginia VA\n", + "47 Washington WA\n", + "48 West Virginia WV\n", + "49 Wisconsin WI\n", + "50 Wyoming WY" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 美国各州 缩写\n", + "abbrevs = pd.read_csv('./state-abbrevs.csv')\n", + "abbrevs" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(51, 2)" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "abbrevs.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
state/regionagesyearpopulation
0ALunder1820121117489.0
1ALtotal20124817528.0
2ALunder1820101130966.0
3ALtotal20104785570.0
4ALunder1820111125763.0
5ALtotal20114801627.0
6ALtotal20094757938.0
7ALunder1820091134192.0
8ALunder1820131111481.0
9ALtotal20134833722.0
10ALtotal20074672840.0
11ALunder1820071132296.0
12ALtotal20084718206.0
13ALunder1820081134927.0
14ALtotal20054569805.0
15ALunder1820051117229.0
16ALtotal20064628981.0
17ALunder1820061126798.0
18ALtotal20044530729.0
19ALunder1820041113662.0
20ALtotal20034503491.0
21ALunder1820031113083.0
22ALtotal20014467634.0
23ALunder1820011120409.0
24ALtotal20024480089.0
25ALunder1820021116590.0
26ALunder1819991121287.0
27ALtotal19994430141.0
28ALtotal20004452173.0
29ALunder1820001122273.0
...............
2514USAunder18199971946051.0
2515USAtotal2000282162411.0
2516USAunder18200072376189.0
2517USAtotal1999279040181.0
2518USAtotal2001284968955.0
2519USAunder18200172671175.0
2520USAtotal2002287625193.0
2521USAunder18200272936457.0
2522USAtotal2003290107933.0
2523USAunder18200373100758.0
2524USAtotal2004292805298.0
2525USAunder18200473297735.0
2526USAtotal2005295516599.0
2527USAunder18200573523669.0
2528USAtotal2006298379912.0
2529USAunder18200673757714.0
2530USAtotal2007301231207.0
2531USAunder18200774019405.0
2532USAtotal2008304093966.0
2533USAunder18200874104602.0
2534USAunder18201373585872.0
2535USAtotal2013316128839.0
2536USAtotal2009306771529.0
2537USAunder18200974134167.0
2538USAunder18201074119556.0
2539USAtotal2010309326295.0
2540USAunder18201173902222.0
2541USAtotal2011311582564.0
2542USAunder18201273708179.0
2543USAtotal2012313873685.0
\n", + "

2544 rows × 4 columns

\n", + "
" + ], + "text/plain": [ + " state/region ages year population\n", + "0 AL under18 2012 1117489.0\n", + "1 AL total 2012 4817528.0\n", + "2 AL under18 2010 1130966.0\n", + "3 AL total 2010 4785570.0\n", + "4 AL under18 2011 1125763.0\n", + "5 AL total 2011 4801627.0\n", + "6 AL total 2009 4757938.0\n", + "7 AL under18 2009 1134192.0\n", + "8 AL under18 2013 1111481.0\n", + "9 AL total 2013 4833722.0\n", + "10 AL total 2007 4672840.0\n", + "11 AL under18 2007 1132296.0\n", + "12 AL total 2008 4718206.0\n", + "13 AL under18 2008 1134927.0\n", + "14 AL total 2005 4569805.0\n", + "15 AL under18 2005 1117229.0\n", + "16 AL total 2006 4628981.0\n", + "17 AL under18 2006 1126798.0\n", + "18 AL total 2004 4530729.0\n", + "19 AL under18 2004 1113662.0\n", + "20 AL total 2003 4503491.0\n", + "21 AL under18 2003 1113083.0\n", + "22 AL total 2001 4467634.0\n", + "23 AL under18 2001 1120409.0\n", + "24 AL total 2002 4480089.0\n", + "25 AL under18 2002 1116590.0\n", + "26 AL under18 1999 1121287.0\n", + "27 AL total 1999 4430141.0\n", + "28 AL total 2000 4452173.0\n", + "29 AL under18 2000 1122273.0\n", + "... ... ... ... ...\n", + "2514 USA under18 1999 71946051.0\n", + "2515 USA total 2000 282162411.0\n", + "2516 USA under18 2000 72376189.0\n", + "2517 USA total 1999 279040181.0\n", + "2518 USA total 2001 284968955.0\n", + "2519 USA under18 2001 72671175.0\n", + "2520 USA total 2002 287625193.0\n", + "2521 USA under18 2002 72936457.0\n", + "2522 USA total 2003 290107933.0\n", + "2523 USA under18 2003 73100758.0\n", + "2524 USA total 2004 292805298.0\n", + "2525 USA under18 2004 73297735.0\n", + "2526 USA total 2005 295516599.0\n", + "2527 USA under18 2005 73523669.0\n", + "2528 USA total 2006 298379912.0\n", + "2529 USA under18 2006 73757714.0\n", + "2530 USA total 2007 301231207.0\n", + "2531 USA under18 2007 74019405.0\n", + "2532 USA total 2008 304093966.0\n", + "2533 USA under18 2008 74104602.0\n", + "2534 USA under18 2013 73585872.0\n", + "2535 USA total 2013 316128839.0\n", + "2536 USA total 2009 306771529.0\n", + "2537 USA under18 2009 74134167.0\n", + "2538 USA under18 2010 74119556.0\n", + "2539 USA total 2010 309326295.0\n", + "2540 USA under18 2011 73902222.0\n", + "2541 USA total 2011 311582564.0\n", + "2542 USA under18 2012 73708179.0\n", + "2543 USA total 2012 313873685.0\n", + "\n", + "[2544 rows x 4 columns]" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 美国的人口数据\n", + "pop = pd.read_csv('./state-population.csv')\n", + "pop" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(2544, 4)" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pop.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "scrolled": false + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
state/regionagesyearpopulation
0ALunder1820121117489.0
1ALtotal20124817528.0
2ALunder1820101130966.0
3ALtotal20104785570.0
4ALunder1820111125763.0
\n", + "
" + ], + "text/plain": [ + " state/region ages year population\n", + "0 AL under18 2012 1117489.0\n", + "1 AL total 2012 4817528.0\n", + "2 AL under18 2010 1130966.0\n", + "3 AL total 2010 4785570.0\n", + "4 AL under18 2011 1125763.0" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pop.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
stateabbreviation
0AlabamaAL
1AlaskaAK
2ArizonaAZ
3ArkansasAR
4CaliforniaCA
\n", + "
" + ], + "text/plain": [ + " state abbreviation\n", + "0 Alabama AL\n", + "1 Alaska AK\n", + "2 Arizona AZ\n", + "3 Arkansas AR\n", + "4 California CA" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "abbrevs.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(2544, 4)" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "(51, 2)" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "display(pop.shape,abbrevs.shape)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(2544, 6)" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 级联时,数据变少了96个,哪些数据变少\n", + "# inner内连接,outer叫做外连接\n", + "pop2 = pop.merge(abbrevs,how = 'outer',left_on='state/region',right_on='abbreviation')\n", + "pop2.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "state/region False\n", + "ages False\n", + "year False\n", + "population True\n", + "state True\n", + "abbreviation True\n", + "dtype: bool" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 前三列没有空值\n", + "pop2.isnull().any()" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
state/regionagesyearpopulationstateabbreviation
0ALunder1820121117489.0AlabamaAL
1ALtotal20124817528.0AlabamaAL
2ALunder1820101130966.0AlabamaAL
3ALtotal20104785570.0AlabamaAL
4ALunder1820111125763.0AlabamaAL
\n", + "
" + ], + "text/plain": [ + " state/region ages year population state abbreviation\n", + "0 AL under18 2012 1117489.0 Alabama AL\n", + "1 AL total 2012 4817528.0 Alabama AL\n", + "2 AL under18 2010 1130966.0 Alabama AL\n", + "3 AL total 2010 4785570.0 Alabama AL\n", + "4 AL under18 2011 1125763.0 Alabama AL" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pop2.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [], + "source": [ + "# 删除一列\n", + "pop2.drop(labels = 'abbreviation',axis = 1,inplace=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
state/regionagesyearpopulationstate
0ALunder1820121117489.0Alabama
1ALtotal20124817528.0Alabama
2ALunder1820101130966.0Alabama
3ALtotal20104785570.0Alabama
4ALunder1820111125763.0Alabama
\n", + "
" + ], + "text/plain": [ + " state/region ages year population state\n", + "0 AL under18 2012 1117489.0 Alabama\n", + "1 AL total 2012 4817528.0 Alabama\n", + "2 AL under18 2010 1130966.0 Alabama\n", + "3 AL total 2010 4785570.0 Alabama\n", + "4 AL under18 2011 1125763.0 Alabama" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pop2.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "state/region False\n", + "ages False\n", + "year False\n", + "population True\n", + "state True\n", + "dtype: bool" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pop2.isnull().any()" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "0 False\n", + "1 False\n", + "2 False\n", + "3 False\n", + "4 False\n", + "5 False\n", + "6 False\n", + "7 False\n", + "8 False\n", + "9 False\n", + "10 False\n", + "11 False\n", + "12 False\n", + "13 False\n", + "14 False\n", + "15 False\n", + "16 False\n", + "17 False\n", + "18 False\n", + "19 False\n", + "20 False\n", + "21 False\n", + "22 False\n", + "23 False\n", + "24 False\n", + "25 False\n", + "26 False\n", + "27 False\n", + "28 False\n", + "29 False\n", + " ... \n", + "2514 True\n", + "2515 True\n", + "2516 True\n", + "2517 True\n", + "2518 True\n", + "2519 True\n", + "2520 True\n", + "2521 True\n", + "2522 True\n", + "2523 True\n", + "2524 True\n", + "2525 True\n", + "2526 True\n", + "2527 True\n", + "2528 True\n", + "2529 True\n", + "2530 True\n", + "2531 True\n", + "2532 True\n", + "2533 True\n", + "2534 True\n", + "2535 True\n", + "2536 True\n", + "2537 True\n", + "2538 True\n", + "2539 True\n", + "2540 True\n", + "2541 True\n", + "2542 True\n", + "2543 True\n", + "Name: state, Length: 2544, dtype: bool" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 定位为空的数据\n", + "cond = pop2['state'].isnull()\n", + "cond" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array(['PR', 'USA'], dtype=object)" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 只有当state为空,返回,为空时True\n", + "# 去重操作,非重复值\n", + "pop2[cond]['state/region'].unique()" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(51, 2)" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "abbrevs.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(52, 2)" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "areas.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
statearea (sq. mi)
0Alabama52423
1Alaska656425
2Arizona114006
3Arkansas53182
4California163707
5Colorado104100
6Connecticut5544
7Delaware1954
8Florida65758
9Georgia59441
10Hawaii10932
11Idaho83574
12Illinois57918
13Indiana36420
14Iowa56276
15Kansas82282
16Kentucky40411
17Louisiana51843
18Maine35387
19Maryland12407
20Massachusetts10555
21Michigan96810
22Minnesota86943
23Mississippi48434
24Missouri69709
25Montana147046
26Nebraska77358
27Nevada110567
28New Hampshire9351
29New Jersey8722
30New Mexico121593
31New York54475
32North Carolina53821
33North Dakota70704
34Ohio44828
35Oklahoma69903
36Oregon98386
37Pennsylvania46058
38Rhode Island1545
39South Carolina32007
40South Dakota77121
41Tennessee42146
42Texas268601
43Utah84904
44Vermont9615
45Virginia42769
46Washington71303
47West Virginia24231
48Wisconsin65503
49Wyoming97818
50District of Columbia68
51Puerto Rico3515
\n", + "
" + ], + "text/plain": [ + " state area (sq. mi)\n", + "0 Alabama 52423\n", + "1 Alaska 656425\n", + "2 Arizona 114006\n", + "3 Arkansas 53182\n", + "4 California 163707\n", + "5 Colorado 104100\n", + "6 Connecticut 5544\n", + "7 Delaware 1954\n", + "8 Florida 65758\n", + "9 Georgia 59441\n", + "10 Hawaii 10932\n", + "11 Idaho 83574\n", + "12 Illinois 57918\n", + "13 Indiana 36420\n", + "14 Iowa 56276\n", + "15 Kansas 82282\n", + "16 Kentucky 40411\n", + "17 Louisiana 51843\n", + "18 Maine 35387\n", + "19 Maryland 12407\n", + "20 Massachusetts 10555\n", + "21 Michigan 96810\n", + "22 Minnesota 86943\n", + "23 Mississippi 48434\n", + "24 Missouri 69709\n", + "25 Montana 147046\n", + "26 Nebraska 77358\n", + "27 Nevada 110567\n", + "28 New Hampshire 9351\n", + "29 New Jersey 8722\n", + "30 New Mexico 121593\n", + "31 New York 54475\n", + "32 North Carolina 53821\n", + "33 North Dakota 70704\n", + "34 Ohio 44828\n", + "35 Oklahoma 69903\n", + "36 Oregon 98386\n", + "37 Pennsylvania 46058\n", + "38 Rhode Island 1545\n", + "39 South Carolina 32007\n", + "40 South Dakota 77121\n", + "41 Tennessee 42146\n", + "42 Texas 268601\n", + "43 Utah 84904\n", + "44 Vermont 9615\n", + "45 Virginia 42769\n", + "46 Washington 71303\n", + "47 West Virginia 24231\n", + "48 Wisconsin 65503\n", + "49 Wyoming 97818\n", + "50 District of Columbia 68\n", + "51 Puerto Rico 3515" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "areas" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "0 False\n", + "1 False\n", + "2 False\n", + "3 False\n", + "4 False\n", + "5 False\n", + "6 False\n", + "7 False\n", + "8 False\n", + "9 False\n", + "10 False\n", + "11 False\n", + "12 False\n", + "13 False\n", + "14 False\n", + "15 False\n", + "16 False\n", + "17 False\n", + "18 False\n", + "19 False\n", + "20 False\n", + "21 False\n", + "22 False\n", + "23 False\n", + "24 False\n", + "25 False\n", + "26 False\n", + "27 False\n", + "28 False\n", + "29 False\n", + " ... \n", + "2514 False\n", + "2515 False\n", + "2516 False\n", + "2517 False\n", + "2518 False\n", + "2519 False\n", + "2520 False\n", + "2521 False\n", + "2522 False\n", + "2523 False\n", + "2524 False\n", + "2525 False\n", + "2526 False\n", + "2527 False\n", + "2528 False\n", + "2529 False\n", + "2530 False\n", + "2531 False\n", + "2532 False\n", + "2533 False\n", + "2534 False\n", + "2535 False\n", + "2536 False\n", + "2537 False\n", + "2538 False\n", + "2539 False\n", + "2540 False\n", + "2541 False\n", + "2542 False\n", + "2543 False\n", + "Name: state/region, Length: 2544, dtype: bool" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cond = pop2['state/region'] == 'PR'\n", + "cond" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "d:\\python36\\lib\\site-packages\\ipykernel_launcher.py:1: SettingWithCopyWarning: \n", + "A value is trying to be set on a copy of a slice from a DataFrame\n", + "\n", + "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n", + " \"\"\"Entry point for launching an IPython kernel.\n" + ] + } + ], + "source": [ + "pop2['state'][cond] = 'Puerto Rico'" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "d:\\python36\\lib\\site-packages\\ipykernel_launcher.py:2: SettingWithCopyWarning: \n", + "A value is trying to be set on a copy of a slice from a DataFrame\n", + "\n", + "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n", + " \n" + ] + } + ], + "source": [ + "cond = pop2['state/region'] == 'USA'\n", + "pop2['state'][cond] = 'United State'" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "state/region False\n", + "ages False\n", + "year False\n", + "population True\n", + "state False\n", + "dtype: bool" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pop2.isnull().any()" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": { + "scrolled": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(20, 5)" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cond = pop2['population'].isnull()\n", + "pop2[cond].shape" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(2544, 5)" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pop2.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [], + "source": [ + "# 将难于进行补全的空数据进行删除\n", + "pop2.dropna(inplace=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(2524, 5)" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pop2.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "state/region False\n", + "ages False\n", + "year False\n", + "population False\n", + "state False\n", + "dtype: bool" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pop2.isnull().any()" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "state/region True\n", + "ages True\n", + "year True\n", + "population True\n", + "state True\n", + "dtype: bool" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pop2.notnull().all()" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
statearea (sq. mi)
0Alabama52423
1Alaska656425
2Arizona114006
3Arkansas53182
4California163707
\n", + "
" + ], + "text/plain": [ + " state area (sq. mi)\n", + "0 Alabama 52423\n", + "1 Alaska 656425\n", + "2 Arizona 114006\n", + "3 Arkansas 53182\n", + "4 California 163707" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "areas.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": { + "scrolled": false + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
state/regionagesyearpopulationstate
0ALunder1820121117489.0Alabama
1ALtotal20124817528.0Alabama
2ALunder1820101130966.0Alabama
3ALtotal20104785570.0Alabama
4ALunder1820111125763.0Alabama
\n", + "
" + ], + "text/plain": [ + " state/region ages year population state\n", + "0 AL under18 2012 1117489.0 Alabama\n", + "1 AL total 2012 4817528.0 Alabama\n", + "2 AL under18 2010 1130966.0 Alabama\n", + "3 AL total 2010 4785570.0 Alabama\n", + "4 AL under18 2011 1125763.0 Alabama" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pop2.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(2524, 6)" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pop3 = pop2.merge(areas,how = 'outer')\n", + "pop3.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
state/regionagesyearpopulationstatearea (sq. mi)
0ALunder1820121117489.0Alabama52423.0
1ALtotal20124817528.0Alabama52423.0
2ALunder1820101130966.0Alabama52423.0
3ALtotal20104785570.0Alabama52423.0
4ALunder1820111125763.0Alabama52423.0
\n", + "
" + ], + "text/plain": [ + " state/region ages year population state area (sq. mi)\n", + "0 AL under18 2012 1117489.0 Alabama 52423.0\n", + "1 AL total 2012 4817528.0 Alabama 52423.0\n", + "2 AL under18 2010 1130966.0 Alabama 52423.0\n", + "3 AL total 2010 4785570.0 Alabama 52423.0\n", + "4 AL under18 2011 1125763.0 Alabama 52423.0" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pop3.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "state/region False\n", + "ages False\n", + "year False\n", + "population False\n", + "state False\n", + "area (sq. mi) True\n", + "dtype: bool" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pop3.isnull().any()" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
state/regionagesyearpopulationstatearea (sq. mi)
2476USAunder18199064218512.0United StateNaN
2477USAtotal1990249622814.0United StateNaN
2478USAtotal1991252980942.0United StateNaN
2479USAunder18199165313018.0United StateNaN
2480USAunder18199266509177.0United StateNaN
2481USAtotal1992256514231.0United StateNaN
2482USAtotal1993259918595.0United StateNaN
2483USAunder18199367594938.0United StateNaN
2484USAunder18199468640936.0United StateNaN
2485USAtotal1994263125826.0United StateNaN
2486USAunder18199569473140.0United StateNaN
2487USAunder18199670233512.0United StateNaN
2488USAtotal1995266278403.0United StateNaN
2489USAtotal1996269394291.0United StateNaN
2490USAtotal1997272646932.0United StateNaN
2491USAunder18199770920738.0United StateNaN
2492USAunder18199871431406.0United StateNaN
2493USAtotal1998275854116.0United StateNaN
2494USAunder18199971946051.0United StateNaN
2495USAtotal2000282162411.0United StateNaN
2496USAunder18200072376189.0United StateNaN
2497USAtotal1999279040181.0United StateNaN
2498USAtotal2001284968955.0United StateNaN
2499USAunder18200172671175.0United StateNaN
2500USAtotal2002287625193.0United StateNaN
2501USAunder18200272936457.0United StateNaN
2502USAtotal2003290107933.0United StateNaN
2503USAunder18200373100758.0United StateNaN
2504USAtotal2004292805298.0United StateNaN
2505USAunder18200473297735.0United StateNaN
2506USAtotal2005295516599.0United StateNaN
2507USAunder18200573523669.0United StateNaN
2508USAtotal2006298379912.0United StateNaN
2509USAunder18200673757714.0United StateNaN
2510USAtotal2007301231207.0United StateNaN
2511USAunder18200774019405.0United StateNaN
2512USAtotal2008304093966.0United StateNaN
2513USAunder18200874104602.0United StateNaN
2514USAunder18201373585872.0United StateNaN
2515USAtotal2013316128839.0United StateNaN
2516USAtotal2009306771529.0United StateNaN
2517USAunder18200974134167.0United StateNaN
2518USAunder18201074119556.0United StateNaN
2519USAtotal2010309326295.0United StateNaN
2520USAunder18201173902222.0United StateNaN
2521USAtotal2011311582564.0United StateNaN
2522USAunder18201273708179.0United StateNaN
2523USAtotal2012313873685.0United StateNaN
\n", + "
" + ], + "text/plain": [ + " state/region ages year population state area (sq. mi)\n", + "2476 USA under18 1990 64218512.0 United State NaN\n", + "2477 USA total 1990 249622814.0 United State NaN\n", + "2478 USA total 1991 252980942.0 United State NaN\n", + "2479 USA under18 1991 65313018.0 United State NaN\n", + "2480 USA under18 1992 66509177.0 United State NaN\n", + "2481 USA total 1992 256514231.0 United State NaN\n", + "2482 USA total 1993 259918595.0 United State NaN\n", + "2483 USA under18 1993 67594938.0 United State NaN\n", + "2484 USA under18 1994 68640936.0 United State NaN\n", + "2485 USA total 1994 263125826.0 United State NaN\n", + "2486 USA under18 1995 69473140.0 United State NaN\n", + "2487 USA under18 1996 70233512.0 United State NaN\n", + "2488 USA total 1995 266278403.0 United State NaN\n", + "2489 USA total 1996 269394291.0 United State NaN\n", + "2490 USA total 1997 272646932.0 United State NaN\n", + "2491 USA under18 1997 70920738.0 United State NaN\n", + "2492 USA under18 1998 71431406.0 United State NaN\n", + "2493 USA total 1998 275854116.0 United State NaN\n", + "2494 USA under18 1999 71946051.0 United State NaN\n", + "2495 USA total 2000 282162411.0 United State NaN\n", + "2496 USA under18 2000 72376189.0 United State NaN\n", + "2497 USA total 1999 279040181.0 United State NaN\n", + "2498 USA total 2001 284968955.0 United State NaN\n", + "2499 USA under18 2001 72671175.0 United State NaN\n", + "2500 USA total 2002 287625193.0 United State NaN\n", + "2501 USA under18 2002 72936457.0 United State NaN\n", + "2502 USA total 2003 290107933.0 United State NaN\n", + "2503 USA under18 2003 73100758.0 United State NaN\n", + "2504 USA total 2004 292805298.0 United State NaN\n", + "2505 USA under18 2004 73297735.0 United State NaN\n", + "2506 USA total 2005 295516599.0 United State NaN\n", + "2507 USA under18 2005 73523669.0 United State NaN\n", + "2508 USA total 2006 298379912.0 United State NaN\n", + "2509 USA under18 2006 73757714.0 United State NaN\n", + "2510 USA total 2007 301231207.0 United State NaN\n", + "2511 USA under18 2007 74019405.0 United State NaN\n", + "2512 USA total 2008 304093966.0 United State NaN\n", + "2513 USA under18 2008 74104602.0 United State NaN\n", + "2514 USA under18 2013 73585872.0 United State NaN\n", + "2515 USA total 2013 316128839.0 United State NaN\n", + "2516 USA total 2009 306771529.0 United State NaN\n", + "2517 USA under18 2009 74134167.0 United State NaN\n", + "2518 USA under18 2010 74119556.0 United State NaN\n", + "2519 USA total 2010 309326295.0 United State NaN\n", + "2520 USA under18 2011 73902222.0 United State NaN\n", + "2521 USA total 2011 311582564.0 United State NaN\n", + "2522 USA under18 2012 73708179.0 United State NaN\n", + "2523 USA total 2012 313873685.0 United State NaN" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cond = pop3['area (sq. mi)'].isnull()\n", + "pop3[cond]" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3790399" + ] + }, + "execution_count": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = areas['area (sq. mi)'].sum()\n", + "a" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "d:\\python36\\lib\\site-packages\\ipykernel_launcher.py:3: SettingWithCopyWarning: \n", + "A value is trying to be set on a copy of a slice from a DataFrame\n", + "\n", + "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n", + " This is separate from the ipykernel package so we can avoid doing imports until\n" + ] + } + ], + "source": [ + "cond = pop3['state'] == \"United State\"\n", + "\n", + "pop3['area (sq. mi)'][cond] = a" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "state/region True\n", + "ages True\n", + "year True\n", + "population True\n", + "state True\n", + "area (sq. mi) True\n", + "dtype: bool" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pop3.notnull().all()" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
state/regionagesyearpopulationstatearea (sq. mi)
0ALunder1820121117489.0Alabama52423.0
1ALtotal20124817528.0Alabama52423.0
2ALunder1820101130966.0Alabama52423.0
3ALtotal20104785570.0Alabama52423.0
4ALunder1820111125763.0Alabama52423.0
\n", + "
" + ], + "text/plain": [ + " state/region ages year population state area (sq. mi)\n", + "0 AL under18 2012 1117489.0 Alabama 52423.0\n", + "1 AL total 2012 4817528.0 Alabama 52423.0\n", + "2 AL under18 2010 1130966.0 Alabama 52423.0\n", + "3 AL total 2010 4785570.0 Alabama 52423.0\n", + "4 AL under18 2011 1125763.0 Alabama 52423.0" + ] + }, + "execution_count": 62, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pop3.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "0 21.3\n", + "1 91.9\n", + "2 21.6\n", + "3 91.3\n", + "4 21.5\n", + "5 91.6\n", + "6 90.8\n", + "7 21.6\n", + "8 21.2\n", + "9 92.2\n", + "10 89.1\n", + "11 21.6\n", + "12 90.0\n", + "13 21.6\n", + "14 87.2\n", + "15 21.3\n", + "16 88.3\n", + "17 21.5\n", + "18 86.4\n", + "19 21.2\n", + "20 85.9\n", + "21 21.2\n", + "22 85.2\n", + "23 21.4\n", + "24 85.5\n", + "25 21.3\n", + "26 21.4\n", + "27 84.5\n", + "28 84.9\n", + "29 21.4\n", + " ... \n", + "2494 19.0\n", + "2495 74.4\n", + "2496 19.1\n", + "2497 73.6\n", + "2498 75.2\n", + "2499 19.2\n", + "2500 75.9\n", + "2501 19.2\n", + "2502 76.5\n", + "2503 19.3\n", + "2504 77.2\n", + "2505 19.3\n", + "2506 78.0\n", + "2507 19.4\n", + "2508 78.7\n", + "2509 19.5\n", + "2510 79.5\n", + "2511 19.5\n", + "2512 80.2\n", + "2513 19.6\n", + "2514 19.4\n", + "2515 83.4\n", + "2516 80.9\n", + "2517 19.6\n", + "2518 19.6\n", + "2519 81.6\n", + "2520 19.5\n", + "2521 82.2\n", + "2522 19.4\n", + "2523 82.8\n", + "Length: 2524, dtype: float64" + ] + }, + "execution_count": 66, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pop_density = (pop3['population']/pop3['area (sq. mi)']).round(1)\n", + "pop_density" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
0
021.3
191.9
221.6
391.3
421.5
591.6
690.8
721.6
821.2
992.2
1089.1
1121.6
1290.0
1321.6
1487.2
1521.3
1688.3
1721.5
1886.4
1921.2
2085.9
2121.2
2285.2
2321.4
2485.5
2521.3
2621.4
2784.5
2884.9
2921.4
......
249419.0
249574.4
249619.1
249773.6
249875.2
249919.2
250075.9
250119.2
250276.5
250319.3
250477.2
250519.3
250678.0
250719.4
250878.7
250919.5
251079.5
251119.5
251280.2
251319.6
251419.4
251583.4
251680.9
251719.6
251819.6
251981.6
252019.5
252182.2
252219.4
252382.8
\n", + "

2524 rows × 1 columns

\n", + "
" + ], + "text/plain": [ + " 0\n", + "0 21.3\n", + "1 91.9\n", + "2 21.6\n", + "3 91.3\n", + "4 21.5\n", + "5 91.6\n", + "6 90.8\n", + "7 21.6\n", + "8 21.2\n", + "9 92.2\n", + "10 89.1\n", + "11 21.6\n", + "12 90.0\n", + "13 21.6\n", + "14 87.2\n", + "15 21.3\n", + "16 88.3\n", + "17 21.5\n", + "18 86.4\n", + "19 21.2\n", + "20 85.9\n", + "21 21.2\n", + "22 85.2\n", + "23 21.4\n", + "24 85.5\n", + "25 21.3\n", + "26 21.4\n", + "27 84.5\n", + "28 84.9\n", + "29 21.4\n", + "... ...\n", + "2494 19.0\n", + "2495 74.4\n", + "2496 19.1\n", + "2497 73.6\n", + "2498 75.2\n", + "2499 19.2\n", + "2500 75.9\n", + "2501 19.2\n", + "2502 76.5\n", + "2503 19.3\n", + "2504 77.2\n", + "2505 19.3\n", + "2506 78.0\n", + "2507 19.4\n", + "2508 78.7\n", + "2509 19.5\n", + "2510 79.5\n", + "2511 19.5\n", + "2512 80.2\n", + "2513 19.6\n", + "2514 19.4\n", + "2515 83.4\n", + "2516 80.9\n", + "2517 19.6\n", + "2518 19.6\n", + "2519 81.6\n", + "2520 19.5\n", + "2521 82.2\n", + "2522 19.4\n", + "2523 82.8\n", + "\n", + "[2524 rows x 1 columns]" + ] + }, + "execution_count": 67, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pop_density = DataFrame(pop_density)\n", + "pop_density" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
pop_density
021.3
191.9
221.6
391.3
421.5
\n", + "
" + ], + "text/plain": [ + " pop_density\n", + "0 21.3\n", + "1 91.9\n", + "2 21.6\n", + "3 91.3\n", + "4 21.5" + ] + }, + "execution_count": 68, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pop_density.columns = ['pop_density']\n", + "pop_density.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
state/regionagesyearpopulationstatearea (sq. mi)pop_density
0ALunder1820121117489.0Alabama52423.021.3
1ALtotal20124817528.0Alabama52423.091.9
2ALunder1820101130966.0Alabama52423.021.6
3ALtotal20104785570.0Alabama52423.091.3
4ALunder1820111125763.0Alabama52423.021.5
\n", + "
" + ], + "text/plain": [ + " state/region ages year population state area (sq. mi) pop_density\n", + "0 AL under18 2012 1117489.0 Alabama 52423.0 21.3\n", + "1 AL total 2012 4817528.0 Alabama 52423.0 91.9\n", + "2 AL under18 2010 1130966.0 Alabama 52423.0 21.6\n", + "3 AL total 2010 4785570.0 Alabama 52423.0 91.3\n", + "4 AL under18 2011 1125763.0 Alabama 52423.0 21.5" + ] + }, + "execution_count": 69, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pop4 = pop3.merge(pop_density,left_index=True,right_index=True)\n", + "pop4.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([2012, 2010, 2011, 2009, 2013, 2007, 2008, 2005, 2006, 2004, 2003,\n", + " 2001, 2002, 1999, 2000, 1998, 1997, 1996, 1995, 1994, 1993, 1992,\n", + " 1991, 1990], dtype=int64)" + ] + }, + "execution_count": 70, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pop4['year'].unique()" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array(['under18', 'total'], dtype=object)" + ] + }, + "execution_count": 71, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pop4['ages'].unique()" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
state/regionagesyearpopulationstatearea (sq. mi)pop_density
1ALtotal20124817528.0Alabama52423.091.9
95AKtotal2012730307.0Alaska656425.01.1
97AZtotal20126551149.0Arizona114006.057.5
191ARtotal20122949828.0Arkansas53182.055.5
193CAtotal201237999878.0California163707.0232.1
287COtotal20125189458.0Colorado104100.049.9
289CTtotal20123591765.0Connecticut5544.0647.9
383DEtotal2012917053.0Delaware1954.0469.3
385DCtotal2012633427.0District of Columbia68.09315.1
479FLtotal201219320749.0Florida65758.0293.8
480GAtotal20129915646.0Georgia59441.0166.8
575HItotal20121390090.0Hawaii10932.0127.2
576IDtotal20121595590.0Idaho83574.019.1
671ILtotal201212868192.0Illinois57918.0222.2
672INtotal20126537782.0Indiana36420.0179.5
767IAtotal20123075039.0Iowa56276.054.6
768KStotal20122885398.0Kansas82282.035.1
863KYtotal20124379730.0Kentucky40411.0108.4
864LAtotal20124602134.0Louisiana51843.088.8
959MEtotal20121328501.0Maine35387.037.5
960MDtotal20125884868.0Maryland12407.0474.3
1055MAtotal20126645303.0Massachusetts10555.0629.6
1056MItotal20129882519.0Michigan96810.0102.1
1151MNtotal20125379646.0Minnesota86943.061.9
1152MStotal20122986450.0Mississippi48434.061.7
1247MOtotal20126024522.0Missouri69709.086.4
1248MTtotal20121005494.0Montana147046.06.8
1343NEtotal20121855350.0Nebraska77358.024.0
1344NVtotal20122754354.0Nevada110567.024.9
1439NHtotal20121321617.0New Hampshire9351.0141.3
1440NJtotal20128867749.0New Jersey8722.01016.7
1535NMtotal20122083540.0New Mexico121593.017.1
1536NYtotal201219576125.0New York54475.0359.4
1631NCtotal20129748364.0North Carolina53821.0181.1
1632NDtotal2012701345.0North Dakota70704.09.9
1727OHtotal201211553031.0Ohio44828.0257.7
1728OKtotal20123815780.0Oklahoma69903.054.6
1823ORtotal20123899801.0Oregon98386.039.6
1824PAtotal201212764475.0Pennsylvania46058.0277.1
1919RItotal20121050304.0Rhode Island1545.0679.8
1920SCtotal20124723417.0South Carolina32007.0147.6
2015SDtotal2012834047.0South Dakota77121.010.8
2016TNtotal20126454914.0Tennessee42146.0153.2
2111TXtotal201226060796.0Texas268601.097.0
2112UTtotal20122854871.0Utah84904.033.6
2207VTtotal2012625953.0Vermont9615.065.1
2208VAtotal20128186628.0Virginia42769.0191.4
2303WAtotal20126895318.0Washington71303.096.7
2304WVtotal20121856680.0West Virginia24231.076.6
2399WItotal20125724554.0Wisconsin65503.087.4
2400WYtotal2012576626.0Wyoming97818.05.9
2475PRtotal20123651545.0Puerto Rico3515.01038.8
2523USAtotal2012313873685.0United State3790399.082.8
\n", + "
" + ], + "text/plain": [ + " state/region ages year population state area (sq. mi) pop_density\n", + "1 AL total 2012 4817528.0 Alabama 52423.0 91.9\n", + "95 AK total 2012 730307.0 Alaska 656425.0 1.1\n", + "97 AZ total 2012 6551149.0 Arizona 114006.0 57.5\n", + "191 AR total 2012 2949828.0 Arkansas 53182.0 55.5\n", + "193 CA total 2012 37999878.0 California 163707.0 232.1\n", + "287 CO total 2012 5189458.0 Colorado 104100.0 49.9\n", + "289 CT total 2012 3591765.0 Connecticut 5544.0 647.9\n", + "383 DE total 2012 917053.0 Delaware 1954.0 469.3\n", + "385 DC total 2012 633427.0 District of Columbia 68.0 9315.1\n", + "479 FL total 2012 19320749.0 Florida 65758.0 293.8\n", + "480 GA total 2012 9915646.0 Georgia 59441.0 166.8\n", + "575 HI total 2012 1390090.0 Hawaii 10932.0 127.2\n", + "576 ID total 2012 1595590.0 Idaho 83574.0 19.1\n", + "671 IL total 2012 12868192.0 Illinois 57918.0 222.2\n", + "672 IN total 2012 6537782.0 Indiana 36420.0 179.5\n", + "767 IA total 2012 3075039.0 Iowa 56276.0 54.6\n", + "768 KS total 2012 2885398.0 Kansas 82282.0 35.1\n", + "863 KY total 2012 4379730.0 Kentucky 40411.0 108.4\n", + "864 LA total 2012 4602134.0 Louisiana 51843.0 88.8\n", + "959 ME total 2012 1328501.0 Maine 35387.0 37.5\n", + "960 MD total 2012 5884868.0 Maryland 12407.0 474.3\n", + "1055 MA total 2012 6645303.0 Massachusetts 10555.0 629.6\n", + "1056 MI total 2012 9882519.0 Michigan 96810.0 102.1\n", + "1151 MN total 2012 5379646.0 Minnesota 86943.0 61.9\n", + "1152 MS total 2012 2986450.0 Mississippi 48434.0 61.7\n", + "1247 MO total 2012 6024522.0 Missouri 69709.0 86.4\n", + "1248 MT total 2012 1005494.0 Montana 147046.0 6.8\n", + "1343 NE total 2012 1855350.0 Nebraska 77358.0 24.0\n", + "1344 NV total 2012 2754354.0 Nevada 110567.0 24.9\n", + "1439 NH total 2012 1321617.0 New Hampshire 9351.0 141.3\n", + "1440 NJ total 2012 8867749.0 New Jersey 8722.0 1016.7\n", + "1535 NM total 2012 2083540.0 New Mexico 121593.0 17.1\n", + "1536 NY total 2012 19576125.0 New York 54475.0 359.4\n", + "1631 NC total 2012 9748364.0 North Carolina 53821.0 181.1\n", + "1632 ND total 2012 701345.0 North Dakota 70704.0 9.9\n", + "1727 OH total 2012 11553031.0 Ohio 44828.0 257.7\n", + "1728 OK total 2012 3815780.0 Oklahoma 69903.0 54.6\n", + "1823 OR total 2012 3899801.0 Oregon 98386.0 39.6\n", + "1824 PA total 2012 12764475.0 Pennsylvania 46058.0 277.1\n", + "1919 RI total 2012 1050304.0 Rhode Island 1545.0 679.8\n", + "1920 SC total 2012 4723417.0 South Carolina 32007.0 147.6\n", + "2015 SD total 2012 834047.0 South Dakota 77121.0 10.8\n", + "2016 TN total 2012 6454914.0 Tennessee 42146.0 153.2\n", + "2111 TX total 2012 26060796.0 Texas 268601.0 97.0\n", + "2112 UT total 2012 2854871.0 Utah 84904.0 33.6\n", + "2207 VT total 2012 625953.0 Vermont 9615.0 65.1\n", + "2208 VA total 2012 8186628.0 Virginia 42769.0 191.4\n", + "2303 WA total 2012 6895318.0 Washington 71303.0 96.7\n", + "2304 WV total 2012 1856680.0 West Virginia 24231.0 76.6\n", + "2399 WI total 2012 5724554.0 Wisconsin 65503.0 87.4\n", + "2400 WY total 2012 576626.0 Wyoming 97818.0 5.9\n", + "2475 PR total 2012 3651545.0 Puerto Rico 3515.0 1038.8\n", + "2523 USA total 2012 313873685.0 United State 3790399.0 82.8" + ] + }, + "execution_count": 73, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 查找2012年美国各州的全民人口数据\n", + "\n", + "# pandas非常强大的,可以像查询数据库一样进行数据查询\n", + "\n", + "pop5 = pop4.query(\"year == 2012 and ages == 'total'\")\n", + "pop5" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "metadata": {}, + "outputs": [], + "source": [ + "pop5.set_index(keys = 'state/region',inplace=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
agesyearpopulationstatearea (sq. mi)pop_density
state/region
AKtotal2012730307.0Alaska656425.01.1
WYtotal2012576626.0Wyoming97818.05.9
MTtotal20121005494.0Montana147046.06.8
NDtotal2012701345.0North Dakota70704.09.9
SDtotal2012834047.0South Dakota77121.010.8
NMtotal20122083540.0New Mexico121593.017.1
IDtotal20121595590.0Idaho83574.019.1
NEtotal20121855350.0Nebraska77358.024.0
NVtotal20122754354.0Nevada110567.024.9
UTtotal20122854871.0Utah84904.033.6
KStotal20122885398.0Kansas82282.035.1
MEtotal20121328501.0Maine35387.037.5
ORtotal20123899801.0Oregon98386.039.6
COtotal20125189458.0Colorado104100.049.9
IAtotal20123075039.0Iowa56276.054.6
OKtotal20123815780.0Oklahoma69903.054.6
ARtotal20122949828.0Arkansas53182.055.5
AZtotal20126551149.0Arizona114006.057.5
MStotal20122986450.0Mississippi48434.061.7
MNtotal20125379646.0Minnesota86943.061.9
VTtotal2012625953.0Vermont9615.065.1
WVtotal20121856680.0West Virginia24231.076.6
USAtotal2012313873685.0United State3790399.082.8
MOtotal20126024522.0Missouri69709.086.4
WItotal20125724554.0Wisconsin65503.087.4
LAtotal20124602134.0Louisiana51843.088.8
ALtotal20124817528.0Alabama52423.091.9
WAtotal20126895318.0Washington71303.096.7
TXtotal201226060796.0Texas268601.097.0
MItotal20129882519.0Michigan96810.0102.1
KYtotal20124379730.0Kentucky40411.0108.4
HItotal20121390090.0Hawaii10932.0127.2
NHtotal20121321617.0New Hampshire9351.0141.3
SCtotal20124723417.0South Carolina32007.0147.6
TNtotal20126454914.0Tennessee42146.0153.2
GAtotal20129915646.0Georgia59441.0166.8
INtotal20126537782.0Indiana36420.0179.5
NCtotal20129748364.0North Carolina53821.0181.1
VAtotal20128186628.0Virginia42769.0191.4
ILtotal201212868192.0Illinois57918.0222.2
CAtotal201237999878.0California163707.0232.1
OHtotal201211553031.0Ohio44828.0257.7
PAtotal201212764475.0Pennsylvania46058.0277.1
FLtotal201219320749.0Florida65758.0293.8
NYtotal201219576125.0New York54475.0359.4
DEtotal2012917053.0Delaware1954.0469.3
MDtotal20125884868.0Maryland12407.0474.3
MAtotal20126645303.0Massachusetts10555.0629.6
CTtotal20123591765.0Connecticut5544.0647.9
RItotal20121050304.0Rhode Island1545.0679.8
NJtotal20128867749.0New Jersey8722.01016.7
PRtotal20123651545.0Puerto Rico3515.01038.8
DCtotal2012633427.0District of Columbia68.09315.1
\n", + "
" + ], + "text/plain": [ + " ages year population state area (sq. mi) pop_density\n", + "state/region \n", + "AK total 2012 730307.0 Alaska 656425.0 1.1\n", + "WY total 2012 576626.0 Wyoming 97818.0 5.9\n", + "MT total 2012 1005494.0 Montana 147046.0 6.8\n", + "ND total 2012 701345.0 North Dakota 70704.0 9.9\n", + "SD total 2012 834047.0 South Dakota 77121.0 10.8\n", + "NM total 2012 2083540.0 New Mexico 121593.0 17.1\n", + "ID total 2012 1595590.0 Idaho 83574.0 19.1\n", + "NE total 2012 1855350.0 Nebraska 77358.0 24.0\n", + "NV total 2012 2754354.0 Nevada 110567.0 24.9\n", + "UT total 2012 2854871.0 Utah 84904.0 33.6\n", + "KS total 2012 2885398.0 Kansas 82282.0 35.1\n", + "ME total 2012 1328501.0 Maine 35387.0 37.5\n", + "OR total 2012 3899801.0 Oregon 98386.0 39.6\n", + "CO total 2012 5189458.0 Colorado 104100.0 49.9\n", + "IA total 2012 3075039.0 Iowa 56276.0 54.6\n", + "OK total 2012 3815780.0 Oklahoma 69903.0 54.6\n", + "AR total 2012 2949828.0 Arkansas 53182.0 55.5\n", + "AZ total 2012 6551149.0 Arizona 114006.0 57.5\n", + "MS total 2012 2986450.0 Mississippi 48434.0 61.7\n", + "MN total 2012 5379646.0 Minnesota 86943.0 61.9\n", + "VT total 2012 625953.0 Vermont 9615.0 65.1\n", + "WV total 2012 1856680.0 West Virginia 24231.0 76.6\n", + "USA total 2012 313873685.0 United State 3790399.0 82.8\n", + "MO total 2012 6024522.0 Missouri 69709.0 86.4\n", + "WI total 2012 5724554.0 Wisconsin 65503.0 87.4\n", + "LA total 2012 4602134.0 Louisiana 51843.0 88.8\n", + "AL total 2012 4817528.0 Alabama 52423.0 91.9\n", + "WA total 2012 6895318.0 Washington 71303.0 96.7\n", + "TX total 2012 26060796.0 Texas 268601.0 97.0\n", + "MI total 2012 9882519.0 Michigan 96810.0 102.1\n", + "KY total 2012 4379730.0 Kentucky 40411.0 108.4\n", + "HI total 2012 1390090.0 Hawaii 10932.0 127.2\n", + "NH total 2012 1321617.0 New Hampshire 9351.0 141.3\n", + "SC total 2012 4723417.0 South Carolina 32007.0 147.6\n", + "TN total 2012 6454914.0 Tennessee 42146.0 153.2\n", + "GA total 2012 9915646.0 Georgia 59441.0 166.8\n", + "IN total 2012 6537782.0 Indiana 36420.0 179.5\n", + "NC total 2012 9748364.0 North Carolina 53821.0 181.1\n", + "VA total 2012 8186628.0 Virginia 42769.0 191.4\n", + "IL total 2012 12868192.0 Illinois 57918.0 222.2\n", + "CA total 2012 37999878.0 California 163707.0 232.1\n", + "OH total 2012 11553031.0 Ohio 44828.0 257.7\n", + "PA total 2012 12764475.0 Pennsylvania 46058.0 277.1\n", + "FL total 2012 19320749.0 Florida 65758.0 293.8\n", + "NY total 2012 19576125.0 New York 54475.0 359.4\n", + "DE total 2012 917053.0 Delaware 1954.0 469.3\n", + "MD total 2012 5884868.0 Maryland 12407.0 474.3\n", + "MA total 2012 6645303.0 Massachusetts 10555.0 629.6\n", + "CT total 2012 3591765.0 Connecticut 5544.0 647.9\n", + "RI total 2012 1050304.0 Rhode Island 1545.0 679.8\n", + "NJ total 2012 8867749.0 New Jersey 8722.0 1016.7\n", + "PR total 2012 3651545.0 Puerto Rico 3515.0 1038.8\n", + "DC total 2012 633427.0 District of Columbia 68.0 9315.1" + ] + }, + "execution_count": 80, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pop5.sort_values(by = 'pop_density')" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
agesyearpopulationstatearea (sq. mi)pop_density
state/region
DCtotal2012633427.0District of Columbia68.09315.1
PRtotal20123651545.0Puerto Rico3515.01038.8
NJtotal20128867749.0New Jersey8722.01016.7
RItotal20121050304.0Rhode Island1545.0679.8
CTtotal20123591765.0Connecticut5544.0647.9
MAtotal20126645303.0Massachusetts10555.0629.6
MDtotal20125884868.0Maryland12407.0474.3
DEtotal2012917053.0Delaware1954.0469.3
NYtotal201219576125.0New York54475.0359.4
FLtotal201219320749.0Florida65758.0293.8
PAtotal201212764475.0Pennsylvania46058.0277.1
OHtotal201211553031.0Ohio44828.0257.7
CAtotal201237999878.0California163707.0232.1
ILtotal201212868192.0Illinois57918.0222.2
VAtotal20128186628.0Virginia42769.0191.4
NCtotal20129748364.0North Carolina53821.0181.1
INtotal20126537782.0Indiana36420.0179.5
GAtotal20129915646.0Georgia59441.0166.8
TNtotal20126454914.0Tennessee42146.0153.2
SCtotal20124723417.0South Carolina32007.0147.6
NHtotal20121321617.0New Hampshire9351.0141.3
HItotal20121390090.0Hawaii10932.0127.2
KYtotal20124379730.0Kentucky40411.0108.4
MItotal20129882519.0Michigan96810.0102.1
TXtotal201226060796.0Texas268601.097.0
WAtotal20126895318.0Washington71303.096.7
ALtotal20124817528.0Alabama52423.091.9
LAtotal20124602134.0Louisiana51843.088.8
WItotal20125724554.0Wisconsin65503.087.4
MOtotal20126024522.0Missouri69709.086.4
USAtotal2012313873685.0United State3790399.082.8
WVtotal20121856680.0West Virginia24231.076.6
VTtotal2012625953.0Vermont9615.065.1
MNtotal20125379646.0Minnesota86943.061.9
MStotal20122986450.0Mississippi48434.061.7
AZtotal20126551149.0Arizona114006.057.5
ARtotal20122949828.0Arkansas53182.055.5
OKtotal20123815780.0Oklahoma69903.054.6
IAtotal20123075039.0Iowa56276.054.6
COtotal20125189458.0Colorado104100.049.9
ORtotal20123899801.0Oregon98386.039.6
MEtotal20121328501.0Maine35387.037.5
KStotal20122885398.0Kansas82282.035.1
UTtotal20122854871.0Utah84904.033.6
NVtotal20122754354.0Nevada110567.024.9
NEtotal20121855350.0Nebraska77358.024.0
IDtotal20121595590.0Idaho83574.019.1
NMtotal20122083540.0New Mexico121593.017.1
SDtotal2012834047.0South Dakota77121.010.8
NDtotal2012701345.0North Dakota70704.09.9
MTtotal20121005494.0Montana147046.06.8
WYtotal2012576626.0Wyoming97818.05.9
AKtotal2012730307.0Alaska656425.01.1
\n", + "
" + ], + "text/plain": [ + " ages year population state area (sq. mi) pop_density\n", + "state/region \n", + "DC total 2012 633427.0 District of Columbia 68.0 9315.1\n", + "PR total 2012 3651545.0 Puerto Rico 3515.0 1038.8\n", + "NJ total 2012 8867749.0 New Jersey 8722.0 1016.7\n", + "RI total 2012 1050304.0 Rhode Island 1545.0 679.8\n", + "CT total 2012 3591765.0 Connecticut 5544.0 647.9\n", + "MA total 2012 6645303.0 Massachusetts 10555.0 629.6\n", + "MD total 2012 5884868.0 Maryland 12407.0 474.3\n", + "DE total 2012 917053.0 Delaware 1954.0 469.3\n", + "NY total 2012 19576125.0 New York 54475.0 359.4\n", + "FL total 2012 19320749.0 Florida 65758.0 293.8\n", + "PA total 2012 12764475.0 Pennsylvania 46058.0 277.1\n", + "OH total 2012 11553031.0 Ohio 44828.0 257.7\n", + "CA total 2012 37999878.0 California 163707.0 232.1\n", + "IL total 2012 12868192.0 Illinois 57918.0 222.2\n", + "VA total 2012 8186628.0 Virginia 42769.0 191.4\n", + "NC total 2012 9748364.0 North Carolina 53821.0 181.1\n", + "IN total 2012 6537782.0 Indiana 36420.0 179.5\n", + "GA total 2012 9915646.0 Georgia 59441.0 166.8\n", + "TN total 2012 6454914.0 Tennessee 42146.0 153.2\n", + "SC total 2012 4723417.0 South Carolina 32007.0 147.6\n", + "NH total 2012 1321617.0 New Hampshire 9351.0 141.3\n", + "HI total 2012 1390090.0 Hawaii 10932.0 127.2\n", + "KY total 2012 4379730.0 Kentucky 40411.0 108.4\n", + "MI total 2012 9882519.0 Michigan 96810.0 102.1\n", + "TX total 2012 26060796.0 Texas 268601.0 97.0\n", + "WA total 2012 6895318.0 Washington 71303.0 96.7\n", + "AL total 2012 4817528.0 Alabama 52423.0 91.9\n", + "LA total 2012 4602134.0 Louisiana 51843.0 88.8\n", + "WI total 2012 5724554.0 Wisconsin 65503.0 87.4\n", + "MO total 2012 6024522.0 Missouri 69709.0 86.4\n", + "USA total 2012 313873685.0 United State 3790399.0 82.8\n", + "WV total 2012 1856680.0 West Virginia 24231.0 76.6\n", + "VT total 2012 625953.0 Vermont 9615.0 65.1\n", + "MN total 2012 5379646.0 Minnesota 86943.0 61.9\n", + "MS total 2012 2986450.0 Mississippi 48434.0 61.7\n", + "AZ total 2012 6551149.0 Arizona 114006.0 57.5\n", + "AR total 2012 2949828.0 Arkansas 53182.0 55.5\n", + "OK total 2012 3815780.0 Oklahoma 69903.0 54.6\n", + "IA total 2012 3075039.0 Iowa 56276.0 54.6\n", + "CO total 2012 5189458.0 Colorado 104100.0 49.9\n", + "OR total 2012 3899801.0 Oregon 98386.0 39.6\n", + "ME total 2012 1328501.0 Maine 35387.0 37.5\n", + "KS total 2012 2885398.0 Kansas 82282.0 35.1\n", + "UT total 2012 2854871.0 Utah 84904.0 33.6\n", + "NV total 2012 2754354.0 Nevada 110567.0 24.9\n", + "NE total 2012 1855350.0 Nebraska 77358.0 24.0\n", + "ID total 2012 1595590.0 Idaho 83574.0 19.1\n", + "NM total 2012 2083540.0 New Mexico 121593.0 17.1\n", + "SD total 2012 834047.0 South Dakota 77121.0 10.8\n", + "ND total 2012 701345.0 North Dakota 70704.0 9.9\n", + "MT total 2012 1005494.0 Montana 147046.0 6.8\n", + "WY total 2012 576626.0 Wyoming 97818.0 5.9\n", + "AK total 2012 730307.0 Alaska 656425.0 1.1" + ] + }, + "execution_count": 81, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pop5.sort_values(by='pop_density',ascending=False)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Day76-90/code/cancer_predict.npy b/Day76-90/code/cancer_predict.npy new file mode 100644 index 0000000..a6bf034 Binary files /dev/null and b/Day76-90/code/cancer_predict.npy differ diff --git a/Day76-90/code/cancer_true.npy b/Day76-90/code/cancer_true.npy new file mode 100644 index 0000000..97e4b66 Binary files /dev/null and b/Day76-90/code/cancer_true.npy differ diff --git a/Day76-90/code/state-abbrevs.csv b/Day76-90/code/state-abbrevs.csv new file mode 100644 index 0000000..6d4db36 --- /dev/null +++ b/Day76-90/code/state-abbrevs.csv @@ -0,0 +1,52 @@ +"state","abbreviation" +"Alabama","AL" +"Alaska","AK" +"Arizona","AZ" +"Arkansas","AR" +"California","CA" +"Colorado","CO" +"Connecticut","CT" +"Delaware","DE" +"District of Columbia","DC" +"Florida","FL" +"Georgia","GA" +"Hawaii","HI" +"Idaho","ID" +"Illinois","IL" +"Indiana","IN" +"Iowa","IA" +"Kansas","KS" +"Kentucky","KY" +"Louisiana","LA" +"Maine","ME" +"Montana","MT" +"Nebraska","NE" +"Nevada","NV" +"New Hampshire","NH" +"New Jersey","NJ" +"New Mexico","NM" +"New York","NY" +"North Carolina","NC" +"North Dakota","ND" +"Ohio","OH" +"Oklahoma","OK" +"Oregon","OR" +"Maryland","MD" +"Massachusetts","MA" +"Michigan","MI" +"Minnesota","MN" +"Mississippi","MS" +"Missouri","MO" +"Pennsylvania","PA" +"Rhode Island","RI" +"South Carolina","SC" +"South Dakota","SD" +"Tennessee","TN" +"Texas","TX" +"Utah","UT" +"Vermont","VT" +"Virginia","VA" +"Washington","WA" +"West Virginia","WV" +"Wisconsin","WI" +"Wyoming","WY" \ No newline at end of file diff --git a/Day76-90/code/state-areas.csv b/Day76-90/code/state-areas.csv new file mode 100644 index 0000000..322345c --- /dev/null +++ b/Day76-90/code/state-areas.csv @@ -0,0 +1,53 @@ +state,area (sq. mi) +Alabama,52423 +Alaska,656425 +Arizona,114006 +Arkansas,53182 +California,163707 +Colorado,104100 +Connecticut,5544 +Delaware,1954 +Florida,65758 +Georgia,59441 +Hawaii,10932 +Idaho,83574 +Illinois,57918 +Indiana,36420 +Iowa,56276 +Kansas,82282 +Kentucky,40411 +Louisiana,51843 +Maine,35387 +Maryland,12407 +Massachusetts,10555 +Michigan,96810 +Minnesota,86943 +Mississippi,48434 +Missouri,69709 +Montana,147046 +Nebraska,77358 +Nevada,110567 +New Hampshire,9351 +New Jersey,8722 +New Mexico,121593 +New York,54475 +North Carolina,53821 +North Dakota,70704 +Ohio,44828 +Oklahoma,69903 +Oregon,98386 +Pennsylvania,46058 +Rhode Island,1545 +South Carolina,32007 +South Dakota,77121 +Tennessee,42146 +Texas,268601 +Utah,84904 +Vermont,9615 +Virginia,42769 +Washington,71303 +West Virginia,24231 +Wisconsin,65503 +Wyoming,97818 +District of Columbia,68 +Puerto Rico,3515 diff --git a/Day76-90/code/state-population.csv b/Day76-90/code/state-population.csv new file mode 100644 index 0000000..c76110e --- /dev/null +++ b/Day76-90/code/state-population.csv @@ -0,0 +1,2545 @@ +state/region,ages,year,population +AL,under18,2012,1117489 +AL,total,2012,4817528 +AL,under18,2010,1130966 +AL,total,2010,4785570 +AL,under18,2011,1125763 +AL,total,2011,4801627 +AL,total,2009,4757938 +AL,under18,2009,1134192 +AL,under18,2013,1111481 +AL,total,2013,4833722 +AL,total,2007,4672840 +AL,under18,2007,1132296 +AL,total,2008,4718206 +AL,under18,2008,1134927 +AL,total,2005,4569805 +AL,under18,2005,1117229 +AL,total,2006,4628981 +AL,under18,2006,1126798 +AL,total,2004,4530729 +AL,under18,2004,1113662 +AL,total,2003,4503491 +AL,under18,2003,1113083 +AL,total,2001,4467634 +AL,under18,2001,1120409 +AL,total,2002,4480089 +AL,under18,2002,1116590 +AL,under18,1999,1121287 +AL,total,1999,4430141 +AL,total,2000,4452173 +AL,under18,2000,1122273 +AL,total,1998,4404701 +AL,under18,1998,1118252 +AL,under18,1997,1122893 +AL,total,1997,4367935 +AL,total,1996,4331103 +AL,total,1995,4296800 +AL,under18,1995,1110553 +AL,under18,1996,1112092 +AL,total,1994,4260229 +AL,total,1993,4214202 +AL,under18,1993,1085606 +AL,under18,1994,1097180 +AL,under18,1992,1072873 +AL,total,1992,4154014 +AL,total,1991,4099156 +AL,under18,1991,1060794 +AL,under18,1990,1050041 +AL,total,1990,4050055 +AK,total,1990,553290 +AK,under18,1990,177502 +AK,total,1992,588736 +AK,under18,1991,182180 +AK,under18,1992,184878 +AK,total,1994,603308 +AK,under18,1994,187439 +AK,total,1991,570193 +AK,total,1993,599434 +AK,under18,1993,187190 +AK,total,1995,604412 +AK,under18,1995,184990 +AK,total,1996,608569 +AK,under18,1996,185360 +AK,under18,1997,188280 +AK,under18,1998,192636 +AK,total,1998,619933 +AK,total,1997,612968 +AK,under18,1999,191422 +AK,total,1999,624779 +AK,total,2000,627963 +AK,under18,2000,190615 +AK,total,2001,633714 +AK,under18,2001,188771 +AK,total,2002,642337 +AK,under18,2002,188482 +AK,total,2003,648414 +AK,under18,2003,186843 +AK,total,2004,659286 +AK,under18,2004,186335 +AK,total,2005,666946 +AK,under18,2005,185304 +AK,total,2006,675302 +AK,under18,2006,185580 +AK,total,2007,680300 +AK,under18,2007,184344 +AK,total,2008,687455 +AK,under18,2008,183124 +AK,under18,2013,188132 +AK,total,2013,735132 +AK,total,2009,698895 +AK,under18,2009,186351 +AK,under18,2010,187902 +AK,total,2010,713868 +AK,under18,2011,188329 +AK,total,2011,723375 +AK,under18,2012,188162 +AK,total,2012,730307 +AZ,under18,2012,1617149 +AZ,total,2012,6551149 +AZ,under18,2011,1616353 +AZ,total,2011,6468796 +AZ,under18,2010,1628563 +AZ,total,2010,6408790 +AZ,under18,2013,1616814 +AZ,total,2013,6626624 +AZ,total,2009,6343154 +AZ,under18,2009,1627343 +AZ,total,2007,6167681 +AZ,under18,2007,1607895 +AZ,total,2008,6280362 +AZ,under18,2008,1628651 +AZ,total,2005,5839077 +AZ,under18,2005,1529168 +AZ,total,2006,6029141 +AZ,under18,2006,1574867 +AZ,total,2004,5652404 +AZ,under18,2004,1484454 +AZ,total,2003,5510364 +AZ,under18,2003,1453671 +AZ,total,2001,5273477 +AZ,under18,2001,1399015 +AZ,total,2002,5396255 +AZ,under18,2002,1427938 +AZ,under18,1999,1332396 +AZ,total,1999,5023823 +AZ,total,2000,5160586 +AZ,under18,2000,1373414 +AZ,total,1998,4883342 +AZ,under18,1998,1285794 +AZ,total,1997,4736990 +AZ,under18,1997,1237159 +AZ,under18,1996,1215285 +AZ,total,1996,4586940 +AZ,total,1995,4432499 +AZ,under18,1995,1173391 +AZ,total,1993,4065440 +AZ,under18,1993,1094233 +AZ,under18,1994,1119857 +AZ,total,1994,4245089 +AZ,under18,1992,1055572 +AZ,under18,1991,1028285 +AZ,total,1991,3788576 +AZ,total,1992,3915740 +AZ,under18,1990,1006040 +AZ,total,1990,3684097 +AR,under18,1990,620933 +AR,total,1990,2356586 +AR,total,1991,2383144 +AR,under18,1991,626212 +AR,under18,1992,638269 +AR,total,1992,2415984 +AR,under18,1994,653842 +AR,total,1994,2494019 +AR,total,1993,2456303 +AR,under18,1993,643474 +AR,under18,1995,667671 +AR,total,1995,2535399 +AR,under18,1996,677912 +AR,total,1996,2572109 +AR,under18,1998,683637 +AR,total,1997,2601091 +AR,under18,1997,680203 +AR,total,1998,2626289 +AR,total,2000,2678588 +AR,under18,2000,680378 +AR,under18,1999,681940 +AR,total,1999,2651860 +AR,total,2002,2705927 +AR,under18,2002,678798 +AR,total,2001,2691571 +AR,under18,2001,679606 +AR,total,2004,2749686 +AR,under18,2004,683166 +AR,total,2003,2724816 +AR,under18,2003,679579 +AR,total,2006,2821761 +AR,under18,2006,697842 +AR,total,2005,2781097 +AR,under18,2005,689787 +AR,total,2008,2874554 +AR,under18,2008,705725 +AR,total,2007,2848650 +AR,under18,2007,702737 +AR,total,2009,2896843 +AR,under18,2009,707886 +AR,under18,2013,709866 +AR,total,2013,2959373 +AR,under18,2011,710576 +AR,total,2011,2938506 +AR,under18,2010,711947 +AR,total,2010,2922280 +AR,under18,2012,710471 +AR,total,2012,2949828 +CA,under18,2012,9209007 +CA,total,2012,37999878 +CA,under18,2011,9252336 +CA,total,2011,37668681 +CA,under18,2010,9284094 +CA,total,2010,37333601 +CA,under18,2013,9174877 +CA,total,2013,38332521 +CA,total,2009,36961229 +CA,under18,2009,9294501 +CA,total,2007,36250311 +CA,under18,2007,9335620 +CA,total,2008,36604337 +CA,under18,2008,9321621 +CA,total,2005,35827943 +CA,under18,2005,9405565 +CA,total,2006,36021202 +CA,under18,2006,9370884 +CA,total,2003,35253159 +CA,under18,2003,9404594 +CA,total,2004,35574576 +CA,under18,2004,9418497 +CA,total,2001,34479458 +CA,under18,2001,9325466 +CA,total,2002,34871843 +CA,under18,2002,9365142 +CA,under18,1999,9207878 +CA,total,1999,33499204 +CA,total,2000,33987977 +CA,under18,2000,9267089 +CA,under18,1998,9163238 +CA,total,1998,32987675 +CA,under18,1997,9135359 +CA,total,1997,32486010 +CA,under18,1996,9079519 +CA,total,1996,32018834 +CA,total,1995,31696582 +CA,under18,1995,8920578 +CA,total,1993,31274928 +CA,under18,1993,8624810 +CA,under18,1994,8790058 +CA,total,1994,31484435 +CA,total,1991,30470736 +CA,under18,1991,8245605 +CA,under18,1992,8439647 +CA,total,1992,30974659 +CA,under18,1990,7980501 +CA,total,1990,29959515 +CO,total,1990,3307618 +CO,under18,1990,881640 +CO,total,1992,3495939 +CO,under18,1992,925577 +CO,under18,1991,896537 +CO,total,1991,3387119 +CO,total,1994,3724168 +CO,under18,1994,966412 +CO,under18,1993,947806 +CO,total,1993,3613734 +CO,under18,1995,984310 +CO,total,1995,3826653 +CO,total,1996,3919972 +CO,under18,1996,1003946 +CO,under18,1997,1030557 +CO,total,1997,4018293 +CO,total,1998,4116639 +CO,under18,1998,1060066 +CO,total,2000,4326921 +CO,under18,2000,1106676 +CO,total,1999,4226018 +CO,under18,1999,1083938 +CO,total,2002,4490406 +CO,under18,2002,1138273 +CO,total,2001,4425687 +CO,under18,2001,1126647 +CO,total,2004,4575013 +CO,under18,2004,1146369 +CO,total,2003,4528732 +CO,under18,2003,1144597 +CO,total,2006,4720423 +CO,under18,2006,1171832 +CO,total,2005,4631888 +CO,under18,2005,1156399 +CO,total,2008,4889730 +CO,under18,2008,1203289 +CO,total,2007,4803868 +CO,under18,2007,1189434 +CO,total,2009,4972195 +CO,under18,2009,1217213 +CO,under18,2013,1237932 +CO,total,2013,5268367 +CO,under18,2010,1226619 +CO,total,2010,5048196 +CO,under18,2011,1230178 +CO,total,2011,5118400 +CO,under18,2012,1232864 +CO,total,2012,5189458 +CT,under18,2012,794959 +CT,total,2012,3591765 +CT,under18,2011,805109 +CT,total,2011,3588948 +CT,under18,2010,814187 +CT,total,2010,3579210 +CT,under18,2013,785566 +CT,total,2013,3596080 +CT,total,2009,3561807 +CT,under18,2009,820839 +CT,total,2007,3527270 +CT,under18,2007,833484 +CT,total,2008,3545579 +CT,under18,2008,826626 +CT,total,2005,3506956 +CT,under18,2005,844034 +CT,total,2006,3517460 +CT,under18,2006,839372 +CT,total,2003,3484336 +CT,under18,2003,851115 +CT,total,2004,3496094 +CT,under18,2004,848979 +CT,total,2001,3432835 +CT,under18,2001,845850 +CT,total,2002,3458749 +CT,under18,2002,848877 +CT,total,1999,3386401 +CT,under18,1999,834654 +CT,total,2000,3411777 +CT,under18,2000,842242 +CT,under18,1998,824600 +CT,total,1998,3365352 +CT,total,1997,3349348 +CT,under18,1997,814373 +CT,under18,1996,811855 +CT,total,1996,3336685 +CT,total,1995,3324144 +CT,under18,1995,808623 +CT,total,1993,3309175 +CT,under18,1993,790749 +CT,under18,1994,801231 +CT,total,1994,3316121 +CT,under18,1991,766304 +CT,total,1991,3302895 +CT,under18,1992,777264 +CT,total,1992,3300712 +CT,total,1990,3291967 +CT,under18,1990,752666 +DE,under18,1990,165628 +DE,total,1990,669567 +DE,under18,1992,174166 +DE,total,1992,694927 +DE,total,1991,683080 +DE,under18,1991,169910 +DE,total,1994,717545 +DE,under18,1994,180833 +DE,total,1993,706378 +DE,under18,1993,176916 +DE,under18,1995,181736 +DE,total,1995,729735 +DE,total,1996,740978 +DE,under18,1996,184021 +DE,under18,1997,186607 +DE,total,1997,751487 +DE,total,1998,763335 +DE,under18,1998,189302 +DE,total,2000,786373 +DE,under18,2000,194914 +DE,total,1999,774990 +DE,under18,1999,192510 +DE,total,2002,806169 +DE,under18,2002,196946 +DE,total,2001,795699 +DE,under18,2001,196038 +DE,total,2004,830803 +DE,under18,2004,199631 +DE,total,2003,818003 +DE,under18,2003,198045 +DE,total,2006,859268 +DE,under18,2006,203729 +DE,total,2005,845150 +DE,under18,2005,201988 +DE,total,2008,883874 +DE,under18,2008,206116 +DE,total,2007,871749 +DE,under18,2007,205155 +DE,under18,2013,203558 +DE,total,2013,925749 +DE,total,2009,891730 +DE,under18,2009,206213 +DE,under18,2010,205478 +DE,total,2010,899711 +DE,under18,2011,204801 +DE,total,2011,907985 +DE,under18,2012,204586 +DE,total,2012,917053 +DC,under18,2012,107642 +DC,total,2012,633427 +DC,under18,2011,103906 +DC,total,2011,619624 +DC,under18,2010,101309 +DC,total,2010,605125 +DC,under18,2013,111474 +DC,total,2013,646449 +DC,total,2009,592228 +DC,under18,2009,102098 +DC,total,2007,574404 +DC,under18,2007,104126 +DC,total,2008,580236 +DC,under18,2008,102257 +DC,total,2005,567136 +DC,under18,2005,107187 +DC,total,2006,570681 +DC,under18,2006,105651 +DC,total,2003,568502 +DC,under18,2003,111403 +DC,total,2004,567754 +DC,under18,2004,109756 +DC,total,2001,574504 +DC,under18,2001,114625 +DC,total,2002,573158 +DC,under18,2002,113822 +DC,total,1999,570220 +DC,under18,1999,115003 +DC,total,2000,572046 +DC,under18,2000,114503 +DC,under18,1998,113839 +DC,total,1998,565232 +DC,under18,1997,119531 +DC,total,1997,567739 +DC,under18,1996,121210 +DC,total,1996,572379 +DC,total,1995,580519 +DC,under18,1995,123620 +DC,total,1993,595302 +DC,under18,1993,120471 +DC,under18,1994,122170 +DC,total,1994,589240 +DC,total,1991,600870 +DC,under18,1991,116825 +DC,under18,1992,118636 +DC,total,1992,597567 +DC,under18,1990,112632 +DC,total,1990,605321 +FL,total,1990,13033307 +FL,under18,1990,2988807 +FL,under18,1991,3045638 +FL,total,1991,13369798 +FL,total,1994,14239444 +FL,under18,1994,3299887 +FL,under18,1993,3214066 +FL,total,1993,13927185 +FL,total,1992,13650553 +FL,under18,1992,3120439 +FL,under18,1995,3366468 +FL,total,1995,14537875 +FL,total,1996,14853360 +FL,under18,1996,3431695 +FL,under18,1998,3557561 +FL,under18,1997,3502269 +FL,total,1997,15186304 +FL,total,1998,15486559 +FL,total,1999,15759421 +FL,under18,1999,3611711 +FL,total,2000,16047515 +FL,under18,2000,3654880 +FL,total,2001,16356966 +FL,under18,2001,3714439 +FL,total,2002,16689370 +FL,under18,2002,3774624 +FL,total,2003,17004085 +FL,under18,2003,3820876 +FL,total,2004,17415318 +FL,under18,2004,3890734 +FL,total,2005,17842038 +FL,under18,2005,3968178 +FL,total,2006,18166990 +FL,under18,2006,4022912 +FL,total,2007,18367842 +FL,under18,2007,4031098 +FL,total,2008,18527305 +FL,under18,2008,4018372 +FL,total,2009,18652644 +FL,under18,2009,3997283 +FL,under18,2013,4026674 +FL,total,2013,19552860 +FL,under18,2010,3999532 +FL,total,2010,18846054 +FL,under18,2011,4002550 +FL,total,2011,19083482 +FL,under18,2012,4012421 +FL,total,2012,19320749 +GA,total,2012,9915646 +GA,under18,2012,2487831 +GA,under18,2011,2488898 +GA,total,2011,9810181 +GA,under18,2010,2490884 +GA,total,2010,9713248 +GA,total,2013,9992167 +GA,total,2009,9620846 +GA,under18,2009,2485781 +GA,under18,2013,2489709 +GA,total,2007,9349988 +GA,under18,2007,2456249 +GA,total,2008,9504843 +GA,under18,2008,2479097 +GA,total,2005,8925922 +GA,under18,2005,2353604 +GA,total,2006,9155813 +GA,under18,2006,2406014 +GA,total,2003,8622793 +GA,under18,2003,2278710 +GA,total,2004,8769252 +GA,under18,2004,2308855 +GA,total,2001,8377038 +GA,under18,2001,2215390 +GA,total,2002,8508256 +GA,under18,2002,2249784 +GA,total,1999,8045965 +GA,under18,1999,2130698 +GA,total,2000,8227303 +GA,under18,2000,2176576 +GA,total,1997,7685099 +GA,under18,1997,2034163 +GA,under18,1998,2078998 +GA,total,1998,7863536 +GA,under18,1996,1993171 +GA,total,1996,7501069 +GA,total,1995,7328413 +GA,under18,1995,1949818 +GA,under18,1992,1817781 +GA,total,1992,6817203 +GA,total,1993,6978240 +GA,under18,1993,1865021 +GA,under18,1994,1906539 +GA,total,1994,7157165 +GA,total,1991,6653005 +GA,under18,1991,1773675 +GA,under18,1990,1747363 +GA,total,1990,6512602 +HI,under18,1990,279983 +HI,total,1990,1113491 +HI,total,1991,1136754 +HI,under18,1991,287871 +HI,under18,1994,307517 +HI,total,1994,1187536 +HI,total,1993,1172838 +HI,under18,1993,301473 +HI,under18,1992,295124 +HI,total,1992,1158613 +HI,total,1995,1196854 +HI,under18,1995,310325 +HI,under18,1996,311213 +HI,total,1996,1203755 +HI,under18,1998,304576 +HI,total,1998,1215233 +HI,total,1997,1211640 +HI,under18,1997,309465 +HI,total,2000,1213519 +HI,under18,2000,295352 +HI,total,1999,1210300 +HI,under18,1999,299680 +HI,total,2002,1239613 +HI,under18,2002,293600 +HI,total,2001,1225948 +HI,under18,2001,294133 +HI,total,2004,1273569 +HI,under18,2004,298103 +HI,total,2003,1251154 +HI,under18,2003,294519 +HI,total,2006,1309731 +HI,under18,2006,299313 +HI,total,2005,1292729 +HI,under18,2005,298497 +HI,total,2008,1332213 +HI,under18,2008,301094 +HI,total,2007,1315675 +HI,under18,2007,300207 +HI,under18,2013,307266 +HI,total,2009,1346717 +HI,under18,2009,302796 +HI,total,2013,1404054 +HI,total,2010,1363731 +HI,under18,2010,303812 +HI,total,2011,1376897 +HI,under18,2011,305396 +HI,under18,2012,305981 +HI,total,2012,1390090 +ID,total,2012,1595590 +ID,under18,2012,427177 +ID,under18,2011,428535 +ID,total,2011,1583930 +ID,under18,2010,428961 +ID,total,2010,1570718 +ID,total,2013,1612136 +ID,total,2009,1554439 +ID,under18,2009,426076 +ID,under18,2013,427781 +ID,total,2007,1505105 +ID,under18,2007,415024 +ID,total,2008,1534320 +ID,under18,2008,422347 +ID,total,2005,1428241 +ID,under18,2005,394651 +ID,total,2006,1468669 +ID,under18,2006,404753 +ID,total,2003,1363380 +ID,under18,2003,379241 +ID,total,2004,1391802 +ID,under18,2004,384692 +ID,total,2001,1319962 +ID,under18,2001,373145 +ID,total,2002,1340372 +ID,under18,2002,375986 +ID,total,1999,1275674 +ID,under18,1999,366689 +ID,total,2000,1299430 +ID,under18,2000,370430 +ID,total,1997,1228520 +ID,under18,1997,357779 +ID,under18,1998,362189 +ID,total,1998,1252330 +ID,under18,1996,353824 +ID,total,1996,1203083 +ID,total,1995,1177322 +ID,under18,1995,349248 +ID,under18,1992,324972 +ID,total,1992,1071685 +ID,total,1993,1108768 +ID,under18,1993,333838 +ID,under18,1994,344242 +ID,total,1994,1145140 +ID,total,1991,1041316 +ID,under18,1991,316732 +ID,under18,1990,313373 +ID,total,1990,1012384 +IL,under18,1990,2940837 +IL,total,1990,11453316 +IL,total,1991,11568964 +IL,under18,1991,2988715 +IL,under18,1994,3110938 +IL,total,1994,11912585 +IL,total,1993,11809579 +IL,under18,1993,3066541 +IL,under18,1992,3033427 +IL,total,1992,11694184 +IL,total,1995,12008437 +IL,under18,1995,3152984 +IL,under18,1996,3192916 +IL,total,1996,12101997 +IL,under18,1998,3225252 +IL,total,1998,12271847 +IL,total,1997,12185715 +IL,under18,1997,3222114 +IL,total,2000,12434161 +IL,under18,2000,3244944 +IL,total,1999,12359020 +IL,under18,1999,3240034 +IL,total,2002,12525556 +IL,under18,2002,3238362 +IL,total,2001,12488445 +IL,under18,2001,3243617 +IL,total,2004,12589773 +IL,under18,2004,3211599 +IL,total,2003,12556006 +IL,under18,2003,3225547 +IL,total,2006,12643955 +IL,under18,2006,3181246 +IL,total,2005,12609903 +IL,under18,2005,3197318 +IL,total,2008,12747038 +IL,under18,2008,3153401 +IL,total,2007,12695866 +IL,under18,2007,3170134 +IL,under18,2013,3023307 +IL,total,2009,12796778 +IL,under18,2009,3138406 +IL,total,2013,12882135 +IL,total,2010,12839695 +IL,under18,2010,3122092 +IL,total,2011,12855970 +IL,under18,2011,3089833 +IL,under18,2012,3057042 +IL,total,2012,12868192 +IN,total,2012,6537782 +IN,under18,2012,1589655 +IN,under18,2011,1598091 +IN,total,2011,6516336 +IN,under18,2010,1605883 +IN,total,2010,6489965 +IN,total,2013,6570902 +IN,total,2009,6459325 +IN,under18,2009,1609704 +IN,under18,2013,1586027 +IN,total,2007,6379599 +IN,under18,2007,1609494 +IN,total,2008,6424806 +IN,under18,2008,1611494 +IN,total,2005,6278616 +IN,under18,2005,1593898 +IN,total,2006,6332669 +IN,under18,2006,1603107 +IN,total,2003,6196638 +IN,under18,2003,1582560 +IN,total,2004,6233007 +IN,under18,2004,1586281 +IN,total,2001,6127760 +IN,under18,2001,1579527 +IN,total,2002,6155967 +IN,under18,2002,1580814 +IN,total,1999,6044970 +IN,under18,1999,1566079 +IN,total,2000,6091866 +IN,under18,2000,1574989 +IN,total,1997,5955267 +IN,under18,1997,1539270 +IN,under18,1998,1551960 +IN,total,1998,5998881 +IN,under18,1996,1517961 +IN,total,1996,5906013 +IN,total,1995,5851459 +IN,under18,1995,1507916 +IN,under18,1992,1461650 +IN,total,1992,5674547 +IN,total,1993,5739019 +IN,under18,1993,1473007 +IN,under18,1994,1491802 +IN,total,1994,5793526 +IN,total,1991,5616388 +IN,under18,1991,1450759 +IN,under18,1990,1437209 +IN,total,1990,5557798 +IA,under18,1990,719366 +IA,total,1990,2781018 +IA,total,1991,2797613 +IA,under18,1991,724446 +IA,under18,1994,728397 +IA,total,1994,2850746 +IA,total,1993,2836972 +IA,under18,1993,727751 +IA,under18,1992,724798 +IA,total,1992,2818401 +IA,total,1995,2867373 +IA,under18,1995,726961 +IA,under18,1996,729177 +IA,total,1996,2880001 +IA,under18,1998,729943 +IA,total,1998,2902872 +IA,total,1997,2891119 +IA,under18,1997,729806 +IA,total,2000,2929067 +IA,under18,2000,733337 +IA,total,1999,2917634 +IA,under18,1999,732671 +IA,total,2002,2934234 +IA,under18,2002,723685 +IA,total,2001,2931997 +IA,under18,2001,728601 +IA,total,2004,2953635 +IA,under18,2004,718708 +IA,total,2003,2941999 +IA,under18,2003,720102 +IA,total,2006,2982644 +IA,under18,2006,721703 +IA,total,2005,2964454 +IA,under18,2005,718488 +IA,total,2008,3016734 +IA,under18,2008,725658 +IA,total,2007,2999212 +IA,under18,2007,723632 +IA,under18,2013,724032 +IA,total,2009,3032870 +IA,under18,2009,726969 +IA,total,2013,3090416 +IA,total,2010,3050314 +IA,under18,2010,727717 +IA,total,2011,3064102 +IA,under18,2011,725522 +IA,under18,2012,723917 +IA,total,2012,3075039 +KS,total,2012,2885398 +KS,under18,2012,726668 +KS,under18,2011,726787 +KS,total,2011,2869548 +KS,under18,2010,727729 +KS,total,2010,2858910 +KS,total,2013,2893957 +KS,total,2009,2832704 +KS,under18,2009,721841 +KS,under18,2013,724092 +KS,total,2007,2783785 +KS,under18,2007,711005 +KS,total,2008,2808076 +KS,under18,2008,714689 +KS,total,2005,2745299 +KS,under18,2005,704689 +KS,total,2006,2762931 +KS,under18,2006,705277 +KS,total,2003,2723004 +KS,under18,2003,707847 +KS,total,2004,2734373 +KS,under18,2004,705456 +KS,total,2001,2702162 +KS,under18,2001,710923 +KS,total,2002,2713535 +KS,under18,2002,709416 +KS,total,1999,2678338 +KS,under18,1999,713022 +KS,total,2000,2693681 +KS,under18,2000,713887 +KS,total,1997,2635292 +KS,under18,1997,704001 +KS,under18,1998,710402 +KS,total,1998,2660598 +KS,under18,1996,696298 +KS,total,1996,2614554 +KS,total,1995,2601008 +KS,under18,1995,694124 +KS,under18,1992,680871 +KS,total,1992,2532395 +KS,total,1993,2556547 +KS,under18,1993,687262 +KS,under18,1994,693673 +KS,total,1994,2580513 +KS,total,1991,2498722 +KS,under18,1991,672033 +KS,under18,1990,662641 +KS,total,1990,2481349 +KY,under18,1990,945951 +KY,total,1990,3694048 +KY,total,1991,3722328 +KY,under18,1991,951512 +KY,under18,1994,981439 +KY,total,1994,3849088 +KY,total,1993,3812206 +KY,under18,1993,971134 +KY,under18,1992,963861 +KY,total,1992,3765469 +KY,total,1995,3887427 +KY,under18,1995,984486 +KY,under18,1996,987062 +KY,total,1996,3919536 +KY,under18,1998,997296 +KY,total,1998,3985391 +KY,total,1997,3952747 +KY,under18,1997,1002609 +KY,total,2000,4049021 +KY,under18,2000,994984 +KY,total,1999,4018053 +KY,under18,1999,996382 +KY,total,2002,4089875 +KY,under18,2002,995251 +KY,total,2001,4068132 +KY,under18,2001,994105 +KY,total,2004,4146101 +KY,under18,2004,998459 +KY,total,2003,4117170 +KY,under18,2003,998485 +KY,total,2006,4219239 +KY,under18,2006,1011295 +KY,total,2005,4182742 +KY,under18,2005,1004020 +KY,total,2008,4289878 +KY,under18,2008,1022001 +KY,total,2007,4256672 +KY,under18,2007,1016288 +KY,under18,2013,1014004 +KY,total,2009,4317074 +KY,under18,2009,1021710 +KY,total,2013,4395295 +KY,total,2010,4347698 +KY,under18,2010,1023679 +KY,total,2011,4366869 +KY,under18,2011,1021926 +KY,under18,2012,1017350 +KY,total,2012,4379730 +LA,total,2012,4602134 +LA,under18,2012,1114620 +LA,under18,2011,1116579 +LA,total,2011,4575197 +LA,under18,2010,1118576 +LA,total,2010,4545392 +LA,total,2013,4625470 +LA,total,2009,4491648 +LA,under18,2009,1114228 +LA,under18,2013,1112957 +LA,total,2007,4375581 +LA,under18,2007,1096642 +LA,total,2008,4435586 +LA,under18,2008,1108728 +LA,total,2005,4576628 +LA,under18,2005,1177954 +LA,total,2006,4302665 +LA,under18,2006,1078779 +LA,total,2003,4521042 +LA,under18,2003,1188070 +LA,total,2004,4552238 +LA,under18,2004,1182731 +LA,total,2001,4477875 +LA,under18,2001,1204187 +LA,total,2002,4497267 +LA,under18,2002,1194819 +LA,total,2000,4471885 +LA,under18,2000,1217670 +LA,total,1999,4460811 +LA,under18,1999,1227167 +LA,total,1997,4421072 +LA,under18,1997,1239665 +LA,under18,1998,1232984 +LA,total,1998,4440344 +LA,under18,1996,1244627 +LA,total,1996,4398877 +LA,total,1995,4378779 +LA,under18,1995,1250112 +LA,under18,1992,1237034 +LA,total,1992,4293003 +LA,total,1993,4316428 +LA,under18,1993,1239161 +LA,under18,1994,1247631 +LA,total,1994,4347481 +LA,total,1991,4253279 +LA,under18,1991,1222330 +LA,under18,1990,1205984 +LA,total,1990,4221532 +ME,under18,1990,308066 +ME,total,1990,1231719 +ME,total,1991,1237081 +ME,under18,1991,309871 +ME,under18,1994,311570 +ME,total,1994,1242662 +ME,total,1993,1242302 +ME,under18,1993,310966 +ME,under18,1992,310679 +ME,total,1992,1238508 +ME,total,1995,1243481 +ME,under18,1995,309173 +ME,under18,1996,307740 +ME,total,1996,1249060 +ME,under18,1998,304496 +ME,total,1998,1259127 +ME,total,1997,1254774 +ME,under18,1997,305097 +ME,total,1999,1266808 +ME,under18,1999,302321 +ME,total,2000,1277072 +ME,under18,2000,301407 +ME,total,2002,1295960 +ME,under18,2002,298595 +ME,total,2001,1285692 +ME,under18,2001,300088 +ME,total,2004,1313688 +ME,under18,2004,294791 +ME,total,2003,1306513 +ME,under18,2003,296786 +ME,total,2006,1323619 +ME,under18,2006,288945 +ME,total,2005,1318787 +ME,under18,2005,292039 +ME,total,2008,1330509 +ME,under18,2008,282204 +ME,total,2007,1327040 +ME,under18,2007,286185 +ME,under18,2013,261276 +ME,total,2009,1329590 +ME,under18,2009,277946 +ME,total,2013,1328302 +ME,total,2010,1327366 +ME,under18,2010,273061 +ME,total,2011,1327844 +ME,under18,2011,268737 +ME,under18,2012,264846 +ME,total,2012,1328501 +MD,total,2012,5884868 +MD,under18,2012,1346235 +MD,under18,2011,1348766 +MD,total,2011,5840241 +MD,under18,2010,1351983 +MD,total,2010,5787193 +MD,total,2013,5928814 +MD,total,2009,5730388 +MD,under18,2009,1353631 +MD,under18,2013,1344522 +MD,total,2007,5653408 +MD,under18,2007,1369563 +MD,total,2008,5684965 +MD,under18,2008,1359214 +MD,total,2005,5592379 +MD,under18,2005,1382966 +MD,total,2006,5627367 +MD,under18,2006,1377756 +MD,total,2003,5496269 +MD,under18,2003,1379641 +MD,total,2004,5546935 +MD,under18,2004,1383450 +MD,total,2001,5374691 +MD,under18,2001,1366552 +MD,total,2002,5440389 +MD,under18,2002,1375354 +MD,total,2000,5311034 +MD,under18,2000,1356961 +MD,total,1999,5254509 +MD,under18,1999,1348659 +MD,total,1997,5157328 +MD,under18,1997,1321306 +MD,under18,1998,1338727 +MD,total,1998,5204464 +MD,under18,1996,1303816 +MD,total,1996,5111986 +MD,total,1995,5070033 +MD,under18,1995,1300695 +MD,under18,1992,1235498 +MD,total,1992,4923369 +MD,total,1993,4971889 +MD,under18,1993,1261738 +MD,under18,1994,1280772 +MD,total,1994,5023060 +MD,total,1991,4867641 +MD,under18,1991,1208898 +MD,under18,1990,1180426 +MD,total,1990,4799770 +MA,under18,1990,1353806 +MA,total,1990,6022639 +MA,total,1991,6018470 +MA,under18,1991,1375110 +MA,under18,1994,1437069 +MA,total,1994,6095241 +MA,total,1993,6060569 +MA,under18,1993,1415724 +MA,under18,1992,1390188 +MA,total,1992,6028709 +MA,total,1995,6141445 +MA,under18,1995,1453489 +MA,under18,1996,1468614 +MA,total,1996,6179756 +MA,under18,1998,1491652 +MA,total,1998,6271838 +MA,total,1997,6226058 +MA,under18,1997,1478203 +MA,total,1999,6317345 +MA,under18,1999,1495818 +MA,total,2000,6361104 +MA,under18,2000,1501334 +MA,total,2001,6397634 +MA,under18,2001,1505028 +MA,total,2002,6417206 +MA,under18,2002,1502652 +MA,total,2004,6412281 +MA,under18,2004,1479541 +MA,total,2003,6422565 +MA,under18,2003,1493372 +MA,total,2006,6410084 +MA,under18,2006,1450202 +MA,total,2005,6403290 +MA,under18,2005,1464140 +MA,total,2008,6468967 +MA,under18,2008,1429727 +MA,total,2007,6431559 +MA,under18,2007,1439757 +MA,under18,2013,1393946 +MA,total,2009,6517613 +MA,under18,2009,1422935 +MA,total,2013,6692824 +MA,total,2010,6563263 +MA,under18,2010,1415962 +MA,total,2011,6606285 +MA,under18,2011,1407240 +MA,under18,2012,1399417 +MA,total,2012,6645303 +MI,total,2012,9882519 +MI,under18,2012,2269365 +MI,under18,2011,2299116 +MI,total,2011,9874589 +MI,under18,2010,2333121 +MI,total,2010,9876149 +MI,total,2013,9895622 +MI,total,2009,9901591 +MI,under18,2009,2372603 +MI,under18,2013,2245201 +MI,total,2007,10001284 +MI,under18,2007,2470063 +MI,total,2008,9946889 +MI,under18,2008,2418879 +MI,total,2005,10051137 +MI,under18,2005,2531839 +MI,total,2006,10036081 +MI,under18,2006,2503548 +MI,total,2003,10041152 +MI,under18,2003,2569080 +MI,total,2004,10055315 +MI,under18,2004,2553314 +MI,total,2002,10015710 +MI,under18,2002,2584310 +MI,total,2001,9991120 +MI,under18,2001,2593310 +MI,total,2000,9952450 +MI,under18,2000,2596114 +MI,total,1999,9897116 +MI,under18,1999,2591944 +MI,total,1997,9809051 +MI,under18,1997,2582270 +MI,under18,1998,2586343 +MI,total,1998,9847942 +MI,under18,1996,2569745 +MI,total,1996,9758645 +MI,total,1995,9676211 +MI,under18,1995,2556799 +MI,under18,1992,2501765 +MI,total,1992,9479065 +MI,total,1993,9540114 +MI,under18,1993,2522249 +MI,under18,1994,2535196 +MI,total,1994,9597737 +MI,total,1991,9400446 +MI,under18,1991,2484957 +MI,under18,1990,2459633 +MI,total,1990,9311319 +MN,under18,1990,1176680 +MN,total,1990,4389857 +MN,total,1991,4440859 +MN,under18,1991,1191207 +MN,under18,1994,1238949 +MN,total,1994,4610355 +MN,total,1993,4555956 +MN,under18,1993,1226723 +MN,under18,1992,1213068 +MN,total,1992,4495572 +MN,total,1995,4660180 +MN,under18,1995,1245932 +MN,under18,1996,1252722 +MN,total,1996,4712827 +MN,under18,1998,1275940 +MN,total,1998,4813412 +MN,total,1997,4763390 +MN,under18,1997,1264250 +MN,total,1999,4873481 +MN,under18,1999,1283102 +MN,total,2000,4933692 +MN,under18,2000,1289715 +MN,total,2001,4982796 +MN,under18,2001,1291261 +MN,total,2002,5018935 +MN,under18,2002,1288795 +MN,total,2004,5087713 +MN,under18,2004,1281946 +MN,total,2003,5053572 +MN,under18,2003,1283687 +MN,total,2006,5163555 +MN,under18,2006,1282381 +MN,total,2005,5119598 +MN,under18,2005,1280557 +MN,total,2008,5247018 +MN,under18,2008,1284179 +MN,total,2007,5207203 +MN,under18,2007,1285074 +MN,under18,2013,1279111 +MN,total,2009,5281203 +MN,under18,2009,1284103 +MN,total,2013,5420380 +MN,total,2010,5310337 +MN,under18,2010,1282693 +MN,total,2011,5347108 +MN,under18,2011,1280424 +MN,under18,2012,1278050 +MN,total,2012,5379646 +MS,total,2012,2986450 +MS,under18,2012,742941 +MS,under18,2011,747742 +MS,total,2011,2977886 +MS,under18,2010,754111 +MS,total,2010,2970047 +MS,total,2013,2991207 +MS,total,2009,2958774 +MS,under18,2009,758539 +MS,under18,2013,737432 +MS,total,2007,2928350 +MS,under18,2007,761171 +MS,total,2008,2947806 +MS,under18,2008,760572 +MS,total,2005,2905943 +MS,under18,2005,760870 +MS,total,2006,2904978 +MS,under18,2006,756990 +MS,total,2003,2868312 +MS,under18,2003,759447 +MS,total,2004,2889010 +MS,under18,2004,760410 +MS,total,2002,2858681 +MS,under18,2002,763148 +MS,total,2001,2852994 +MS,under18,2001,768418 +MS,total,2000,2848353 +MS,under18,2000,774353 +MS,total,1999,2828408 +MS,under18,1999,775662 +MS,total,1997,2777004 +MS,under18,1997,774832 +MS,under18,1998,773721 +MS,total,1998,2804834 +MS,under18,1996,769680 +MS,total,1996,2748085 +MS,total,1995,2722659 +MS,under18,1995,767892 +MS,under18,1992,750224 +MS,total,1992,2623734 +MS,total,1993,2655100 +MS,under18,1993,755820 +MS,under18,1994,763795 +MS,total,1994,2688992 +MS,total,1991,2598733 +MS,under18,1991,738911 +MS,under18,1990,733660 +MS,total,1990,2578897 +MO,under18,1990,1316423 +MO,total,1990,5128880 +MO,total,1991,5170800 +MO,under18,1991,1332306 +MO,under18,1994,1378700 +MO,total,1994,5324497 +MO,total,1993,5271175 +MO,under18,1993,1365903 +MO,under18,1992,1349729 +MO,total,1992,5217101 +MO,under18,1996,1408732 +MO,total,1996,5431553 +MO,total,1995,5378247 +MO,under18,1995,1393554 +MO,under18,1998,1428999 +MO,total,1998,5521765 +MO,total,1997,5481193 +MO,under18,1997,1419837 +MO,total,1999,5561948 +MO,under18,1999,1428047 +MO,total,2000,5607285 +MO,under18,2000,1428383 +MO,total,2001,5641142 +MO,under18,2001,1426575 +MO,total,2002,5674825 +MO,under18,2002,1424513 +MO,total,2004,5747741 +MO,under18,2004,1420956 +MO,total,2003,5709403 +MO,under18,2003,1421927 +MO,total,2006,5842704 +MO,under18,2006,1428324 +MO,total,2005,5790300 +MO,under18,2005,1422978 +MO,total,2008,5923916 +MO,under18,2008,1428945 +MO,total,2007,5887612 +MO,under18,2007,1431346 +MO,under18,2013,1397685 +MO,total,2009,5961088 +MO,under18,2009,1426603 +MO,total,2013,6044171 +MO,total,2010,5996063 +MO,under18,2010,1424042 +MO,total,2011,6010065 +MO,under18,2011,1414444 +MO,under18,2012,1405015 +MO,total,2012,6024522 +MT,total,2012,1005494 +MT,under18,2012,222905 +MT,under18,2011,222977 +MT,total,2011,997600 +MT,under18,2010,223292 +MT,total,2010,990527 +MT,total,2013,1015165 +MT,total,2009,983982 +MT,under18,2009,223675 +MT,under18,2013,223981 +MT,total,2007,964706 +MT,under18,2007,223135 +MT,total,2008,976415 +MT,under18,2008,223814 +MT,total,2005,940102 +MT,under18,2005,221685 +MT,total,2006,952692 +MT,under18,2006,221930 +MT,total,2003,919630 +MT,under18,2003,223012 +MT,total,2004,930009 +MT,under18,2004,221999 +MT,total,2002,911667 +MT,under18,2002,224772 +MT,total,2001,906961 +MT,under18,2001,227118 +MT,total,1999,897508 +MT,under18,1999,231133 +MT,total,2000,903773 +MT,under18,2000,230067 +MT,total,1997,889865 +MT,under18,1997,232813 +MT,under18,1998,231746 +MT,total,1998,892431 +MT,total,1995,876553 +MT,under18,1995,236583 +MT,under18,1996,235294 +MT,total,1996,886254 +MT,under18,1992,230868 +MT,total,1992,825770 +MT,total,1993,844761 +MT,under18,1993,234987 +MT,under18,1994,237289 +MT,total,1994,861306 +MT,total,1991,809680 +MT,under18,1991,225259 +MT,under18,1990,223677 +MT,total,1990,800204 +NE,under18,1990,430068 +NE,total,1990,1581660 +NE,total,1991,1595919 +NE,under18,1991,434525 +NE,under18,1994,442589 +NE,total,1994,1639041 +NE,total,1993,1625590 +NE,under18,1993,439313 +NE,under18,1992,436378 +NE,total,1992,1611687 +NE,under18,1996,446841 +NE,total,1996,1673740 +NE,total,1995,1656993 +NE,under18,1995,444418 +NE,under18,1998,451192 +NE,total,1998,1695817 +NE,total,1997,1686418 +NE,under18,1997,450076 +NE,total,1999,1704764 +NE,under18,1999,451047 +NE,total,2000,1713820 +NE,under18,2000,450380 +NE,total,2001,1719836 +NE,under18,2001,448307 +NE,total,2002,1728292 +NE,under18,2002,447714 +NE,total,2004,1749370 +NE,under18,2004,448360 +NE,total,2003,1738643 +NE,under18,2003,447444 +NE,total,2006,1772693 +NE,under18,2006,450098 +NE,total,2005,1761497 +NE,under18,2005,448918 +NE,total,2008,1796378 +NE,under18,2008,453787 +NE,total,2007,1783440 +NE,under18,2007,451946 +NE,under18,2013,464348 +NE,total,2009,1812683 +NE,under18,2009,456543 +NE,total,2013,1868516 +NE,total,2010,1829838 +NE,under18,2010,459621 +NE,total,2011,1841749 +NE,under18,2011,460872 +NE,under18,2012,462673 +NE,total,2012,1855350 +NV,total,2012,2754354 +NV,under18,2012,659655 +NV,under18,2011,659236 +NV,total,2011,2717951 +NV,under18,2010,663180 +NV,total,2010,2703230 +NV,total,2013,2790136 +NV,total,2009,2684665 +NV,under18,2009,666041 +NV,under18,2013,661605 +NV,total,2007,2601072 +NV,under18,2007,654053 +NV,total,2008,2653630 +NV,under18,2008,662621 +NV,total,2005,2432143 +NV,under18,2005,611595 +NV,total,2006,2522658 +NV,under18,2006,634403 +NV,total,2003,2248850 +NV,under18,2003,568963 +NV,total,2004,2346222 +NV,under18,2004,591314 +NV,total,2002,2173791 +NV,under18,2002,552816 +NV,total,2001,2098399 +NV,under18,2001,534708 +NV,total,1999,1934718 +NV,under18,1999,493701 +NV,total,2000,2018741 +NV,under18,2000,516018 +NV,total,1997,1764104 +NV,under18,1997,443626 +NV,under18,1998,469424 +NV,total,1998,1853192 +NV,total,1995,1581578 +NV,under18,1995,396223 +NV,under18,1996,419133 +NV,total,1996,1666320 +NV,under18,1992,337396 +NV,total,1992,1351367 +NV,total,1993,1411215 +NV,under18,1993,354990 +NV,under18,1994,376745 +NV,total,1994,1499298 +NV,total,1991,1296172 +NV,under18,1991,325033 +NV,under18,1990,316406 +NV,total,1990,1220695 +NH,under18,1990,277454 +NH,total,1990,1112384 +NH,total,1991,1109929 +NH,under18,1991,281127 +NH,under18,1994,295563 +NH,total,1994,1142561 +NH,total,1993,1129458 +NH,under18,1993,290409 +NH,under18,1992,286314 +NH,total,1992,1117785 +NH,under18,1996,300161 +NH,total,1996,1174719 +NH,total,1995,1157561 +NH,under18,1995,298246 +NH,under18,1998,307292 +NH,total,1998,1205941 +NH,total,1997,1189425 +NH,under18,1997,302834 +NH,total,2000,1239882 +NH,under18,2000,310352 +NH,total,1999,1222015 +NH,under18,1999,308423 +NH,total,2001,1255517 +NH,under18,2001,311877 +NH,total,2002,1269089 +NH,under18,2002,312743 +NH,total,2004,1290121 +NH,under18,2004,309243 +NH,total,2003,1279840 +NH,under18,2003,311412 +NH,total,2005,1298492 +NH,under18,2005,307403 +NH,total,2006,1308389 +NH,under18,2006,305169 +NH,total,2008,1315906 +NH,under18,2008,296029 +NH,total,2007,1312540 +NH,under18,2007,300918 +NH,under18,2013,271122 +NH,total,2009,1316102 +NH,under18,2009,290850 +NH,total,2013,1323459 +NH,total,2010,1316614 +NH,under18,2010,285702 +NH,total,2011,1318075 +NH,under18,2011,280486 +NH,under18,2012,275818 +NH,total,2012,1321617 +NJ,total,2012,8867749 +NJ,under18,2012,2035106 +NJ,under18,2011,2049453 +NJ,total,2011,8836639 +NJ,under18,2010,2062013 +NJ,total,2010,8802707 +NJ,total,2013,8899339 +NJ,total,2009,8755602 +NJ,under18,2009,2068684 +NJ,under18,2013,2022117 +NJ,total,2007,8677885 +NJ,under18,2007,2091023 +NJ,total,2008,8711090 +NJ,under18,2008,2076366 +NJ,total,2006,8661679 +NJ,under18,2006,2106403 +NJ,total,2005,8651974 +NJ,under18,2005,2121878 +NJ,total,2003,8601402 +NJ,under18,2003,2126014 +NJ,total,2004,8634561 +NJ,under18,2004,2129051 +NJ,total,2002,8552643 +NJ,under18,2002,2116591 +NJ,total,2001,8492671 +NJ,under18,2001,2102838 +NJ,total,1999,8359592 +NJ,under18,1999,2066678 +NJ,total,2000,8430621 +NJ,under18,2000,2088885 +NJ,total,1997,8218808 +NJ,under18,1997,2028349 +NJ,under18,1998,2042080 +NJ,total,1998,8287418 +NJ,total,1995,8083242 +NJ,under18,1995,1997187 +NJ,under18,1996,2016502 +NJ,total,1996,8149596 +NJ,under18,1992,1890108 +NJ,total,1992,7880508 +NJ,total,1993,7948915 +NJ,under18,1993,1928623 +NJ,under18,1994,1968232 +NJ,total,1994,8014306 +NJ,total,1991,7814676 +NJ,under18,1991,1849605 +NJ,under18,1990,1818187 +NJ,total,1990,7762963 +NM,total,1990,1521574 +NM,under18,1990,453538 +NM,under18,1991,461811 +NM,total,1991,1555305 +NM,under18,1994,497542 +NM,under18,1993,487742 +NM,total,1993,1636453 +NM,total,1992,1595442 +NM,under18,1992,473176 +NM,total,1994,1682398 +NM,under18,1996,508100 +NM,total,1995,1720394 +NM,under18,1995,504558 +NM,total,1996,1752326 +NM,under18,1998,512801 +NM,total,1998,1793484 +NM,total,1997,1774839 +NM,under18,1997,514500 +NM,under18,1999,511135 +NM,total,1999,1808082 +NM,total,2000,1821204 +NM,under18,2000,508132 +NM,total,2001,1831690 +NM,under18,2001,503404 +NM,total,2002,1855309 +NM,under18,2002,502779 +NM,total,2004,1903808 +NM,under18,2004,501184 +NM,total,2003,1877574 +NM,under18,2003,500777 +NM,total,2005,1932274 +NM,under18,2005,502612 +NM,total,2006,1962137 +NM,under18,2006,505125 +NM,total,2008,2010662 +NM,under18,2008,511214 +NM,total,2007,1990070 +NM,under18,2007,508725 +NM,under18,2013,507540 +NM,total,2013,2085287 +NM,total,2009,2036802 +NM,under18,2009,515470 +NM,total,2010,2064982 +NM,under18,2010,518763 +NM,under18,2011,516513 +NM,total,2011,2077919 +NM,under18,2012,512314 +NM,total,2012,2083540 +NY,total,2012,19576125 +NY,under18,2012,4264694 +NY,total,2011,19502728 +NY,under18,2011,4294555 +NY,under18,2010,4318033 +NY,total,2010,19398228 +NY,total,2009,19307066 +NY,under18,2009,4342926 +NY,total,2013,19651127 +NY,under18,2013,4239976 +NY,total,2007,19132335 +NY,under18,2007,4410949 +NY,total,2008,19212436 +NY,under18,2008,4372170 +NY,total,2006,19104631 +NY,under18,2006,4457777 +NY,total,2005,19132610 +NY,under18,2005,4514456 +NY,total,2003,19175939 +NY,under18,2003,4619506 +NY,total,2004,19171567 +NY,under18,2004,4574065 +NY,total,2002,19137800 +NY,under18,2002,4652232 +NY,total,2001,19082838 +NY,under18,2001,4672425 +NY,under18,1999,4672587 +NY,total,1999,18882725 +NY,total,2000,19001780 +NY,under18,2000,4687374 +NY,under18,1997,4670787 +NY,total,1997,18656546 +NY,total,1998,18755906 +NY,under18,1998,4652140 +NY,total,1996,18588460 +NY,under18,1995,4648419 +NY,total,1995,18524104 +NY,under18,1996,4667647 +NY,total,1994,18459470 +NY,under18,1992,4465539 +NY,total,1992,18246653 +NY,total,1993,18374954 +NY,under18,1993,4538171 +NY,under18,1994,4605284 +NY,total,1991,18122510 +NY,under18,1991,4372727 +NY,under18,1990,4281643 +NY,total,1990,18020784 +NC,under18,1990,1625804 +NC,total,1990,6664016 +NC,total,1991,6784280 +NC,under18,1991,1640394 +NC,total,1993,7042818 +NC,under18,1993,1710267 +NC,under18,1992,1674144 +NC,total,1992,6897214 +NC,under18,1994,1750754 +NC,total,1994,7187398 +NC,total,1995,7344674 +NC,under18,1995,1785150 +NC,under18,1996,1821506 +NC,total,1996,7500670 +NC,under18,1998,1894753 +NC,total,1998,7809122 +NC,total,1997,7656825 +NC,under18,1997,1861621 +NC,total,2000,8081614 +NC,under18,2000,1967626 +NC,total,1999,7949362 +NC,under18,1999,1932141 +NC,total,2001,8210122 +NC,under18,2001,2003782 +NC,total,2002,8326201 +NC,under18,2002,2034451 +NC,total,2004,8553152 +NC,under18,2004,2085165 +NC,total,2003,8422501 +NC,under18,2003,2060838 +NC,total,2005,8705407 +NC,under18,2005,2122485 +NC,total,2006,8917270 +NC,under18,2006,2166393 +NC,total,2008,9309449 +NC,under18,2008,2252101 +NC,total,2007,9118037 +NC,under18,2007,2219168 +NC,under18,2013,2285605 +NC,total,2013,9848060 +NC,total,2009,9449566 +NC,under18,2009,2272955 +NC,total,2010,9559533 +NC,under18,2010,2282288 +NC,under18,2011,2284238 +NC,total,2011,9651377 +NC,under18,2012,2284122 +NC,total,2012,9748364 +ND,total,2012,701345 +ND,under18,2012,156765 +ND,total,2011,684867 +ND,under18,2011,152357 +ND,under18,2010,150179 +ND,total,2010,674344 +ND,total,2009,664968 +ND,under18,2009,148674 +ND,total,2013,723393 +ND,under18,2013,162688 +ND,total,2007,652822 +ND,under18,2007,147263 +ND,total,2008,657569 +ND,under18,2008,147462 +ND,total,2006,649422 +ND,under18,2006,147331 +ND,total,2005,646089 +ND,under18,2005,148119 +ND,total,2003,638817 +ND,under18,2003,150406 +ND,total,2004,644705 +ND,under18,2004,149128 +ND,total,2002,638168 +ND,under18,2002,153097 +ND,total,2001,639062 +ND,under18,2001,156113 +ND,total,1999,644259 +ND,under18,1999,163056 +ND,total,2000,642023 +ND,under18,2000,160477 +ND,total,1997,649716 +ND,under18,1997,167475 +ND,under18,1998,165448 +ND,total,1998,647532 +ND,under18,1996,169257 +ND,total,1996,650382 +ND,total,1995,647832 +ND,under18,1995,171146 +ND,under18,1994,172160 +ND,total,1994,644806 +ND,under18,1992,172052 +ND,total,1992,638223 +ND,total,1993,641216 +ND,under18,1993,172168 +ND,total,1991,635753 +ND,under18,1991,171730 +ND,under18,1990,170920 +ND,total,1990,637685 +OH,under18,1990,2778491 +OH,total,1990,10864162 +OH,total,1991,10945762 +OH,under18,1991,2806959 +OH,total,1993,11101140 +OH,under18,1993,2855785 +OH,under18,1992,2839356 +OH,total,1992,11029431 +OH,under18,1994,2875397 +OH,total,1994,11152455 +OH,total,1995,11202751 +OH,under18,1995,2879930 +OH,under18,1996,2883443 +OH,total,1996,11242827 +OH,under18,1998,2896255 +OH,total,1998,11311536 +OH,total,1997,11277357 +OH,under18,1997,2897375 +OH,total,2000,11363543 +OH,under18,2000,2886585 +OH,total,1999,11335454 +OH,under18,1999,2893270 +OH,total,2001,11387404 +OH,under18,2001,2878123 +OH,total,2002,11407889 +OH,under18,2002,2865674 +OH,total,2004,11452251 +OH,under18,2004,2836068 +OH,total,2003,11434788 +OH,under18,2003,2849573 +OH,total,2005,11463320 +OH,under18,2005,2819794 +OH,total,2006,11481213 +OH,under18,2006,2804828 +OH,total,2008,11515391 +OH,under18,2008,2768968 +OH,total,2007,11500468 +OH,under18,2007,2790347 +OH,under18,2013,2649830 +OH,total,2013,11570808 +OH,total,2009,11528896 +OH,under18,2009,2748051 +OH,total,2010,11545435 +OH,under18,2010,2722589 +OH,under18,2011,2693469 +OH,total,2011,11549772 +OH,under18,2012,2668125 +OH,total,2012,11553031 +OK,total,2012,3815780 +OK,under18,2012,939911 +OK,total,2011,3785534 +OK,under18,2011,935714 +OK,under18,2010,931483 +OK,total,2010,3759263 +OK,total,2009,3717572 +OK,under18,2009,922711 +OK,total,2013,3850568 +OK,under18,2013,947027 +OK,total,2007,3634349 +OK,under18,2007,904328 +OK,total,2008,3668976 +OK,under18,2008,910617 +OK,total,2006,3594090 +OK,under18,2006,894761 +OK,total,2005,3548597 +OK,under18,2005,885316 +OK,total,2003,3504892 +OK,under18,2003,883959 +OK,total,2004,3525233 +OK,under18,2004,881606 +OK,total,2002,3489080 +OK,under18,2002,884961 +OK,total,2001,3467100 +OK,under18,2001,885218 +OK,total,1999,3437148 +OK,under18,1999,895678 +OK,total,2000,3454365 +OK,under18,2000,891847 +OK,total,1997,3372918 +OK,under18,1997,893835 +OK,under18,1998,898501 +OK,total,1998,3405194 +OK,under18,1996,887093 +OK,total,1996,3340129 +OK,total,1995,3308208 +OK,under18,1995,883667 +OK,under18,1994,877803 +OK,total,1994,3280940 +OK,under18,1992,862548 +OK,total,1992,3220517 +OK,total,1993,3252285 +OK,under18,1993,870137 +OK,total,1991,3175440 +OK,under18,1991,849639 +OK,under18,1990,841715 +OK,total,1990,3148825 +OR,under18,1990,742436 +OR,total,1990,2860375 +OR,total,1991,2928507 +OR,under18,1991,752442 +OR,total,1993,3060367 +OR,under18,1993,778973 +OR,under18,1992,770191 +OR,total,1992,2991755 +OR,under18,1994,793435 +OR,total,1994,3121264 +OR,total,1995,3184369 +OR,under18,1995,806512 +OR,under18,1996,816102 +OR,total,1996,3247111 +OR,under18,1998,837928 +OR,total,1998,3352449 +OR,total,1997,3304310 +OR,under18,1997,830002 +OR,total,2000,3429708 +OR,under18,2000,847511 +OR,total,1999,3393941 +OR,under18,1999,843484 +OR,total,2001,3467937 +OR,under18,2001,848663 +OR,total,2002,3513424 +OR,under18,2002,850733 +OR,total,2004,3569463 +OR,under18,2004,846786 +OR,total,2003,3547376 +OR,under18,2003,850251 +OR,total,2005,3613202 +OR,under18,2005,849323 +OR,total,2006,3670883 +OR,under18,2006,857003 +OR,total,2008,3768748 +OR,under18,2008,865664 +OR,total,2007,3722417 +OR,under18,2007,862161 +OR,under18,2013,857606 +OR,total,2013,3930065 +OR,total,2009,3808600 +OR,under18,2009,866194 +OR,total,2010,3837208 +OR,under18,2010,865129 +OR,under18,2011,862518 +OR,total,2011,3867937 +OR,under18,2012,859910 +OR,total,2012,3899801 +PA,total,2012,12764475 +PA,under18,2012,2737905 +PA,total,2011,12741310 +PA,under18,2011,2761343 +PA,under18,2010,2785316 +PA,total,2010,12710472 +PA,total,2009,12666858 +PA,under18,2009,2804929 +PA,total,2013,12773801 +PA,under18,2013,2715645 +PA,total,2007,12563937 +PA,under18,2007,2839574 +PA,total,2008,12612285 +PA,under18,2008,2821004 +PA,total,2006,12510809 +PA,under18,2006,2850778 +PA,total,2005,12449990 +PA,under18,2005,2859793 +PA,total,2003,12374658 +PA,under18,2003,2883270 +PA,total,2004,12410722 +PA,under18,2004,2873125 +PA,total,2002,12331031 +PA,under18,2002,2894935 +PA,total,2001,12298970 +PA,under18,2001,2905836 +PA,total,1999,12263805 +PA,under18,1999,2930193 +PA,total,2000,12284173 +PA,under18,2000,2918850 +PA,total,1997,12227814 +PA,under18,1997,2942240 +PA,under18,1998,2940285 +PA,total,1998,12245672 +PA,under18,1996,2937411 +PA,total,1996,12220464 +PA,total,1995,12198403 +PA,under18,1995,2941531 +PA,under18,1994,2932851 +PA,total,1994,12166050 +PA,under18,1992,2873013 +PA,total,1992,12049450 +PA,total,1993,12119724 +PA,under18,1993,2907351 +PA,total,1991,11982164 +PA,under18,1991,2830059 +PA,under18,1990,2799168 +PA,total,1990,11903299 +RI,under18,1990,225923 +RI,total,1990,1005995 +RI,total,1991,1010649 +RI,under18,1991,229448 +RI,total,1993,1015113 +RI,under18,1993,237218 +RI,under18,1992,232630 +RI,total,1992,1012581 +RI,under18,1994,239100 +RI,total,1994,1015960 +RI,total,1995,1017002 +RI,under18,1995,240553 +RI,under18,1996,240569 +RI,total,1996,1020893 +RI,under18,1998,241760 +RI,total,1998,1031155 +RI,total,1997,1025353 +RI,under18,1997,242079 +RI,total,2000,1050268 +RI,under18,2000,248065 +RI,total,1999,1040402 +RI,under18,1999,247014 +RI,total,2001,1057142 +RI,under18,2001,248296 +RI,total,2002,1065995 +RI,under18,2002,248690 +RI,total,2004,1074579 +RI,under18,2004,246228 +RI,total,2003,1071342 +RI,under18,2003,248075 +RI,total,2005,1067916 +RI,under18,2005,241932 +RI,total,2006,1063096 +RI,under18,2006,237348 +RI,total,2008,1055003 +RI,under18,2008,229798 +RI,total,2007,1057315 +RI,under18,2007,233655 +RI,under18,2013,213987 +RI,total,2013,1051511 +RI,total,2009,1053646 +RI,under18,2009,225902 +RI,total,2010,1052669 +RI,under18,2010,223088 +RI,under18,2011,219783 +RI,total,2011,1050350 +RI,under18,2012,216591 +RI,total,2012,1050304 +SC,total,2012,4723417 +SC,under18,2012,1077455 +SC,total,2011,4673509 +SC,under18,2011,1076524 +SC,under18,2010,1079978 +SC,total,2010,4636361 +SC,total,2009,4589872 +SC,under18,2009,1079729 +SC,total,2013,4774839 +SC,under18,2013,1079798 +SC,total,2007,4444110 +SC,under18,2007,1064190 +SC,total,2008,4528996 +SC,under18,2008,1074116 +SC,total,2006,4357847 +SC,under18,2006,1050042 +SC,total,2005,4270150 +SC,under18,2005,1036941 +SC,total,2003,4150297 +SC,under18,2003,1023785 +SC,total,2004,4210921 +SC,under18,2004,1029111 +SC,total,2002,4107795 +SC,under18,2002,1020531 +SC,total,2001,4064995 +SC,under18,2001,1016134 +SC,total,1999,3974682 +SC,under18,1999,1007050 +SC,total,2000,4024223 +SC,under18,2000,1010641 +SC,total,1997,3859696 +SC,under18,1997,1001681 +SC,under18,1998,1006371 +SC,total,1998,3919235 +SC,under18,1996,987576 +SC,total,1996,3796200 +SC,total,1995,3748582 +SC,under18,1995,975884 +SC,under18,1994,969766 +SC,total,1994,3705397 +SC,under18,1992,947868 +SC,total,1992,3620464 +SC,total,1993,3663314 +SC,under18,1993,956951 +SC,total,1991,3570404 +SC,under18,1991,936122 +SC,under18,1990,921041 +SC,total,1990,3501155 +SD,under18,1990,199453 +SD,total,1990,697101 +SD,total,1991,703669 +SD,under18,1991,201749 +SD,total,1993,722160 +SD,under18,1993,207975 +SD,under18,1992,206632 +SD,total,1992,712801 +SD,under18,1994,208443 +SD,total,1994,730790 +SD,total,1995,737926 +SD,under18,1995,207890 +SD,under18,1996,205780 +SD,total,1996,742214 +SD,under18,1998,204786 +SD,total,1998,746059 +SD,total,1997,744223 +SD,under18,1997,205978 +SD,total,2000,755844 +SD,under18,2000,202681 +SD,total,1999,750413 +SD,under18,1999,203737 +SD,total,2001,757972 +SD,under18,2001,200795 +SD,total,2002,760020 +SD,under18,2002,198694 +SD,total,2004,770396 +SD,under18,2004,196804 +SD,total,2003,763729 +SD,under18,2003,197326 +SD,total,2005,775493 +SD,under18,2005,196476 +SD,total,2006,783033 +SD,under18,2006,197332 +SD,total,2008,799124 +SD,under18,2008,199848 +SD,total,2007,791623 +SD,under18,2007,198847 +SD,under18,2013,207959 +SD,total,2013,844877 +SD,total,2009,807067 +SD,under18,2009,201204 +SD,total,2010,816211 +SD,under18,2010,203145 +SD,under18,2011,203948 +SD,total,2011,823772 +SD,under18,2012,205298 +SD,total,2012,834047 +TN,total,2012,6454914 +TN,under18,2012,1492689 +TN,total,2011,6398361 +TN,under18,2011,1491837 +TN,under18,2010,1495090 +TN,total,2010,6356683 +TN,total,2009,6306019 +TN,under18,2009,1494687 +TN,total,2013,6495978 +TN,under18,2013,1491577 +TN,total,2007,6175727 +TN,under18,2007,1482747 +TN,total,2008,6247411 +TN,under18,2008,1494354 +TN,total,2006,6088766 +TN,under18,2006,1470166 +TN,total,2005,5991057 +TN,under18,2005,1449326 +TN,total,2003,5847812 +TN,under18,2003,1424861 +TN,total,2004,5910809 +TN,under18,2004,1433343 +TN,total,2002,5795918 +TN,under18,2002,1414857 +TN,total,2001,5750789 +TN,under18,2001,1407578 +TN,total,1999,5638706 +TN,under18,1999,1385997 +TN,total,2000,5703719 +TN,under18,2000,1399685 +TN,total,1997,5499233 +TN,under18,1997,1359030 +TN,under18,1998,1369987 +TN,total,1998,5570045 +TN,under18,1996,1345723 +TN,total,1996,5416643 +TN,total,1995,5326936 +TN,under18,1995,1331616 +TN,under18,1994,1310988 +TN,total,1994,5231438 +TN,under18,1992,1259458 +TN,total,1992,5049742 +TN,total,1993,5137584 +TN,under18,1993,1285044 +TN,total,1991,4966587 +TN,under18,1991,1233260 +TN,under18,1990,1220200 +TN,total,1990,4894492 +TX,under18,1990,4906220 +TX,total,1990,17056755 +TX,total,1991,17398005 +TX,under18,1991,5000793 +TX,total,1993,18161612 +TX,under18,1993,5217899 +TX,under18,1992,5109805 +TX,total,1992,17759738 +TX,under18,1994,5331524 +TX,total,1994,18564062 +TX,total,1995,18958751 +TX,under18,1995,5421784 +TX,under18,1996,5551447 +TX,total,1996,19340342 +TX,under18,1998,5759054 +TX,total,1998,20157531 +TX,total,1997,19740317 +TX,under18,1997,5655482 +TX,total,2000,20944499 +TX,under18,2000,5906301 +TX,total,1999,20558220 +TX,under18,1999,5840211 +TX,total,2001,21319622 +TX,under18,2001,5980187 +TX,total,2002,21690325 +TX,under18,2002,6060372 +TX,total,2004,22394023 +TX,under18,2004,6208259 +TX,total,2003,22030931 +TX,under18,2003,6132980 +TX,total,2005,22778123 +TX,under18,2005,6290970 +TX,total,2006,23359580 +TX,under18,2006,6446798 +TX,total,2008,24309039 +TX,under18,2008,6675917 +TX,total,2007,23831983 +TX,under18,2007,6565872 +TX,under18,2013,7041986 +TX,total,2013,26448193 +TX,total,2009,24801761 +TX,under18,2009,6792907 +TX,total,2010,25245178 +TX,under18,2010,6879014 +TX,under18,2011,6931758 +TX,total,2011,25640909 +TX,under18,2012,6985807 +TX,total,2012,26060796 +UT,total,2012,2854871 +UT,under18,2012,888578 +UT,total,2011,2814784 +UT,under18,2011,881350 +UT,under18,2010,873019 +UT,total,2010,2774424 +UT,total,2009,2723421 +UT,under18,2009,857853 +UT,total,2013,2900872 +UT,under18,2013,896589 +UT,total,2007,2597746 +UT,under18,2007,815496 +UT,total,2008,2663029 +UT,under18,2008,837258 +UT,total,2006,2525507 +UT,under18,2006,789957 +UT,total,2005,2457719 +UT,under18,2005,767888 +UT,total,2003,2360137 +UT,under18,2003,740483 +UT,total,2004,2401580 +UT,under18,2004,751771 +UT,total,2002,2324815 +UT,under18,2002,733517 +UT,total,2001,2283715 +UT,under18,2001,726819 +UT,total,1999,2203482 +UT,under18,1999,715398 +UT,total,2000,2244502 +UT,under18,2000,721686 +UT,total,1997,2119784 +UT,under18,1997,699528 +UT,under18,1998,709386 +UT,total,1998,2165961 +UT,under18,1996,687078 +UT,total,1996,2067976 +UT,total,1995,2014179 +UT,under18,1995,679636 +UT,under18,1994,673935 +UT,total,1994,1960446 +UT,under18,1992,648725 +UT,total,1992,1836799 +UT,total,1993,1898404 +UT,under18,1993,662968 +UT,total,1991,1779780 +UT,under18,1991,637216 +UT,under18,1990,627122 +UT,total,1990,1731223 +VT,under18,1990,143296 +VT,total,1990,564798 +VT,total,1991,568606 +VT,under18,1991,145219 +VT,total,1993,577748 +VT,under18,1993,148705 +VT,under18,1992,146983 +VT,total,1992,572751 +VT,under18,1994,150794 +VT,total,1994,583836 +VT,total,1995,589003 +VT,under18,1995,151439 +VT,under18,1996,151490 +VT,total,1996,593701 +VT,under18,1998,148467 +VT,total,1998,600416 +VT,total,1997,597239 +VT,under18,1997,150040 +VT,total,2000,609618 +VT,under18,2000,147549 +VT,total,1999,604683 +VT,under18,1999,147859 +VT,total,2001,612223 +VT,under18,2001,146040 +VT,total,2002,615442 +VT,under18,2002,144441 +VT,total,2004,619920 +VT,under18,2004,141068 +VT,total,2003,617858 +VT,under18,2003,142718 +VT,total,2005,621215 +VT,under18,2005,138933 +VT,total,2006,622892 +VT,under18,2006,136731 +VT,total,2008,624151 +VT,under18,2008,132600 +VT,total,2007,623481 +VT,under18,2007,134695 +VT,under18,2013,122701 +VT,total,2013,626630 +VT,total,2009,624817 +VT,under18,2009,130450 +VT,total,2010,625793 +VT,under18,2010,128601 +VT,under18,2011,126500 +VT,total,2011,626320 +VT,under18,2012,124555 +VT,total,2012,625953 +VA,total,2012,8186628 +VA,under18,2012,1861323 +VA,total,2011,8105850 +VA,under18,2011,1857585 +VA,under18,2010,1855025 +VA,total,2010,8024417 +VA,total,2009,7925937 +VA,under18,2009,1845132 +VA,total,2013,8260405 +VA,under18,2013,1864535 +VA,total,2007,7751000 +VA,under18,2007,1834386 +VA,total,2008,7833496 +VA,under18,2008,1838361 +VA,total,2005,7577105 +VA,under18,2005,1816270 +VA,total,2006,7673725 +VA,under18,2006,1826368 +VA,total,2003,7366977 +VA,under18,2003,1782254 +VA,total,2004,7475575 +VA,under18,2004,1801958 +VA,total,2002,7286873 +VA,under18,2002,1771247 +VA,total,2001,7198362 +VA,under18,2001,1754549 +VA,total,1999,7000174 +VA,under18,1999,1723125 +VA,total,2000,7105817 +VA,under18,2000,1741420 +VA,total,1997,6829183 +VA,under18,1997,1683766 +VA,under18,1998,1706261 +VA,total,1998,6900918 +VA,under18,1996,1664147 +VA,total,1996,6750884 +VA,total,1995,6670693 +VA,under18,1995,1649005 +VA,under18,1994,1628711 +VA,total,1994,6593139 +VA,under18,1992,1581544 +VA,total,1992,6414307 +VA,total,1993,6509630 +VA,under18,1993,1604758 +VA,total,1991,6301217 +VA,under18,1991,1548258 +VA,under18,1990,1520670 +VA,total,1990,6216884 +WA,under18,1990,1301545 +WA,total,1990,4903043 +WA,total,1991,5025624 +WA,under18,1991,1326527 +WA,total,1993,5278842 +WA,under18,1993,1387716 +WA,under18,1992,1365480 +WA,total,1992,5160757 +WA,under18,1994,1409922 +WA,total,1994,5375161 +WA,total,1995,5481027 +WA,under18,1995,1429397 +WA,under18,1996,1449613 +WA,total,1996,5569753 +WA,under18,1998,1494784 +WA,total,1998,5769562 +WA,total,1997,5674747 +WA,under18,1997,1473646 +WA,total,2000,5910512 +WA,under18,2000,1516361 +WA,total,1999,5842564 +WA,under18,1999,1507824 +WA,total,2001,5985722 +WA,under18,2001,1517527 +WA,total,2002,6052349 +WA,under18,2002,1517655 +WA,total,2004,6178645 +WA,under18,2004,1520751 +WA,total,2003,6104115 +WA,under18,2003,1514877 +WA,total,2005,6257305 +WA,under18,2005,1523890 +WA,total,2006,6370753 +WA,under18,2006,1536926 +WA,total,2008,6562231 +WA,under18,2008,1560302 +WA,total,2007,6461587 +WA,under18,2007,1549582 +WA,under18,2013,1595795 +WA,total,2013,6971406 +WA,total,2009,6667426 +WA,under18,2009,1574403 +WA,total,2010,6742256 +WA,under18,2010,1581436 +WA,under18,2011,1584709 +WA,total,2011,6821481 +WA,under18,2012,1588451 +WA,total,2012,6895318 +WV,total,2012,1856680 +WV,under18,2012,384030 +WV,total,2011,1855184 +WV,under18,2011,385283 +WV,under18,2010,387224 +WV,total,2010,1854146 +WV,total,2009,1847775 +WV,under18,2009,389036 +WV,total,2013,1854304 +WV,under18,2013,381678 +WV,total,2007,1834052 +WV,under18,2007,390661 +WV,total,2008,1840310 +WV,under18,2008,390210 +WV,total,2006,1827912 +WV,under18,2006,390637 +WV,total,2005,1820492 +WV,under18,2005,390431 +WV,total,2003,1812295 +WV,under18,2003,392460 +WV,total,2004,1816438 +WV,under18,2004,391856 +WV,total,2002,1805414 +WV,under18,2002,393569 +WV,total,2001,1801481 +WV,under18,2001,395307 +WV,total,1999,1811799 +WV,under18,1999,406784 +WV,total,2000,1807021 +WV,under18,2000,401062 +WV,total,1997,1819113 +WV,under18,1997,418037 +WV,under18,1998,412793 +WV,total,1998,1815609 +WV,under18,1996,422831 +WV,total,1996,1822808 +WV,total,1995,1823700 +WV,under18,1995,428790 +WV,under18,1994,429128 +WV,total,1994,1820421 +WV,under18,1992,433116 +WV,total,1992,1806451 +WV,total,1993,1817539 +WV,under18,1993,432364 +WV,total,1991,1798735 +WV,under18,1991,433918 +WV,under18,1990,436797 +WV,total,1990,1792548 +WI,under18,1990,1302869 +WI,total,1990,4904562 +WI,total,1991,4964343 +WI,under18,1991,1314855 +WI,total,1993,5084889 +WI,under18,1993,1337334 +WI,under18,1992,1330555 +WI,total,1992,5025398 +WI,under18,1994,1348110 +WI,total,1994,5133678 +WI,total,1995,5184836 +WI,under18,1995,1351343 +WI,under18,1996,1352877 +WI,total,1996,5229986 +WI,under18,1998,1362907 +WI,total,1998,5297673 +WI,total,1997,5266213 +WI,under18,1997,1359712 +WI,total,1999,5332666 +WI,under18,1999,1367019 +WI,total,2000,5373999 +WI,under18,2000,1370440 +WI,total,2001,5406835 +WI,under18,2001,1367593 +WI,total,2002,5445162 +WI,under18,2002,1365315 +WI,total,2004,5514026 +WI,under18,2004,1354643 +WI,total,2003,5479203 +WI,under18,2003,1358505 +WI,total,2005,5546166 +WI,under18,2005,1349866 +WI,total,2006,5577655 +WI,under18,2006,1348785 +WI,total,2008,5640996 +WI,under18,2008,1345573 +WI,total,2007,5610775 +WI,under18,2007,1348901 +WI,under18,2013,1307776 +WI,total,2013,5742713 +WI,total,2009,5669264 +WI,under18,2009,1342411 +WI,total,2010,5689060 +WI,under18,2010,1336094 +WI,under18,2011,1325870 +WI,total,2011,5708785 +WI,under18,2012,1316113 +WI,total,2012,5724554 +WY,total,2012,576626 +WY,under18,2012,136526 +WY,total,2011,567329 +WY,under18,2011,135407 +WY,under18,2010,135351 +WY,total,2010,564222 +WY,total,2009,559851 +WY,under18,2009,134960 +WY,total,2013,582658 +WY,under18,2013,137679 +WY,total,2007,534876 +WY,under18,2007,128760 +WY,total,2008,546043 +WY,under18,2008,131511 +WY,total,2006,522667 +WY,under18,2006,125525 +WY,total,2005,514157 +WY,under18,2005,124022 +WY,total,2003,503453 +WY,under18,2003,124182 +WY,total,2004,509106 +WY,under18,2004,123974 +WY,total,2002,500017 +WY,under18,2002,125495 +WY,total,2001,494657 +WY,under18,2001,126212 +WY,total,2000,494300 +WY,under18,2000,128774 +WY,total,1999,491780 +WY,under18,1999,130793 +WY,total,1997,489452 +WY,under18,1997,134328 +WY,under18,1998,132602 +WY,total,1998,490787 +WY,under18,1996,135698 +WY,total,1996,488167 +WY,total,1995,485160 +WY,under18,1995,136785 +WY,under18,1994,137733 +WY,total,1994,480283 +WY,under18,1992,137308 +WY,total,1992,466251 +WY,total,1993,473081 +WY,under18,1993,137458 +WY,total,1991,459260 +WY,under18,1991,136720 +WY,under18,1990,136078 +WY,total,1990,453690 +PR,under18,1990,NaN +PR,total,1990,NaN +PR,total,1991,NaN +PR,under18,1991,NaN +PR,total,1993,NaN +PR,under18,1993,NaN +PR,under18,1992,NaN +PR,total,1992,NaN +PR,under18,1994,NaN +PR,total,1994,NaN +PR,total,1995,NaN +PR,under18,1995,NaN +PR,under18,1996,NaN +PR,total,1996,NaN +PR,under18,1998,NaN +PR,total,1998,NaN +PR,total,1997,NaN +PR,under18,1997,NaN +PR,total,1999,NaN +PR,under18,1999,NaN +PR,total,2000,3810605 +PR,under18,2000,1089063 +PR,total,2001,3818774 +PR,under18,2001,1077566 +PR,total,2002,3823701 +PR,under18,2002,1065051 +PR,total,2004,3826878 +PR,under18,2004,1035919 +PR,total,2003,3826095 +PR,under18,2003,1050615 +PR,total,2005,3821362 +PR,under18,2005,1019447 +PR,total,2006,3805214 +PR,under18,2006,998543 +PR,total,2007,3782995 +PR,under18,2007,973613 +PR,total,2008,3760866 +PR,under18,2008,945705 +PR,under18,2013,814068 +PR,total,2013,3615086 +PR,total,2009,3740410 +PR,under18,2009,920794 +PR,total,2010,3721208 +PR,under18,2010,896945 +PR,under18,2011,869327 +PR,total,2011,3686580 +PR,under18,2012,841740 +PR,total,2012,3651545 +USA,under18,1990,64218512 +USA,total,1990,249622814 +USA,total,1991,252980942 +USA,under18,1991,65313018 +USA,under18,1992,66509177 +USA,total,1992,256514231 +USA,total,1993,259918595 +USA,under18,1993,67594938 +USA,under18,1994,68640936 +USA,total,1994,263125826 +USA,under18,1995,69473140 +USA,under18,1996,70233512 +USA,total,1995,266278403 +USA,total,1996,269394291 +USA,total,1997,272646932 +USA,under18,1997,70920738 +USA,under18,1998,71431406 +USA,total,1998,275854116 +USA,under18,1999,71946051 +USA,total,2000,282162411 +USA,under18,2000,72376189 +USA,total,1999,279040181 +USA,total,2001,284968955 +USA,under18,2001,72671175 +USA,total,2002,287625193 +USA,under18,2002,72936457 +USA,total,2003,290107933 +USA,under18,2003,73100758 +USA,total,2004,292805298 +USA,under18,2004,73297735 +USA,total,2005,295516599 +USA,under18,2005,73523669 +USA,total,2006,298379912 +USA,under18,2006,73757714 +USA,total,2007,301231207 +USA,under18,2007,74019405 +USA,total,2008,304093966 +USA,under18,2008,74104602 +USA,under18,2013,73585872 +USA,total,2013,316128839 +USA,total,2009,306771529 +USA,under18,2009,74134167 +USA,under18,2010,74119556 +USA,total,2010,309326295 +USA,under18,2011,73902222 +USA,total,2011,311582564 +USA,under18,2012,73708179 +USA,total,2012,313873685 diff --git a/Day91-100/英语面试.md b/Day91-100/100.英语面试.md similarity index 100% rename from Day91-100/英语面试.md rename to Day91-100/100.英语面试.md diff --git a/Day91-100/团队项目开发.md b/Day91-100/91.团队项目开发准备.md similarity index 99% rename from Day91-100/团队项目开发.md rename to Day91-100/91.团队项目开发准备.md index d86e03b..86f0e5e 100644 --- a/Day91-100/团队项目开发.md +++ b/Day91-100/91.团队项目开发准备.md @@ -1,4 +1,4 @@ -## 团队项目开发 +## 团队项目开发准备 我们经常听到个人开发和团队开发这两个词,所谓个人开发就是一个人把控产品的所有内容;而团队开发则是由多个人组成团队并完成产品的开发。要实施团队开发以下几点是必不可少的: diff --git a/Day91-100/Docker简易上手指南.md b/Day91-100/92.Docker在项目中的应用.md similarity index 99% rename from Day91-100/Docker简易上手指南.md rename to Day91-100/92.Docker在项目中的应用.md index 4d4de42..ee47fb3 100644 --- a/Day91-100/Docker简易上手指南.md +++ b/Day91-100/92.Docker在项目中的应用.md @@ -1,4 +1,4 @@ -## Docker简易上手指南 +## Docker在项目中的应用 ### Docker简介 diff --git a/Day91-100/MySQL相关知识.md b/Day91-100/93.数据库设计和OOAD.md similarity index 98% rename from Day91-100/MySQL相关知识.md rename to Day91-100/93.数据库设计和OOAD.md index 8f78647..2281042 100644 --- a/Day91-100/MySQL相关知识.md +++ b/Day91-100/93.数据库设计和OOAD.md @@ -1,4 +1,4 @@ -## MySQL相关知识 +## 数据库设计和OOAD ### 存储引擎 diff --git a/Day91-100/网络API接口设计.md b/Day91-100/94.网络API接口设计.md similarity index 98% rename from Day91-100/网络API接口设计.md rename to Day91-100/94.网络API接口设计.md index e83a568..df05a3f 100644 --- a/Day91-100/网络API接口设计.md +++ b/Day91-100/94.网络API接口设计.md @@ -46,7 +46,7 @@ API接口返回的数据通常都是JSON或XML格式,我们这里不讨论后 | 10003 | 评论已被删除 | 查看评论时评论因不和谐因素已被删除 | | 10004 | …… | …… | -1. **GET** `/comments/{article-id}` +1. **GET** `/articles/{article-id}/comments/` 开发者:王大锤 @@ -95,7 +95,7 @@ API接口返回的数据通常都是JSON或XML格式,我们这里不讨论后 } ``` -2. **POST** `/comments/{article-id}` +2. **POST** `/articles/{article-id}/comments` 开发者:王大锤 diff --git a/Day91-100/Django知识点概述.md b/Day91-100/95.使用Django开发项目.md similarity index 99% rename from Day91-100/Django知识点概述.md rename to Day91-100/95.使用Django开发项目.md index 4ae84e5..e1a34e2 100644 --- a/Day91-100/Django知识点概述.md +++ b/Day91-100/95.使用Django开发项目.md @@ -1,5 +1,7 @@ ## Django知识点概述 +> 说明:本文的部分插图来自于《Python项目开发实战》和《精通Django》,这两本书中都包含了对Django框架精彩的讲解,有兴趣的读者可以自行购买阅读。 + ### Web应用 问题1:描述一个Web应用的工作流程。 diff --git a/Day91-100/关于测试.md b/Day91-100/96.软件测试和自动化测试.md similarity index 100% rename from Day91-100/关于测试.md rename to Day91-100/96.软件测试和自动化测试.md diff --git a/Day91-100/电商网站技术要点剖析.md b/Day91-100/97.电商网站技术要点剖析.md similarity index 100% rename from Day91-100/电商网站技术要点剖析.md rename to Day91-100/97.电商网站技术要点剖析.md diff --git a/Day91-100/项目部署上线指南.md b/Day91-100/98.项目部署上线和性能调优.md similarity index 100% rename from Day91-100/项目部署上线指南.md rename to Day91-100/98.项目部署上线和性能调优.md diff --git a/Day91-100/面试中的公共问题.md b/Day91-100/99.面试中的公共问题.md similarity index 100% rename from Day91-100/面试中的公共问题.md rename to Day91-100/99.面试中的公共问题.md diff --git a/Python惯例.md b/Python惯例.md index df875fa..54b0256 100644 --- a/Python惯例.md +++ b/Python惯例.md @@ -5,7 +5,6 @@ 1. 让代码既可以被导入又可以被执行。 ```Python - if __name__ == '__main__': ``` @@ -13,7 +12,6 @@ 2. 用下面的方式判断逻辑“真”或“假”。 ```Python - if x: if not x: ``` @@ -21,7 +19,6 @@ **好**的代码: ```Python - name = 'jackfrued' fruits = ['apple', 'orange', 'grape'] owners = {'1001': '骆昊', '1002': '王大锤'} @@ -32,7 +29,6 @@ **不好**的代码: ```Python - name = 'jackfrued' fruits = ['apple', 'orange', 'grape'] owners = {'1001': '骆昊', '1002': '王大锤'} @@ -43,7 +39,6 @@ 3. 善于使用in运算符。 ```Python - if x in items: # 包含 for x in items: # 迭代 ``` @@ -51,7 +46,6 @@ **好**的代码: ```Python - name = 'Hao LUO' if 'L' in name: print('The name has an L in it.') @@ -60,7 +54,6 @@ **不好**的代码: ```Python - name = 'Hao LUO' if name.find('L') != -1: print('This name has an L in it!') @@ -69,7 +62,6 @@ 4. 不使用临时变量交换两个值。 ```Python - a, b = b, a ``` @@ -78,7 +70,6 @@ **好**的代码: ```Python - chars = ['j', 'a', 'c', 'k', 'f', 'r', 'u', 'e', 'd'] name = ''.join(chars) print(name) # jackfrued @@ -87,7 +78,6 @@ **不好**的代码: ```Python - chars = ['j', 'a', 'c', 'k', 'f', 'r', 'u', 'e', 'd'] name = '' for char in chars: @@ -104,7 +94,6 @@ **好**的代码: ```Python - d = {'x': '5'} try: value = int(d['x']) @@ -116,7 +105,6 @@ **不好**的代码: ```Python - d = {'x': '5'} if 'x' in d and isinstance(d['x'], str) \ and d['x'].isdigit(): @@ -131,7 +119,6 @@ **好**的代码: ```Python - fruits = ['orange', 'grape', 'pitaya', 'blueberry'] for index, fruit in enumerate(fruits): print(index, ':', fruit) @@ -140,7 +127,6 @@ **不好**的代码: ```Python - fruits = ['orange', 'grape', 'pitaya', 'blueberry'] index = 0 for fruit in fruits: @@ -153,7 +139,6 @@ **好**的代码: ```Python - data = [7, 20, 3, 15, 11] result = [num * 3 for num in data if num > 10] print(result) # [60, 45, 33] @@ -162,7 +147,6 @@ **不好**的代码: ```Python - data = [7, 20, 3, 15, 11] result = [] for i in data: @@ -176,7 +160,6 @@ **好**的代码: ```Python - keys = ['1001', '1002', '1003'] values = ['骆昊', '王大锤', '白元芳'] d = dict(zip(keys, values)) @@ -186,7 +169,6 @@ **不好**的代码: ```Python - keys = ['1001', '1002', '1003'] values = ['骆昊', '王大锤', '白元芳'] d = {} diff --git a/README.md b/README.md index 68be96b..178c77b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,10 @@ ## Python - 100天从新手到大师 > 作者:骆昊 +> +> 说明:最近有很多想学习Python的小伙伴申请单独加我微信和QQ,因为我自己平时也很忙,没办法一一解答大家的问题,我创建了**Python100天学习交流2群**和**Python100天学习交流4群**两个交流群,群号分别为**213132758**和**661325483**,二维码在下方(建议大家加4群,因为2群也即将满员)。我的同事和朋友很多也在这个群里,他们都是优秀的Python开发者,有丰富的商业项目经验,我们在时间充足的时候会及时解答大家的问题,而且从Python语言入门到Web应用开发,从数据分析到机器学习,每个领域都有技术大咖为大家解惑答疑。以后我们争取每周做一次视频直播,以专题的形式分享Python开发的点点滴滴,同时还会不定期的举办线上和线下的技术交流和分享活动,小伙伴们可以加群进行交流。感谢**千锋教育Python教学部**对本群的支持。 + +![](./res/python-100-days-qq-group.png) ### Python应用领域和就业形势分析 @@ -35,7 +39,7 @@ ![](./res/python-bj-salary.png) -![](./res/python-cd-salary.png) +![](./res/python-salary-chengdu.png) 给初学者的几个建议: @@ -47,7 +51,7 @@ ### Day01~15 - [Python语言基础](./Day01-15) -#### Day01 - [初识Python](./Day01-15/Day01/初识Python.md) +#### Day01 - [初识Python](./Day01-15/01.初识Python.md) - Python简介 - Python的历史 / Python的优缺点 / Python的应用领域 - 搭建编程环境 - Windows环境 / Linux环境 / MacOS环境 @@ -55,7 +59,7 @@ - 使用IDLE - 交互式环境(REPL) / 编写多行代码 / 运行程序 / 退出IDLE - 注释 - 注释的作用 / 单行注释 / 多行注释 -#### Day02 - [语言元素](./Day01-15/Day02/语言元素.md) +#### Day02 - [语言元素](./Day01-15/02.语言元素.md) - 程序和进制 - 指令和程序 / 冯诺依曼机 / 二进制和十进制 / 八进制和十六进制 - 变量和类型 - 变量的命名 / 变量的使用 / input函数 / 检查变量类型 / 类型转换 @@ -63,25 +67,25 @@ - 运算符 - 数学运算符 / 赋值运算符 / 比较运算符 / 逻辑运算符 / 身份运算符 / 运算符的优先级 - 应用案例 - 华氏温度转换成摄氏温度 / 输入圆的半径计算周长和面积 / 输入年份判断是否是闰年 -#### Day03 - [分支结构](./Day01-15/Day03/分支结构.md) +#### Day03 - [分支结构](./Day01-15/03.分支结构.md) - 分支结构的应用场景 - 条件 / 缩进 / 代码块 / 流程图 - if语句 - 简单的if / if-else结构 / if-elif-else结构 / 嵌套的if - 应用案例 - 用户身份验证 / 英制单位与公制单位互换 / 掷骰子决定做什么 / 百分制成绩转等级制 / 分段函数求值 / 输入三条边的长度如果能构成三角形就计算周长和面积 -#### Day04 - [循环结构](./Day01-15/Day04/循环结构.md) +#### Day04 - [循环结构](./Day01-15/04.循环结构.md) - 循环结构的应用场景 - 条件 / 缩进 / 代码块 / 流程图 - while循环 - 基本结构 / break语句 / continue语句 - for循环 - 基本结构 / range类型 / 循环中的分支结构 / 嵌套的循环 / 提前结束程序 - 应用案例 - 1~100求和 / 判断素数 / 猜数字游戏 / 打印九九表 / 打印三角形图案 / 猴子吃桃 / 百钱百鸡 -#### Day05 - [总结和练习](./Day01-15/Day05/总结和练习.md) +#### Day05 - [构造程序逻辑](./Day01-15/05.构造程序逻辑.md) - 基础练习 - 水仙花数 / 完美数 / 五人分鱼 / Fibonacci数列 / 回文素数 - 综合练习 - Craps赌博游戏 -#### Day06 - [函数和模块的使用](./Day01-15/Day06/函数和模块的使用.md) +#### Day06 - [函数和模块的使用](./Day01-15/06.函数和模块的使用.md) - 函数的作用 - 代码的坏味道 / 用函数封装功能模块 - 定义函数 - def语句 / 函数名 / 参数列表 / return语句 / 调用自定义函数 @@ -91,7 +95,7 @@ - 作用域问题 - 局部作用域 / 嵌套作用域 / 全局作用域 / 内置作用域 / 和作用域相关的关键字 - 用模块管理函数 - 模块的概念 / 用自定义模块管理函数 / 命名冲突的时候会怎样(同一个模块和不同的模块) -#### Day07 - [字符串和常用数据结构](./Day01-15/Day07/字符串和常用数据结构.md) +#### Day07 - [字符串和常用数据结构](./Day01-15/07.字符串和常用数据结构.md) - 字符串的使用 - 计算长度 / 下标运算 / 切片 / 常用方法 - 列表基本用法 - 定义列表 / 用下表访问元素 / 下标越界 / 添加元素 / 删除元素 / 修改元素 / 切片 / 循环遍历 @@ -105,7 +109,7 @@ - 基础练习 - 跑马灯效果 / 列表找最大元素 / 统计考试成绩的平均分 / Fibonacci数列 / 杨辉三角 - 综合案例 - 双色球选号 / 井字棋 -#### Day08 - [面向对象编程基础](./Day01-15/Day08/面向对象编程基础.md) +#### Day08 - [面向对象编程基础](./Day01-15/08.面向对象编程基础.md) - 类和对象 - 什么是类 / 什么是对象 / 面向对象其他相关概念 - 定义类 - 基本结构 / 属性和方法 / 构造器 / 析构器 / \_\_str\_\_方法 @@ -113,7 +117,7 @@ - 面向对象的四大支柱 - 抽象 / 封装 / 继承 / 多态 - 基础练习 - 定义学生类 / 定义时钟类 / 定义图形类 / 定义汽车类 -#### Day09 - [面向对象进阶](./Day01-15/Day09/面向对象进阶.md) +#### Day09 - [面向对象进阶](./Day01-15/09.面向对象进阶.md) - 属性 - 类属性 / 实例属性 / 属性访问器 / 属性修改器 / 属性删除器 / 使用\_\_slots\_\_ - 类中的方法 - 实例方法 / 类方法 / 静态方法 @@ -122,13 +126,13 @@ - 继承和多态 - 什么是继承 / 继承的语法 / 调用父类方法 / 方法重写 / 类型判定 / 多重继承 / 菱形继承(钻石继承)和C3算法 - 综合案例 - 工资结算系统 / 图书自动折扣系统 / 自定义分数类 -#### Day10 - [图形用户界面和游戏开发](./Day01-15/Day10/图形用户界面和游戏开发.md) +#### Day10 - [图形用户界面和游戏开发](./Day01-15/10.图形用户界面和游戏开发.md) - 使用tkinter开发GUI - 使用pygame三方库开发游戏应用 - “大球吃小球”游戏 -#### Day11 - [文件和异常](./Day01-15/Day11/文件和异常.md) +#### Day11 - [文件和异常](./Day01-15/11.文件和异常.md) - 读文件 - 读取整个文件 / 逐行读取 / 文件路径 - 写文件 - 覆盖写入 / 追加写入 / 文本文件 / 二进制文件 @@ -136,40 +140,38 @@ - 数据持久化 - CSV文件概述 / csv模块的应用 / JSON数据格式 / json模块的应用 - 综合案例 - 歌词解析 -#### Day12 - [字符串和正则表达式](./Day01-15/Day12/字符串和正则表达式.md) +#### Day12 - [字符串和正则表达式](./Day01-15/12.字符串和正则表达式.md) - 字符串高级操作 - 转义字符 \ 原始字符串 \ 多行字符串 \ in和 not in运算符 \ is开头的方法 \ join和split方法 \ strip相关方法 \ pyperclip模块 \ 不变字符串和可变字符串 \ StringIO的使用 - 正则表达式入门 - 正则表达式的作用 \ 元字符 \ 转义 \ 量词 \ 分组 \ 零宽断言 \贪婪匹配与惰性匹配懒惰 \ 使用re模块实现正则表达式操作(匹配、搜索、替换、捕获) - 使用正则表达式 - re模块 \ compile函数 \ group和groups方法 \ match方法 \ search方法 \ findall和finditer方法 \ sub和subn方法 \ split方法 - 应用案例 - 使用正则表达式验证输入的字符串 -#### Day13 - [进程和线程](./Day01-15/Day13/进程和线程.md) +#### Day13 - [进程和线程](./Day01-15/13.进程和线程.md) - 进程和线程的概念 - 什么是进程 / 什么是线程 / 多线程的应用场景 - 使用进程 - fork函数 / multiprocessing模块 / 进程池 / 进程间通信 - 使用线程 - thread模块 / threading模块 / Thread类 / Lock类 / Condition类 / 线程池 -#### Day14-A - [网络编程入门](./Day01-15/Day14-A/网络编程入门.md) +#### Day14 - [网络编程入门和网络应用开发](./Day01-15/14.网络编程入门和网络应用开发.md) - 计算机网络基础 - 计算机网络发展史 / “TCP-IP”模型 / IP地址 / 端口 / 协议 / 其他相关概念 - 网络应用架构 - “客户端-服务器”架构 / “浏览器-服务器”架构 - Python网络编程 - 套接字的概念 / socket模块 / socket函数 / 创建TCP服务器 / 创建TCP客户端 / 创建UDP服务器 / 创建UDP客户端 / SocketServer模块 -#### Day14-B - [网络应用开发](./Day01-15/Day14-B/网络应用开发.md) - - 访问网络API - 网络API概述 / 访问URL / requests模块 / 解析JSON格式数据 - 文件传输 - FTP协议 / ftplib模块 / 交互式FTP应用 - 电子邮件 - SMTP协议 / POP3协议 / IMAP协议 / smtplib模块 / poplib模块 / imaplib模块 - 短信服务 - twilio模块 / 国内的短信服务 -#### Day15 - [图像和文档处理](./Day01-15/Day15/图像和办公文档处理.md) +#### Day15 - [图像和文档处理](./Day01-15/15.图像和办公文档处理.md) - 用Pillow处理图片 - 图片读写 / 图片合成 / 几何变换 / 色彩转换 / 滤镜效果 - 读写Word文档 - 文本内容的处理 / 段落 / 页眉和页脚 / 样式的处理 - 读写Excel文件 - xlrd模块 / xlwt模块 - 生成PDF文件 - pypdf2模块 / reportlab模块 -### Day16~Day20 - [Python语言进阶 ](./Day16-20/Python语言进阶.md) +### Day16~Day20 - [Python语言进阶 ](./Day16-20/16.Python语言进阶.md) - 常用数据结构 - 函数的高级用法 - “一等公民” / 高阶函数 / Lambda函数 / 作用域和闭包 / 装饰器 @@ -177,7 +179,7 @@ - 迭代器和生成器 - 相关魔术方法 / 创建生成器的两种方式 / - 并发和异步编程 - 多线程 / 多进程 / 异步IO / async和await -### Day21~30 - [Web前端入门](./Day21-30/Web前端概述.md) +### Day21~30 - [Web前端入门](./Day21-30/21.Web前端概述.md) - 用HTML标签承载页面内容 - 用CSS渲染页面 @@ -187,7 +189,7 @@ - Element的使用 - Bootstrap的使用 -### Day31~35 - [玩转Linux操作系统](./Day31-35/玩转Linux操作系统.md) +### Day31~35 - [玩转Linux操作系统](./Day31-35/31.玩转Linux操作系统.md) - 操作系统发展史和Linux概述 - Linux基础命令 @@ -201,7 +203,7 @@ ### Day36~40 - [数据库基础和进阶](./Day36-40) -- [关系型数据库MySQL](./Day36-40/关系型数据库MySQL.md) +- [关系型数据库MySQL](./Day36-40/36.关系型数据库MySQL.md) - 关系型数据库概述 - MySQL的安装和使用 - SQL的使用 @@ -213,21 +215,21 @@ - 数据完整性 - 数据一致性 - 在Python中操作MySQL -- [NoSQL入门](./Day36-40/NoSQL入门.md) +- [NoSQL入门](./Day36-40/39.NoSQL入门.md) - NoSQL概述 - Redis概述 - Mongo概述 ### Day41~55 - [实战Django](./Day41-55) -#### Day41 - [快速上手](./Day41-55/01.快速上手.md) +#### Day41 - [快速上手](./Day41-55/41.快速上手.md) - Web应用工作原理和HTTP协议 - Django框架概述 - 5分钟快速上手 - 使用视图模板 -#### Day42 - [深入模型](./Day41-55/02.深入模型.md) +#### Day42 - [深入模型](./Day41-55/42.深入模型.md) - 关系型数据库配置 - 管理后台的使用 @@ -235,80 +237,125 @@ - Django模型最佳实践 - 模型定义参考 -#### Day43 - [静态资源和Ajax请求](./Day41-55/03.静态资源和Ajax请求.md) +#### Day43 - [静态资源和Ajax请求](./Day41-55/43.静态资源和Ajax请求.md) - 加载静态资源 - 用Ajax请求获取数据 -#### Day44 - [表单的应用](./Day41-55/04.表单的应用.md) +#### Day44 - [表单的应用](./Day41-55/44.表单的应用.md) -#### Day45 - [Cookie和Session](./Day41-55/05.Cookie和Session.md) +- 表单和表单控件 +- 跨站请求伪造和CSRF令牌 +- Form和ModelForm +- 表单验证 -#### Day46 - [中间件的应用](./Day41-55/06.中间件的应用.md) +#### Day45 - [Cookie和Session](./Day41-55/45.Cookie和Session.md) -#### Day47 - [日志和缓存](./Day41-55/07.日志和缓存.md) +- 实现用户跟踪 +- cookie和session的关系 +- Django框架对session的支持 +- 视图函数中的cookie读写操作 -#### Day48 - [文件上传和富文本编辑](./Day41-55/08.文件上传.md) +#### Day46 - [中间件的应用](./Day41-55/46.中间件的应用.md) -#### Day49 - [文件下载和报表](./Day41-55/09.文件下载和报表.md) +- 什么是中间件 +- Django框架内置的中间件 +- 自定义中间件及其应用场景 -#### Day50 - [RESTful架构和DRF入门](./Day41-55/10.RESTful架构和DRF入门.md) +#### Day47 - [日志和调试](./Day41-55/47.日志和调试.md) -#### Day51 - [RESTful架构和DRF进阶](./Day41-55/11.RESTful架构和DRF进阶.md) +- 配置日志 +- 配置和使用Django-Debug-Toolbar -#### Day52 - [使用缓存](./Day41-55/12.使用缓存.md) +#### Day48 - [文件上传和富文本编辑](./Day41-55/48.文件上传.md) -#### Day53 - [短信和邮件](./Day41-55/13.短信和邮件.md) +- 文件上传表单控件和图片文件预览 +- 服务器端如何处理上传的文件 +- 富文本编辑器概述 +- wangEditor的使用 -#### Day54 - [异步任务和定时任务](./Day41-55/14.异步任务和定时任务.md) +#### Day49 - [文件下载和报表](./Day41-55/49.文件下载和报表.md) -#### Day55 - [单元测试和项目上线](./Day41-55/15.单元测试和项目上线.md) +- 通过HttpResponse修改响应头 +- 使用StreamingHttpResponse处理大文件 +- 使用xlwt生成Excel报表 +- 使用reportlab生成PDF报表 +- 使用ECharts生成前端图表 -- 项目开发流程和相关工具 -- 生成非HTML内容 -- 项目部署和测试 -- 项目性能初步调优 -- Web应用安全保护 +#### Day50 - [RESTful架构和DRF入门](./Day41-55/50.RESTful架构和DRF入门.md) + +#### Day51 - [RESTful架构和DRF进阶](./Day41-55/51.RESTful架构和DRF进阶.md) + +#### Day52 - [使用缓存](./Day41-55/52.使用缓存.md) + +- 网站优化第一定律 + +- 在Django项目中使用Redis提供缓存服务 +- 在视图函数中读写缓存 +- 使用装饰器实现页面缓存 +- 为数据接口提供缓存服务 + +#### Day53 - [短信和邮件](./Day41-55/53.短信和邮件.md) + +- 常用短信网关平台介绍 +- 使用螺丝帽发送短信 +- Django框架对邮件服务的支持 + +#### Day54 - [异步任务和定时任务](./Day41-55/54.异步任务和定时任务.md) + +- 网站优化第二定律 +- 配置消息队列服务 +- 在项目中使用celery实现任务异步化 +- 在项目中使用celery实现定时任务 + +#### Day55 - [单元测试和项目上线](./Day41-55/55.单元测试和项目上线.md) + +- Python中的单元测试 +- Django框架对单元测试的支持 +- 使用版本控制系统 +- 配置和使用uWSGI +- 动静分离和Nginx配置 +- 配置HTTPS ### Day56~60 - [实战Flask](./Day56-65) -#### Day56 - [Flask入门](./Day56-60/01.Flask入门.md) +#### Day56 - [Flask入门](./Day56-60/56.Flask入门.md) -#### Day57 - [模板的使用](./Day56-60/02.模板的使用.md) +#### Day57 - [模板的使用](./Day56-60/57.模板的使用.md) -#### Day58 - [表单的处理](./Day56-60/03.表单的处理.md) +#### Day58 - [表单的处理](./Day56-60/58.表单的处理.md) -#### Day59 - [数据库操作](./Day56-60/04.数据库操作.md) +#### Day59 - [数据库操作](./Day56-60/59.数据库操作.md) -#### Day60 - [项目实战](./Day56-60/05.项目实战.md) +#### Day60 - [项目实战](./Day56-60/60.项目实战.md) ### Day61~65 - [实战Tornado](./Day61-65) -#### Day61 - [预备知识](./Day61-65/01.预备知识.md) +#### Day61 - [预备知识](./Day61-65/61.预备知识.md) - 并发编程 - I/O模式和事件驱动 -#### Day62 - [Tornado入门](./Day61-65/02.Tornado入门.md) +#### Day62 - [Tornado入门](./Day61-65/62.Tornado入门.md) - Tornado概述 - 5分钟上手Tornado - 路由解析 - 请求处理器 -#### Day63 - [异步化](./Day61-65/03.异步化.md) +#### Day63 - [异步化](./Day61-65/63.异步化.md) - aiomysql和aioredis的使用 -#### Day64 - [WebSocket的应用](./Day61-65/04.WebSocket的应用.md) +#### Day64 - [WebSocket的应用](./Day61-65/64.WebSocket的应用.md) - WebSocket简介 - WebSocket服务器端编程 - WebSocket客户端编程 - 项目:Web聊天室 -#### Day65 - [项目实战](./Day61-65/05.项目实战.md) +#### Day65 - [项目实战](./Day61-65/65.项目实战.md) - 前后端分离开发和接口文档的撰写 - 使用Vue.js实现前端渲染 @@ -317,61 +364,61 @@ ### Day66~75 - [爬虫开发](./Day66-75) -#### Day66 - [网络爬虫和相关工具](./Day66-75/01.网络爬虫和相关工具.md) +#### Day66 - [网络爬虫和相关工具](./Day66-75/66.网络爬虫和相关工具.md) -#### Day67 - [数据采集和解析](./Day66-75/02.数据采集和解析.md) +#### Day67 - [数据采集和解析](./Day66-75/67.数据采集和解析.md) -#### Day68 - [存储数据](./Day66-75/03.存储数据.md) +#### Day68 - [存储数据](./Day66-75/68.存储数据.md) -#### Day69 - [并发下载](./Day66-75/04.并发下载.md) +#### Day69 - [并发下载](./Day66-75/69.并发下载.md) -#### Day70 - [解析动态内容](./Day66-75/05.解析动态内容.md) +#### Day70 - [解析动态内容](./Day66-75/70.解析动态内容.md) -#### Day71 - [表单交互和验证码处理](./Day66-75/06.表单交互和验证码处理.md) +#### Day71 - [表单交互和验证码处理](./Day66-75/71.表单交互和验证码处理.md) -#### Day72 - [Scrapy入门](./Day66-75/07.Scrapy入门.md) +#### Day72 - [Scrapy入门](./Day66-75/72.Scrapy入门.md) -#### Day73 - [Scrapy高级应用](./Day66-75/08.Scrapy高级应用.md) +#### Day73 - [Scrapy高级应用](./Day66-75/73.Scrapy高级应用.md) -#### Day74 - [Scrapy分布式实现](./Day66-75/09.Scrapy分布式实现.md) +#### Day74 - [Scrapy分布式实现](./Day66-75/74.Scrapy分布式实现.md) -#### Day75 - [爬虫项目实战](./Day66-75/10.爬虫项目实战.md) +#### Day75 - [爬虫项目实战](./Day66-75/75.爬虫项目实战.md) ### Day76~90 - [数据处理和机器学习](./Day76-90) -#### Day76 - [机器学习基础](./Day76-90/01.机器学习基础.md) +#### Day76 - [机器学习基础](./Day76-90/76.机器学习基础.md) -#### Day77 - [Pandas的应用](./Day76-90/02.Pandas的应用.md) +#### Day77 - [Pandas的应用](./Day76-90/77.Pandas的应用.md) -#### Day78 - [NumPy和SciPy的应用](./Day76-90/03.NumPy和SciPy的应用) +#### Day78 - [NumPy和SciPy的应用](./Day76-90/78.NumPy和SciPy的应用) -#### Day79 - [Matplotlib和数据可视化](./Day76-90/04.Matplotlib和数据可视化) +#### Day79 - [Matplotlib和数据可视化](./Day76-90/79.Matplotlib和数据可视化) -#### Day80 - [k最近邻(KNN)分类](./Day76-90/05.k最近邻分类.md) +#### Day80 - [k最近邻(KNN)分类](./Day76-90/80.k最近邻分类.md) -#### Day81 - [决策树](./Day76-90/06.决策树.md) +#### Day81 - [决策树](./Day76-90/81.决策树.md) -#### Day82 - [贝叶斯分类](./Day76-90/07.贝叶斯分类.md) +#### Day82 - [贝叶斯分类](./Day76-90/82.贝叶斯分类.md) -#### Day83 - [支持向量机(SVM)](./Day76-90/08.支持向量机.md) +#### Day83 - [支持向量机(SVM)](./Day76-90/83.支持向量机.md) -#### Day84 - [K-均值聚类](./Day76-90/09.K-均值聚类.md) +#### Day84 - [K-均值聚类](./Day76-90/84.K-均值聚类.md) -#### Day85 - [回归分析](./Day76-90/10.回归分析.md) +#### Day85 - [回归分析](./Day76-90/85.回归分析.md) -#### Day86 - [大数据分析入门](./Day76-90/11.大数据分析入门.md) +#### Day86 - [大数据分析入门](./Day76-90/86.大数据分析入门.md) -#### Day87 - [大数据分析进阶](./Day76-90/12.大数据分析进阶.md) +#### Day87 - [大数据分析进阶](./Day76-90/87.大数据分析进阶.md) -#### Day88 - [Tensorflow入门](./Day76-90/13.Tensorflow入门.md) +#### Day88 - [Tensorflow入门](./Day76-90/88.Tensorflow入门.md) -#### Day89 - [Tensorflow实战](./Day76-90/14.Tensorflow实战.md) +#### Day89 - [Tensorflow实战](./Day76-90/89.Tensorflow实战.md) -#### Day90 - [推荐系统](./Day76-90/15.推荐系统.md) +#### Day90 - [推荐系统](./Day76-90/90.推荐系统.md) ### Day91~100 - [团队项目开发](./Day91-100) -#### 第91天:团队开发和项目选题 +#### 第91天:[团队项目开发准备](./Day91-100/91.团队项目开发准备.md) 1. 软件过程模型 - 经典过程模型(瀑布模型) @@ -395,15 +442,15 @@ > - **客户合作** 高于 合同谈判 > - **响应变化** 高于 遵循计划 - ![](./res/the-daily-scrum-in-the-sprint-cycle.png) + ![](./res/agile-scrum-sprint-cycle.png) - > 角色:产品所有者(决定做什么,能对需求拍板的人)、团队负责人(解决各种问题,专注如何更好的工作,屏蔽外部对开发团队的影响)、开发团队(项目执行人员,具体指开发人员和测试人员)。 - > - > 准备工作:商业案例和资金、合同、憧憬、初始产品需求、初始发布计划、入股、组建团队。 - > - > 敏捷团队通常人数为8-10人。 - > - > 工作量估算:将开发任务量化,包括原型、Logo设计、UI设计、前端开发等,尽量把每个工作分解到最小任务量,最小任务量标准为工作时间不能超过两天,然后估算总体项目时间。把每个任务都贴在白板上面,白板上分三部分:to do(待完成)、in progress(进行中)和done(已完成)。 + > 角色:产品所有者(决定做什么,能对需求拍板的人)、团队负责人(解决各种问题,专注如何更好的工作,屏蔽外部对开发团队的影响)、开发团队(项目执行人员,具体指开发人员和测试人员)。 + + > 准备工作:商业案例和资金、合同、憧憬、初始产品需求、初始发布计划、入股、组建团队。 + + > 敏捷团队通常人数为8-10人。 + + > 工作量估算:将开发任务量化,包括原型、Logo设计、UI设计、前端开发等,尽量把每个工作分解到最小任务量,最小任务量标准为工作时间不能超过两天,然后估算总体项目时间。把每个任务都贴在白板上面,白板上分三部分:to do(待完成)、in progress(进行中)和done(已完成)。 2. 项目团队组建 @@ -460,7 +507,9 @@ | | 查看评论 | 白元芳 | 正在进行 | 20% | 4 | 2018/8/7 | | 2018/8/7 | | 需要进行代码审查 | | | 评论投票 | 白元芳 | 等待 | 0% | 4 | 2018/8/8 | | 2018/8/8 | | | -#### 第92天:数据库设计和OOAD +#### 第92天:[Docker的使用](./Day91-100/92.Docker在项目中的应用.md) + +#### 第93天:[数据库设计和OOAD](./Day91-100/93.数据库设计和OOAD.md) ##### 概念模型和正向工程 @@ -487,9 +536,9 @@ python manage.py inspectdb > app/models.py ``` -#### 第93-98天:使用Django开发项目 +#### 第94天:[网络API接口设计](./Day91-100/94.网络API接口设计) -> 说明:具体内容请参考[《Django知识点概述》](Day91-100/Django知识点概述.md) +#### 第95天:[使用Django开发项目](./Day91-100/95.使用Django开发项目.md) ##### 项目开发中的公共问题 @@ -505,7 +554,7 @@ - [理解RESTful架构](http://www.ruanyifeng.com/blog/2011/09/restful.html) - [RESTful API设计指南](http://www.ruanyifeng.com/blog/2014/05/restful_api.html) - [RESTful API最佳实践](http://www.ruanyifeng.com/blog/2018/10/restful-api-best-practices.html) -2. API接口文档的撰写([《网络API接口设计》](Day91-100/网络API接口设计.md)) +2. API接口文档的撰写 - [RAP2](http://rap2.taobao.org/) - [YAPI](http://yapi.demo.qunar.com/) 3. [django-REST-framework](https://www.django-rest-framework.org/)的应用 @@ -515,7 +564,7 @@ 1. 使用缓存缓解数据库压力 - Redis 2. 使用消息队列做解耦合和削峰 - Celery + RabbitMQ -#### 第99-100天:测试和部署 +#### 第96天:[软件测试和自动化测试](Day91-100/96.软件测试和自动化测试.md) ##### 单元测试 @@ -525,8 +574,6 @@ ##### 项目部署 -> 说明:请参考[《项目部署上线指南》](Day91-100/项目部署上线指南.md)。 - 1. 部署前的准备工作 - 关键设置(SECRET_KEY / DEBUG / ALLOWED_HOSTS / 缓存 / 数据库) - HTTPS / CSRF_COOKIE_SECUR / SESSION_COOKIE_SECURE @@ -543,8 +590,6 @@ ##### 性能测试 -> 说明:具体内容请参考[《Django知识点概述》](Day91-100/Django知识点概述.md)。 - 1. AB的使用 2. SQLslap的使用 3. sysbench的使用 @@ -558,35 +603,23 @@ - Selenium Remote Control 3. 测试工具Robot Framework介绍 -##### 项目性能调优 +#### 第97天:[电商网站技术要点剖析](./Day91-100/97.电商网站技术要点剖析.md) -1. 数据库性能调优 - 请参考[《MySQL相关知识》](Day91-100/MySQL相关知识.md) - - 软硬件优化 - - - SQL优化 - - - 架构优化 - - - 分表分库 - - - 主从复制,读写分离 - - 集群架构 +#### 第98天:[项目部署上线和性能调优](./Day91-100/98.项目部署上线和性能调优.md) +1. MySQL数据库调优 2. Web服务器性能优化 - - Nginx负载均衡配置 - - Keepalived实现高可用 - 3. 代码性能调优 - - 多线程 - 异步化 - 4. 静态资源访问优化 - - 云存储 - - CDN + - 云存储 + - CDN +#### 第99天:[面试中的公共问题](./Day91-100/99.面试中的公共问题.md) +#### 第100天:[英语面试](./Day91-100/100.英语面试.md) > 致谢:感谢的我的同事古晔、张旭、肖世荣、王海飞、荣佳伟、路丰坤等在技术上给予的指导和帮助。 \ No newline at end of file diff --git a/res/01.django_single_server.png b/res/01.django_single_server.png deleted file mode 100644 index 70d3bf3..0000000 Binary files a/res/01.django_single_server.png and /dev/null differ diff --git a/res/02.django_dedicated_db_server.png b/res/02.django_dedicated_db_server.png deleted file mode 100644 index 143eb6e..0000000 Binary files a/res/02.django_dedicated_db_server.png and /dev/null differ diff --git a/res/03.django_dedicated_static_server.png b/res/03.django_dedicated_static_server.png deleted file mode 100644 index f13a247..0000000 Binary files a/res/03.django_dedicated_static_server.png and /dev/null differ diff --git a/res/04.django_load_balance.png b/res/04.django_load_balance.png deleted file mode 100644 index 16e7515..0000000 Binary files a/res/04.django_load_balance.png and /dev/null differ diff --git a/res/05.django_massive_cluster.png b/res/05.django_massive_cluster.png deleted file mode 100644 index 9163fb2..0000000 Binary files a/res/05.django_massive_cluster.png and /dev/null differ diff --git a/res/Celery_RabitMQ.png b/res/Celery_RabitMQ.png deleted file mode 100644 index 4140130..0000000 Binary files a/res/Celery_RabitMQ.png and /dev/null differ diff --git a/res/Producer-Broker-Consumer-Arrangement.png b/res/Producer-Broker-Consumer-Arrangement.png deleted file mode 100644 index b073a60..0000000 Binary files a/res/Producer-Broker-Consumer-Arrangement.png and /dev/null differ diff --git a/res/abstraction-view.png b/res/abstraction-view.png deleted file mode 100644 index 39ca0a5..0000000 Binary files a/res/abstraction-view.png and /dev/null differ diff --git a/res/the-daily-scrum-in-the-sprint-cycle.png b/res/agile-scrum-sprint-cycle.png similarity index 100% rename from res/the-daily-scrum-in-the-sprint-cycle.png rename to res/agile-scrum-sprint-cycle.png diff --git a/res/algorithm_complexity_1.png b/res/algorithm_complexity_1.png deleted file mode 100644 index 952889d..0000000 Binary files a/res/algorithm_complexity_1.png and /dev/null differ diff --git a/res/algorithm_complexity_2.png b/res/algorithm_complexity_2.png deleted file mode 100644 index 4c14249..0000000 Binary files a/res/algorithm_complexity_2.png and /dev/null differ diff --git a/res/alipay_web_developer.png b/res/alipay_web_developer.png deleted file mode 100644 index 0716552..0000000 Binary files a/res/alipay_web_developer.png and /dev/null differ diff --git a/res/aliyun-certificate.png b/res/aliyun-certificate.png deleted file mode 100644 index c19a30b..0000000 Binary files a/res/aliyun-certificate.png and /dev/null differ diff --git a/res/aliyun-dnslist.png b/res/aliyun-dnslist.png deleted file mode 100644 index e6eda2f..0000000 Binary files a/res/aliyun-dnslist.png and /dev/null differ diff --git a/res/aliyun-domain.png b/res/aliyun-domain.png deleted file mode 100644 index 64a1f48..0000000 Binary files a/res/aliyun-domain.png and /dev/null differ diff --git a/res/aliyun-keeprecord.png b/res/aliyun-keeprecord.png deleted file mode 100644 index d9d39bc..0000000 Binary files a/res/aliyun-keeprecord.png and /dev/null differ diff --git a/res/aliyun-resolve-settings.png b/res/aliyun-resolve-settings.png deleted file mode 100644 index 3f9363a..0000000 Binary files a/res/aliyun-resolve-settings.png and /dev/null differ diff --git a/res/app_folder_arch.png b/res/app_folder_arch.png deleted file mode 100644 index 7d51e95..0000000 Binary files a/res/app_folder_arch.png and /dev/null differ diff --git a/res/builtin-middlewares.png b/res/builtin-middlewares.png deleted file mode 100644 index 59acb4b..0000000 Binary files a/res/builtin-middlewares.png and /dev/null differ diff --git a/res/celery.png b/res/celery.png deleted file mode 100644 index a8d5f7d..0000000 Binary files a/res/celery.png and /dev/null differ diff --git a/res/celery_architecture.png b/res/celery_architecture.png deleted file mode 100644 index d3118a0..0000000 Binary files a/res/celery_architecture.png and /dev/null differ diff --git a/res/class-and-object.png b/res/class-and-object.png deleted file mode 100644 index fa0f995..0000000 Binary files a/res/class-and-object.png and /dev/null differ diff --git a/res/click-jacking.png b/res/click-jacking.png deleted file mode 100644 index 9d92979..0000000 Binary files a/res/click-jacking.png and /dev/null differ diff --git a/res/concurrency.png b/res/concurrency.png deleted file mode 100644 index 3e47d97..0000000 Binary files a/res/concurrency.png and /dev/null differ diff --git a/res/deployment_pipelines.png b/res/deployment_pipelines.png deleted file mode 100644 index 6db6c2d..0000000 Binary files a/res/deployment_pipelines.png and /dev/null differ diff --git a/res/django-middleware.png b/res/django-middleware.png deleted file mode 100644 index 9885582..0000000 Binary files a/res/django-middleware.png and /dev/null differ diff --git a/res/django-mtv.png b/res/django-mtv.png deleted file mode 100644 index 76f330e..0000000 Binary files a/res/django-mtv.png and /dev/null differ diff --git a/res/django_request_response_cycle.png b/res/django_request_response_cycle.png deleted file mode 100644 index 3423c9a..0000000 Binary files a/res/django_request_response_cycle.png and /dev/null differ diff --git a/res/docker_logo.png b/res/docker_logo.png deleted file mode 100644 index 19ff797..0000000 Binary files a/res/docker_logo.png and /dev/null differ diff --git a/res/docker_vs_vm.png b/res/docker_vs_vm.png deleted file mode 100644 index 0eb6fea..0000000 Binary files a/res/docker_vs_vm.png and /dev/null differ diff --git a/res/encapsulation.png b/res/encapsulation.png deleted file mode 100644 index 437df0a..0000000 Binary files a/res/encapsulation.png and /dev/null differ diff --git a/res/er-graph.png b/res/er-graph.png deleted file mode 100644 index c8415be..0000000 Binary files a/res/er-graph.png and /dev/null differ diff --git a/res/git_logo.png b/res/git_logo.png deleted file mode 100644 index 04b0f00..0000000 Binary files a/res/git_logo.png and /dev/null differ diff --git a/res/git_repository.png b/res/git_repository.png deleted file mode 100644 index d85bdbd..0000000 Binary files a/res/git_repository.png and /dev/null differ diff --git a/res/gitignore_io.png b/res/gitignore_io.png deleted file mode 100644 index a305da6..0000000 Binary files a/res/gitignore_io.png and /dev/null differ diff --git a/res/greedy.png b/res/greedy.png deleted file mode 100644 index 376ceef..0000000 Binary files a/res/greedy.png and /dev/null differ diff --git a/res/http-request.png b/res/http-request.png deleted file mode 100644 index aca9287..0000000 Binary files a/res/http-request.png and /dev/null differ diff --git a/res/http-response.png b/res/http-response.png deleted file mode 100644 index f2b8ae3..0000000 Binary files a/res/http-response.png and /dev/null differ diff --git a/res/jenkins_new_project.png b/res/jenkins_new_project.png deleted file mode 100644 index 8019ada..0000000 Binary files a/res/jenkins_new_project.png and /dev/null differ diff --git a/res/modularity.png b/res/modularity.png deleted file mode 100644 index 25a6696..0000000 Binary files a/res/modularity.png and /dev/null differ diff --git a/res/multi-inheritance.png b/res/multi-inheritance.png deleted file mode 100644 index c2ae49b..0000000 Binary files a/res/multi-inheritance.png and /dev/null differ diff --git a/res/mvc.png b/res/mvc.png deleted file mode 100644 index 7ba14ba..0000000 Binary files a/res/mvc.png and /dev/null differ diff --git a/res/oauth2.png b/res/oauth2.png deleted file mode 100644 index 7a07bd3..0000000 Binary files a/res/oauth2.png and /dev/null differ diff --git a/res/object-roles.png b/res/object-roles.png deleted file mode 100644 index 41174cd..0000000 Binary files a/res/object-roles.png and /dev/null differ diff --git a/res/objects-collaborate.png b/res/objects-collaborate.png deleted file mode 100644 index bae06e8..0000000 Binary files a/res/objects-collaborate.png and /dev/null differ diff --git a/res/objects-lifetime.png b/res/objects-lifetime.png deleted file mode 100644 index 54207bb..0000000 Binary files a/res/objects-lifetime.png and /dev/null differ diff --git a/res/python-100-days-qq-group.png b/res/python-100-days-qq-group.png new file mode 100644 index 0000000..7b2339d Binary files /dev/null and b/res/python-100-days-qq-group.png differ diff --git a/res/python-cd-salary.png b/res/python-cd-salary.png deleted file mode 100644 index f728255..0000000 Binary files a/res/python-cd-salary.png and /dev/null differ diff --git a/res/python-salary-hangzhou.png b/res/python-salary-hangzhou.png deleted file mode 100644 index fc901aa..0000000 Binary files a/res/python-salary-hangzhou.png and /dev/null differ diff --git a/res/python-salary-shanghai.png b/res/python-salary-shanghai.png deleted file mode 100644 index 9402051..0000000 Binary files a/res/python-salary-shanghai.png and /dev/null differ diff --git a/res/python-salary-shenzhen.png b/res/python-salary-shenzhen.png deleted file mode 100644 index 07b6f20..0000000 Binary files a/res/python-salary-shenzhen.png and /dev/null differ diff --git a/res/python-str-join.png b/res/python-str-join.png deleted file mode 100644 index b784ffb..0000000 Binary files a/res/python-str-join.png and /dev/null differ diff --git a/res/python_salary_guangzhou.png b/res/python_salary_guangzhou.png deleted file mode 100644 index e7567e3..0000000 Binary files a/res/python_salary_guangzhou.png and /dev/null differ diff --git a/res/redmine_new_issue.png b/res/redmine_new_issue.png deleted file mode 100644 index 63971fc..0000000 Binary files a/res/redmine_new_issue.png and /dev/null differ diff --git a/res/selenium-ide.png b/res/selenium-ide.png deleted file mode 100644 index 52a4a4f..0000000 Binary files a/res/selenium-ide.png and /dev/null differ diff --git a/res/selenium_ide.png b/res/selenium_ide.png deleted file mode 100644 index 4d2f275..0000000 Binary files a/res/selenium_ide.png and /dev/null differ diff --git a/res/shopping-pdm.png b/res/shopping-pdm.png deleted file mode 100644 index 2b93a21..0000000 Binary files a/res/shopping-pdm.png and /dev/null differ diff --git a/res/uml-graph.png b/res/uml-graph.png deleted file mode 100644 index def109b..0000000 Binary files a/res/uml-graph.png and /dev/null differ diff --git a/res/uml.png b/res/uml.png deleted file mode 100644 index 98168cb..0000000 Binary files a/res/uml.png and /dev/null differ diff --git a/res/web-application.png b/res/web-application.png deleted file mode 100644 index 89d2dec..0000000 Binary files a/res/web-application.png and /dev/null differ diff --git a/res/web-queue-worker-physical.png b/res/web-queue-worker-physical.png deleted file mode 100644 index 81da48d..0000000 Binary files a/res/web-queue-worker-physical.png and /dev/null differ diff --git a/Day66-75/常见反爬策略及应对方案.md b/常见反爬策略及应对方案.md similarity index 100% rename from Day66-75/常见反爬策略及应对方案.md rename to 常见反爬策略及应对方案.md diff --git a/用函数还是用复杂的表达式.md b/用函数还是用复杂的表达式.md index b84d8cf..ebf10eb 100644 --- a/用函数还是用复杂的表达式.md +++ b/用函数还是用复杂的表达式.md @@ -15,7 +15,6 @@ else: if c > the_max: the_max = c print('The max is:', the_max) - ``` 但是我们刚才说了,程序员都是懒惰的,很多程序员都会使用三元条件运算符来改写上面的代码。 @@ -27,7 +26,6 @@ c = int(input('c = ')) the_max = a if a > b else b the_max = c if c > the_max else the_max print('The max is:', the_max) - ``` 需要说明的是,Python在2.5版本以前是没有上面代码第4行和第5行中使用的三元条件运算符的,究其原因是Guido van Rossum(Python之父)认为三元条件运算符并不能帮助 Python变得更加简洁,于是那些习惯了在C/C++或Java中使用三元条件运算符(在这些语言中,三元条件运算符也称为“Elvis运算符”,因为`?:`放在一起很像著名摇滚歌手猫王Elvis的大背头)的程序员试着用`and`和`or`运算符的短路特性来模拟出三元操作符,于是在那个年代,上面的代码是这样写的。 @@ -39,7 +37,6 @@ c = int(input('c = ')) the_max = a > b and a or b the_max = c > the_max and c or the_max print('The max is:', the_max) - ``` 但是这种做法在某些场景下是不能成立的,且看下面的代码。 @@ -60,7 +57,6 @@ a = int(input('a = ')) b = int(input('b = ')) c = int(input('c = ')) print('The max is:', (a if a > b else b) if (a if a > b else b) > c else c) - ``` 但是,这样做真的好吗?如此复杂的表达式是不是让代码变得晦涩了很多呢?我们发现,在实际开发中很多开发者都喜欢过度的使用某种语言的特性或语法糖,于是简单的多行代码变成了复杂的单行表达式,这样做真的好吗?这个问题我也不止一次的问过自己,现在我能给出的答案是下面的代码,使用辅助函数。 @@ -74,7 +70,6 @@ a = int(input('a = ')) b = int(input('b = ')) c = int(input('c = ')) print('The max is:', the_max(the_max(a, b), c)) - ``` 上面的代码中,我定义了一个辅助函数`the_max`用来找出参数传入的两个值中较大的那一个,于是下面的输出语句可以通过两次调用`the_max`函数来找出三个数中的最大值,现在代码的可读性是不是好了很多。用辅助函数来替代复杂的表达式真的是一个不错的选择,关键是比较大小的逻辑转移到这个辅助函数后不仅可以反复调用它,而且还可以进行级联操作。 @@ -86,6 +81,5 @@ a = int(input('a = ')) b = int(input('b = ')) c = int(input('c = ')) print('The max is:', max(a, b, c)) - ``` diff --git a/知乎问题回答.md b/知乎问题回答.md index 68bc00d..2d96658 100644 --- a/知乎问题回答.md +++ b/知乎问题回答.md @@ -94,4 +94,4 @@ - 《Scrum敏捷软件开发》(*Software Development using Scrum*) - 《高效团队开发 - 工具与方法》 -当然学习编程,最重要的通过项目实战来提升自己的综合能力,Github上有大量的优质开源项目,其中不乏优质的Python项目。有一个名为[“awesome-python-applications”](https://github.com/mahmoud/awesome-python-applications)的项目对这些优质的资源进行了归类并提供了传送门,大家可以了解下。除此之外,还要为大家推荐一个名为[“Python-100-Days”](https://github.com/jackfrued/Python-100-Days)的项目,上面有大量优质的Python学习资料(包括文档、代码和相关资源)。如果自学能力不是那么强,可以通过网络上免费或者付费的视频课程来学习对应的知识;如果自律性没有那么强,那就只能建议花钱参加培训班了,因为花钱在有人监督的环境下学习对很多人来说确实是一个捷径,但是要记得:“师傅领进门,修行靠各人”。选择自己热爱的东西并全力以赴,不要盲目的跟风学习,这一点算是过来人的忠告吧。记得我自己刚开始进入软件开发这个行业时,有人跟我说过这么一句话,现在也分享出来与诸君共勉:“浮躁的人有两种:只观望而不学习的人,只学习而不坚持的人;浮躁的人都不是高手。” \ No newline at end of file +当然学习编程,最重要的通过项目实战来提升自己的综合能力,Github上有大量的优质开源项目,其中不乏优质的Python项目。有一个名为[“awesome-python-applications”](https://github.com/mahmoud/awesome-python-applications)的项目对这些优质的资源进行了归类并提供了传送门,大家可以了解下。如果自学能力不是那么强,可以通过网络上免费或者付费的视频课程来学习对应的知识;如果自律性没有那么强,那就只能建议花钱参加培训班了,因为花钱在有人监督的环境下学习对很多人来说确实是一个捷径,但是要记得:“师傅领进门,修行靠各人”。选择自己热爱的东西并全力以赴,不要盲目的跟风学习,这一点算是过来人的忠告吧。记得我自己刚开始进入软件开发这个行业时,有人跟我说过这么一句话,现在也分享出来与诸君共勉:“浮躁的人有两种:只观望而不学习的人,只学习而不坚持的人;浮躁的人都不是高手。” \ No newline at end of file