Python 科学计算库—NumPy pandas库基础 异常 文件操作
Python概述 基础语法 判断 循环 函数 数据容器 函数进阶 Jupyter notebook
第2章 科学计算库—NumPy
NumPy作为高性能科学计算和数据分析的基础包,它是 众多数据分析、机器学习等工具的基础架构,掌握 NumPy的功能及其用法将有助于后续其他数据分析工具的学习。本章将针对NumPy库的基础功能进行详细地讲解。
NumPy中提供了一个重要的数据结构是ndarray(又称为array)对象,该对象是一个N 维数组对象,可以存储相同类型、以多种形式组织的数据。 与Python中的数组相比,ndarray对象可以处理结构更复杂的数据
2.1数组对象
![](https://img-blog.csdnimg.cn/img_convert/b1f4acf1377b469ebd8da49f05d25980.png)
![](https://img-blog.csdnimg.cn/img_convert/ae394dfb98af45babd4c8749fe7199fe.png)
![](https://img-blog.csdnimg.cn/img_convert/e7eb6a2d44a144c1b72558093e79f501.png)
白色部分是原始数值;红绿部分是a,b广播相加的补充;
shape属性表示数组的形状,也就是数组中各维度的大小,该属性的值为一个形如(行,列)的元组。
![](https://img-blog.csdnimg.cn/img_convert/760a9ee0406c45da8a7464a3b027f6b3.png)
![](https://img-blog.csdnimg.cn/img_convert/e6b541380a6c47f4ad0592cd13f3cacd.png)
2.2创建数组
![](https://img-blog.csdnimg.cn/img_convert/56f403e90191438f9af13bed905a108a.png)
![](https://img-blog.csdnimg.cn/img_convert/a6d2ed27e69c4754b87a816af2c53585.png)
![](https://img-blog.csdnimg.cn/img_convert/b5195589cae7498ca0ca5655e01072af.png)
![](https://img-blog.csdnimg.cn/img_convert/2e5d68d3ebe741e59701b0a7f3773a7c.png)
![](https://img-blog.csdnimg.cn/img_convert/64c221a2da664b2a9c47db897eb66e5b.png)
2.3访问数组元素
![](https://img-blog.csdnimg.cn/img_convert/d530ab42d6e5473e843bd353fda24f5c.png)
2.3.1整数索引
![](https://img-blog.csdnimg.cn/img_convert/a0fbaa55626c42a48b66cfa4c0f206d5.png)
![](https://img-blog.csdnimg.cn/img_convert/bccb4147ad0f402ea663ce35f4e812b9.png)
2.3.2花式索引
![](https://img-blog.csdnimg.cn/img_convert/881108bdd3544b66b73d715a08897b4c.png)
![](https://img-blog.csdnimg.cn/img_convert/3fadcbfb69fa4a9d9a5d6474e7168e7a.png)
![](https://img-blog.csdnimg.cn/img_convert/b2d622ac913048958eb19c3228ccba7d.png)
![](https://img-blog.csdnimg.cn/img_convert/0eb2bcf4fdad4971b4f1611c1bda45ed.png)
2.3.3布尔索引
![](https://img-blog.csdnimg.cn/img_convert/daa53be2b44f4f6e81e54c59368f7c81.png)
![](https://img-blog.csdnimg.cn/img_convert/95417c0e52ee49aa86f8683aeb8ed2a7.png)
2.35使用切片访问元素
![](https://img-blog.csdnimg.cn/img_convert/d499b1394b5344959098845a0bbb24fa.png)
![](https://img-blog.csdnimg.cn/img_convert/2604fdb413334782bdb96de49838cadc.png)
![](https://img-blog.csdnimg.cn/img_convert/508074e5cd984fee9173b31f00f8f526.png)
![](https://img-blog.csdnimg.cn/img_convert/1092fdef2c3849cc8bd5ab1d65c095ed.png)
2.4数组运算
![](https://img-blog.csdnimg.cn/img_convert/adad20f4a9a24a24b2109346ceb054df.png)
![](https://img-blog.csdnimg.cn/img_convert/43b1a23b2c5d41c786e6c6e233011fc4.png)
2.4.2 形状不同的数组-广播机制 (期末要考)
![](https://img-blog.csdnimg.cn/img_convert/401b80c0ab3146599f38bd8b42cfccda.png)
![](https://img-blog.csdnimg.cn/img_convert/021d2eb13af04426a6e3fff9b7cd6850.png)
![](https://img-blog.csdnimg.cn/img_convert/db6b302ba13a4083a99db51b13f5b817.png)
![](https://img-blog.csdnimg.cn/img_convert/0e3fc73e097d4671902515d1578e0199.png)
![](https://img-blog.csdnimg.cn/img_convert/27b8a280a51043caacacb0ff29a83fa2.png)
![](https://img-blog.csdnimg.cn/img_convert/21a2a7afbf5c442bb8bfa3a9d34f78a5.png)
![](https://img-blog.csdnimg.cn/img_convert/0167d4c3ab394b9a9305cc570db843e1.png)
![](https://img-blog.csdnimg.cn/img_convert/b3cfe4126e254913ad780dc137b09416.png)
2.5数组操作-排序&检索
![](https://img-blog.csdnimg.cn/img_convert/ce20b4d6b7574bef860607f452003d54.png)
![](https://img-blog.csdnimg.cn/img_convert/60a0b827660d460bb706e18175e31ebe.png)
![](https://img-blog.csdnimg.cn/img_convert/5edb769bee774ea9a18ff0b5897a461c.png)
2.6数组的转置
数组的转置指数组中各元素按照一定的规则变换位置。NumPy中提供了三种实现数组转置的方式,分别为T属性、transpose()方法、swapaxes()方法。
![](https://img-blog.csdnimg.cn/img_convert/c709a22c2fb04f63838015e0c2f64583.png)
![](https://img-blog.csdnimg.cn/img_convert/4649f25a0dfd4e06ae0d57f623d5acf5.png)
![](https://img-blog.csdnimg.cn/img_convert/1931a7a9676641b99a8d0fcc8c853c27.png)
第三章pandas库基础
pandas是一个基于NumPy,专门为数据分析而设计的库,该库中不仅提供了大量的库及一些标准的数据模型,还提供了高效操作数据集的数据结构,被广泛地应用到众多领域中。
3.1数据结构
3.1.1 Series
![](https://img-blog.csdnimg.cn/img_convert/7a23192fba0b42cfb156fa7ea18dd0b5.png)
![](https://img-blog.csdnimg.cn/img_convert/d7541780a5f5448096f7ced2d70c122a.png)
![](https://img-blog.csdnimg.cn/img_convert/891dc1b71e754cd8aa38d994fa0f4724.png)
![](https://img-blog.csdnimg.cn/img_convert/4557a124165f4ffb8ae84f16b2f01ed3.png)
![](https://img-blog.csdnimg.cn/img_convert/78e84ec72c1c4e8c94fd93b9f7e0204a.png)
![](https://img-blog.csdnimg.cn/img_convert/bf3288f275a84d09b69d40f450bfccd9.png)
3.1.2 DataFrame
![](https://img-blog.csdnimg.cn/img_convert/6f84fce705f74156b047b9303c58c71a.png)
![](https://img-blog.csdnimg.cn/img_convert/bf7e80e7976a42398b0111dc2486e7cf.png)
![](https://img-blog.csdnimg.cn/img_convert/8f8a9c08f19540d18db27f0ba641f4ae.png)
3.2索引操作
3.2.1 索引对象
![](https://img-blog.csdnimg.cn/img_convert/8c69e1e71bd04b149d013cc845374469.png)
![](https://img-blog.csdnimg.cn/img_convert/bcec69921e214024be694f5b8e09b4e3.png)
![](https://img-blog.csdnimg.cn/img_convert/21960408f24342a086b29313ee6ab5f0.png)
![](https://img-blog.csdnimg.cn/img_convert/8b18b0dc0c5d409db8cac0e4dd7ebb64.png)
3.2.2 使用单层索引访问数据
pandas 中 可 以 使 用 [] 、 loc 、 iloc 、 at 和 iat 这几 种 方 式 访 问 Series 类对 象 和DataFrame类对象的数据。
1. 使用[]访问数据
![](https://img-blog.csdnimg.cn/img_convert/d1b55735005f469baa471b2c2a6f15c6.png)
2. 使用loc和iloc访问数据
![](https://img-blog.csdnimg.cn/img_convert/5d7cfbe8d1904865912ba273bd32248e.png)
3. 使用at和iat访问数据
![](https://img-blog.csdnimg.cn/img_convert/1362178a01f7478b9e8f25f85030f756.png)
3.2.3 使用分层索引访问数据
![](https://img-blog.csdnimg.cn/img_convert/004a721a7ef84988b5360750f0625640.png)
![](https://img-blog.csdnimg.cn/img_convert/4a50232485674f45901a5556f5a3cfe1.png)
![](https://img-blog.csdnimg.cn/img_convert/e7e627df67e341c4b7187f20f64fcdeb.png)
3.2.4 重新索引reindex()
![](https://img-blog.csdnimg.cn/img_convert/fe770d8769e747b4ba9fd575f6657791.png)
![](https://img-blog.csdnimg.cn/img_convert/db970a602ddf4482918e708c9fbeefda.png)
3.3数据排序
3.3.1 按索引排序
![](https://img-blog.csdnimg.cn/img_convert/cfdd97b24dc24da793b961bd8b04a765.png)
3.3.2 按值排序
![](https://img-blog.csdnimg.cn/img_convert/195f6b37933f4c97b7df9d083d5891d5.png)
3.4统计计算与描述
3.4.1 统计计算
![](https://img-blog.csdnimg.cn/img_convert/32291f49a4124f4c8165d6781a12cbde.png)
3.4.2 统计描述
![](https://img-blog.csdnimg.cn/img_convert/d388dc7af7db4c0895865f6cd0ed0753.png)
3.5绘制图表
![](https://img-blog.csdnimg.cn/img_convert/52aa2a68295f4766a9068225c1580b2e.png)
![](https://img-blog.csdnimg.cn/img_convert/0a9058c695b945cc9c138618eb4d2b24.png)
第八章 文件操作
8.1文件的编码
![](https://img-blog.csdnimg.cn/img_convert/76e4ac67d21c4beeb3762fcc464abe72.png)
8.2文件的读取
8.2.0文件的操作步骤
① 打开文件
![](https://img-blog.csdnimg.cn/img_convert/f0e3b024982c431bbef70ca0ccaad196.png)
![](https://img-blog.csdnimg.cn/img_convert/c6ee2181217c4c58b672c8620047be54.png)
② 读写文件
![](https://img-blog.csdnimg.cn/img_convert/3734985ff5314588b2b9710dfa0e3828.png)
![](https://img-blog.csdnimg.cn/img_convert/38e0199e31fb425dbe48b7c72e74fe50.png)
③ 关闭文件
![](https://img-blog.csdnimg.cn/img_convert/cc81c9840a97468e83802a0c1b6c768f.png)
![](https://img-blog.csdnimg.cn/img_convert/bd0dbb059b2a46bba152e12701aeef93.png)
8.3文件的写入
![](https://img-blog.csdnimg.cn/img_convert/885abcf91ea547e1a793614704a7c5a9.png)
8.4文件的追加
![](https://img-blog.csdnimg.cn/img_convert/cd4f7b337342456daf8314a5ff67fa34.png)
8.5文件操作综合案例
第九章-异常、模块与包
了解异常
异常就是程序运行的过程中出现了错误
异常的捕获方法
![](https://img-blog.csdnimg.cn/img_convert/c78133d96077474398155ed610734385.png)
![](https://img-blog.csdnimg.cn/img_convert/02ff02df604c44959b83df5e9edd4260.png)
![](https://img-blog.csdnimg.cn/img_convert/e980e3688f6245eb8b35f60f11ba9abf.png)
![](https://img-blog.csdnimg.cn/img_convert/4054492a2960430e95c2646c8f733b3b.png)
![](https://img-blog.csdnimg.cn/img_convert/cf809d668a544e3b9a20838cf28a8679.png)
![](https://img-blog.csdnimg.cn/img_convert/8d7d1f3ec7d84e5eb3e04b9a32e0bbfc.png)
![](https://img-blog.csdnimg.cn/img_convert/bbb36e95046d4d6088bc0c4605341692.png)
异常的传递
![](https://img-blog.csdnimg.cn/img_convert/e08c55165748491baf84caa3018b37d7.png)
![](https://img-blog.csdnimg.cn/img_convert/879eda45ebd94be88d742c9481eca27e.png)
Python模块
导入模块
![](https://img-blog.csdnimg.cn/img_convert/b4db0796ae324ebb92d61270e85a6bd1.png)
![](https://img-blog.csdnimg.cn/img_convert/cfb3c8c27a754a16bd793ccd664a4746.png)
![](https://img-blog.csdnimg.cn/img_convert/f6e90328a59c46c19f321736b0331967.png)
自定义模块
![](https://img-blog.csdnimg.cn/img_convert/dea173e4edc94e77b4bc346d170c8776.png)
![](https://img-blog.csdnimg.cn/img_convert/48901054cffc4442a6304aa92ba8e02a.png)
注意事项:当导入多个模块的时候,且模块内有同名功能. 当调用这个同名功能的时候,调用到的是后面导入的模块的功能
![](https://img-blog.csdnimg.cn/img_convert/aaffcbfc9ec149b68a301d157b8222d1.png)
Python包
自定义包
![](https://img-blog.csdnimg.cn/img_convert/c37570b95b7148d4adb3e8fccb2ec097.png)
![](https://img-blog.csdnimg.cn/img_convert/7e0afcaa01ed4359a4b04780fe56efb5.png)
![](https://img-blog.csdnimg.cn/img_convert/b8f475aa34734af186214110c40e84ba.png)
![](https://img-blog.csdnimg.cn/img_convert/f788f51145324d79afcc3a5f1700dbdb.png)
安装第三方Python包
![](https://img-blog.csdnimg.cn/img_convert/32272ed41dc94a85bf0f66f561580bf1.png)
![](https://img-blog.csdnimg.cn/img_convert/23e5c707973140379731f45e71a9ce78.png)
![](https://img-blog.csdnimg.cn/img_convert/3fb488b98a744265be06a4a8132571d3.png)
https://pypi.tuna.tsinghua.edu.cn/simple 是清华大学提供的一个网站,可供pip程序下载第三方包
![](https://img-blog.csdnimg.cn/img_convert/96930a03cded4acca9292fdfe3de5437.png)
本文来自博客园,作者:软工菜鸡,转载请注明原文链接:https://www.cnblogs.com/SElearner/p/17676667.html