NumPy 和 Pandas 数据分析实用指南 (初译) (it-ebooks) (Z-Library)
Data
No Description
250
Views
0
Downloads
0.00
Total Donations
Registered users can read the full content for free
Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.
Page
1
(This page has no text content)
Page
2
1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 Table of Contents NumPy 和 Pandas 数据分析实⽤指南 零、前⾔ ⼀、配置 Python 数据分析环境 ⼆、探索 NumPy 三、NumPy 数组上的运算 四、Pandas 很有趣! 什么是 Pandas? 五、Pandas 的算术,函数应⽤以及映射 六、排序,索引和绘图
Page
3
NumPy 和 Pandas 数据分析实⽤指 南 原⽂:Hands-On Data Analysis with NumPy and pandas 协议:CC BY-NC-SA 4.0 欢迎任何⼈参与和完善:⼀个⼈可以⾛的很快,但是⼀群⼈却可 以⾛的更远。 在线阅读 ApacheCN ⾯试求职交流群 724187166 ApacheCN 学习资源
Page
4
贡献指南 本项⽬需要校对,欢迎⼤家提交 Pull Request。 请您勇敢地去翻译和改进翻译。虽然我们追求卓越,但我们并不 要求您做到⼗全⼗美,因此请不要担⼼因为翻译上犯错——在⼤ 部分情况下,我们的服务器已经记录所有的翻译,因此您不必担 ⼼会因为您的失误遭到⽆法挽回的破坏。(改编⾃维基百科)
Page
5
联系⽅式 负责⼈ ⻜⻰: 562826179 其他 在我们的 apachecn/apachecn-ds-zh github 上提 issue. 发邮件到 Email: apachecn@163.com . 在我们的 组织学习交流群 中联系群主/管理员即可.
Page
6
赞助我们
Page
7
零、前⾔ Python 是⼀种多范式编程语⾔,已成为数据科学家进⾏数据分析,可 视化和机器学习的⾸选语⾔。 您将⾸先学习如何为 Python 建⽴正确的数据分析环境。 在这⾥,您 将学习安装正确的 Python 发⾏版,以及使⽤ Jupyter 笔记本和建⽴数 据库。 之后,您将深⼊研究 Python 的 NumPy 包 -- Python 的强⼤扩展 以及⾼级数学函数。 您将学习如何创建 NumPy 数组,以及如何使⽤ 不同的数组⽅法和函数。 然后,您将探索 Python 的 pandas 扩展,在 那⾥您将学习数据的⼦集,并深⼊研究使⽤ pandas 的数据映射。 您还 将学习通过对数据集进⾏排序和排序来管理它们。 到本书结尾,您将学习对数据进⾏索引和分组以进⾏复杂的数据分析 和处理。
Page
8
这本书是给谁的 如果您是 Python 开发⼈员,并且想迈出第⼀步进⼊数据分析领域,那 么这就是您⼀直在等待的书!
Page
9
充分利⽤这本书 Python 3.4.x 或更⾼版本。 在 Debian 及其衍⽣产品(Ubuntu)上: python,python-dev 或 python3-dev。 在 Windows 上: www.python.org 上的官⽅ python 安装程序就⾜够了: NumPy Pandas
Page
10
使⽤约定 本书中使⽤了许多⽂本约定。 CodeInText :表⽰⽂本中的词,数据库表名称,⽂件夹名称,⽂件 名,⽂件扩展名,路径名,伪 URL,⽤户输⼊和 Twitter 句柄。 这是 ⼀个⽰例:“然后⽤这个符号与 arr1 相乘”。 任何命令⾏输⼊或输出的编写⽅式如下: conda install selenium 粗体:表⽰您在屏幕上看到的新术语,重要单词或顺序。 例如,菜单 或对话框中的单词会出现在这样的⽂本中。 这是⼀个⽰例:“在此添 加单元,然后单击再次运⾏单元”。 警告或重要提⽰如下所⽰。 提⽰和技巧如下所⽰。
Page
11
⼀、配置 Python 数据分析环境 在本章中,我们将介绍以下主题: 安装 Anaconda 探索 Jupyter 笔记本 探索 Jupyter 的替代品 管理 Anaconda 包 配置数据库 在本章中,我们将讨论如何安装和管理 Anaconda。 Anaconda 是⼀个 包,我们将在本书的以下各章中使⽤。
Page
12
什么是 Anaconda? 在本节中,我们将讨论什么是 Anaconda 以及为什么使⽤它。 我们将 提供⼀个链接,以显⽰从其赞助商 Continuum Analytics 的⽹站下载 Anaconda 的位置,并讨论如何安装 Anaconda。 Anaconda 是 Python 和 R 编程语⾔的开源发⾏版。 在本书中,我们将专注于 Anaconda 专门⽤于 Python 的部分。 Anaconda 帮助我们将这些语⾔⽤于数据分析应⽤,包括⼤规模数据处 理,预测分析以及科学和统计计算。 Continuum Analytics 为 Anaconda 提供企业⽀持,包括可帮助团队协作并提⾼其系统性能的版本,并提 供⼀种部署使⽤ Anaconda 开发的模型的⽅法。 因此,Anaconda 出现 在企业环境中,有抱负的分析师应该熟悉它的⽤法。 Anaconda 附带 了本书中使⽤的许多包,包括 Jupyter,NumPy,pandas 以及其他许多 数据分析中常⽤的包。 仅此⼀项就可以解释其受欢迎程度。 Anaconda 的安装包括现成的数据分析所需的⼤部分内容。 Conda 包管 理器还可⽤于下载和安装新包。 为什么要使⽤ Anaconda? Anaconda 专门为数据分析打包了 Python。 Anaconda 安装中包含了您项⽬中最重要的包。 除了 Anaconda 提供的 ⼀些性能提升,和 Continuum Analytics 对该包的企业⽀持之外,对于 它的流⾏也不应感到惊讶。
Page
13
安装 Anaconda 您可以从 Continuum Analytics ⽹站免费下载 Anaconda。 下载主⻚⾯ 在这⾥; 否则,很容易找到。 确保选择适合您系统的安装程序。 显 然,选择适合您的操作系统的安装程序,但也要注意 Anaconda 具有 32 位和 64 位版本。 64 位版本为 64 位系统提供最佳性能。 Python 社区正处于从 Python 2.7 到 Python 3.6 的缓慢过渡中,这不是 完全向后兼容的。 如果您需要使⽤ Python 2.7,可能是由于遗留代码 或尚未更新为与 Python 3.6 兼容的包,请选择 Anaconda 的 Python 2.7 版本。 否则,我们将使⽤ Python 3.6。 以下屏幕截图来⾃ Anaconda ⽹站,分析⼈员可从该⽹站下载 Anaconda:
Page
14
Anaconda website 如您所⻅,我们可以选择适⽤于操作系统(包括 Windows,macOS 和 Linux),处理器和 Python 版本的 Anaconda 安装。 导航到正确的操作 系统和处理器,然后在 Python 2.7 和 Python 3.6 之间进⾏选择。 在这⾥,我们将使⽤ Python 3.6。 在 Windows 和 macOS 上进⾏安装 最终等同于使⽤安装向导,该安装向导通常会为您的系统选择最佳选 项,尽管它确实允许某些选项根据您的⾸选项⽽有所不同。
Page
15
Linux 安装必须通过命令⾏完成,但是对于那些熟悉 Linux 安装的⼈来 说,它应该不会太复杂。 最终,这相当于运⾏ Bash 脚本。 在本书 中,我们将使⽤ Windows。
Page
16
探索 Jupyter 笔记本 在本节中,我们将探索 Jupyter 笔记本,这是我们将使⽤ Python 进⾏ 数据分析的主要⼯具。 我们将看到什么是 Jupyter 笔记本,还将讨论 Markdown,这是我们在 Jupyter 笔记本中⽤于创建格式化⽂本的⼯ 具。 在 Jupyter 笔记本中,有两种类型的块。 有⼀些可执⾏的 Python 代码块,然后是带格式的,⼈类可读的⽂本块。 ⽤户执⾏ Python 代码块,然后将结果直接插⼊⽂档中。 除⾮以同样 的⽅式运⾏,否则代码块可以以任何顺序重新运⾏,⽽不必影响以后 的块。 由于 Jupyter 笔记本基于 IPython,因此有⼀些附加功能,例如 魔术命令。 Anaconda 随附 Jupyter 笔记本。 Jupyter 笔记本允许纯⽂本与代码混 合。 可以使⽤称为 Markdown 的语⾔格式化纯⽂本。 它以纯⽂本格 式完成。 我们也可以插⼊段落。 以下⽰例是您在 Markdown 中看到的 ⼀些常⻅语法:
Page
17
以下屏幕截图显⽰了 Jupyter 笔记本: 如您所⻅,它⽤尽了⽹络浏览器,例如 Chrome 或 Firefox,在这种情 况下为 Chrome。 当我们开始 Jupyter 笔记本时,我们在⽂件浏览器 中。 我们在⼀个新创建的⽬录 Untitled Folder 中。 在 Jupyter 笔记 本中,有⽤于创建新笔记本,⽂本⽂件和⽂件夹的选项。 如前⾯的屏 幕截图所⽰,当前没有保存笔记本。 我们将需要⼀个 Python 笔记 本,可以通过在以下屏幕快照中显⽰的“新建”下拉菜单中选择 Python 选项来创建它。 笔记本启动后,我们从⼀个代码块开始。 我们可以将此代码块更改为 Markdown 块,现在可以开始输⼊⽂本了。 例如,我们可以输⼊标题。 我们还可以输⼊纯⽂本以及粗体和斜体, 如下⾯的屏幕快照所⽰:
Page
18
如您所⻅,在渲染结束时会有⼀些提⽰,但是实际上我们可以通过单 击运⾏单元按钮来查看渲染。 如果要更改此设置,可以双击同⼀单元 格。 现在我们回到纯⽂本编辑。 在这⾥我们添加单型,然后再次单击 运⾏单元,如下所⽰: 在按下 Enter 时,随后将⽴即创建⼀个新单元格。 该单元格是⼀个 Python 单元格,我们可以在其中输⼊ Python 代码。 例如,我们可以 创建⼀个变量。 我们多次打印 Hello, world! ,如以下屏幕截图所 ⽰:
Page
19
要查看执⾏单元时会发⽣什么,我们只需单击运⾏单元; 同样,当我 们按 Enter 时,将创建⼀个新的单元块。 让我们将此单元格块标记为 Markdown 块。 如果要插⼊其他单元格,可以按下⾯的插⼊单元格。 在第⼀个单元格中,我们将输⼊⼀些代码,在第⼆个单元格中,我们 可以输⼊依赖于第⼀个单元格中的代码的代码。 注意当我们尝试在第 ⼀个单元格中执⾏代码之前在第⼆个单元格中执⾏代码时会发⽣什 么。 将产⽣⼀个错误,如下所⽰:
Page
20
投诉变量 trigger 尚未定义。 为了使第⼆个单元正常⼯作,我们需要 运⾏第⼀个单元。 然后,当我们运⾏第⼆个单元格时,我们将获得预 期的输出。 现在假设我们要更改此单元格中的代码。 ⽐⽅说,我们 有 trigger = True ⽽不是 trigger = False 。 第⼆个单元将不知道该 更改。 如果再次运⾏此单元格,则会得到相同的输出。 因此,我们将 需要⾸先运⾏此单元格,从⽽影响更改。 然后我们可以运⾏第⼆个单 元并获得预期的输出。 后台发⽣了什么? 发⽣的事情是有⼀个内核,它基本上是⼀个正在运 ⾏的 Python 会话,它跟踪我们所有的变量以及到⽬前为⽌发⽣的所有 事情。 如果单击内核,则可以看到重新启动内核的选项。 这将基本上 重新启动我们的 Python 会话。 我们最初警告说,通过重新启动内 核,所有变量都将丢失。
The above is a preview of the first 20 pages. Register to read the complete e-book.
AI Reading Assistant
Whole-book reading guide from stratified index samples; jump to passages in the text
AI guide
【One-Line Pitch】
A hands-on, beginner-friendly guide for Python developers stepping into data analysis, covering environment setup, NumPy fundamentals, and Pandas essentials with practical, code-driven examples.
【Book Arc】
- **Opening (~0%–17%)**: Starts with setting up a Python data analysis environment—installing Anaconda, exploring Jupyter Notebook and its alternatives (like Spyder and ptpython), managing packages, and configuring MySQL databases. This stage solves the "how do I get started" problem by establishing a working toolkit.
- **Early (~17%–33%)**: Introduces NumPy core concepts: creating arrays, understanding `dtype`, special values like `nan` and `inf`, and the critical distinction between array views and copies. This builds the foundational data structures needed for analysis.
- **Early (~33%–50%)**: Dives into array manipulation—slicing with colons, indexing with arrays, concatenation, and broadcasting. It also covers arithmetic operations and introduces linear algebra support, showing how NumPy handles multi-dimensional data efficiently.
- **Middle (~50%–67%)**: Transitions to Pandas, explaining what it is and why it's popular. It covers the two key objects—Series and DataFrames—and how to create, subset, and save them, including handling index alignment and missing data.
- **Late (~67%–83%)**: Explores Pandas operations: arithmetic on Series/DataFrames, function application with `apply` and `applymap`, handling missing data (detection, filling, and advanced techniques like bootstrapping), and sorting by index or values.
- **Late (~83%–100%)**: Covers advanced indexing with `MultiIndex`, including creation and slicing, and ends with visualization—using the `plot` method for line charts, histograms, bar charts, and scatter plot matrices, integrating with Matplotlib.
【Key Takeaways】
- **Environment setup is the first hurdle** (Opening): Anaconda simplifies installing Python, Jupyter, and key libraries; mastering Jupyter's magic commands like `timeit` helps you test code efficiency early on.
- **NumPy arrays are the backbone of data analysis** (Early): Understanding `dtype`, shapes, and special values (`nan`, `inf`) is essential—`nan` never equals itself, so use `isnan` to detect missing or undefined data.
- **Views vs. copies can trip you up** (Early): Assigning an array to a new variable creates a pointer to the same data, not a copy; use the `copy` method to create independent arrays and avoid unintended side effects.
- **Slicing and broadcasting unlock array power** (Early): Colon slicing works across dimensions, and broadcasting lets you perform arithmetic on arrays of different shapes—smaller arrays are "stretched" to match larger ones, enabling efficient operations.
- **Pandas Series and DataFrames bring structure to data** (Middle): These objects handle index alignment automatically, filling mismatches with `NaN`; creating DataFrames from dictionaries with indexed Series is a robust way to manage heterogeneous data.
- **Function application is key for transformation** (Late): Use `apply` for column/row-wise operations and `applymap` for element-wise ones; vectorized functions via `numpy.vectorize` work but are slower than using built-in Pandas methods.
- **Missing data handling is a core skill** (Late): Detect with `isnan`, ignore with `skipna` parameters, and fill using means or bootstrapping—random resampling preserves mean and standard deviation better than simple imputation.
- **Sorting and MultiIndex enable complex queries** (Late): `sort_index` and `sort_values` handle ordering, while `MultiIndex` allows hierarchical indexing; slicing with tuples and `loc` gives precise control over multi-level data.
【Reading Tips】
- **Skim the environment setup** (Opening): If you already have Anaconda and Jupyter installed, skip ahead—but do review the MySQL configuration if you plan to work with SQL databases.
- **Deep-read the NumPy sections** (Early–Middle): Focus on array creation, slicing, broadcasting, and the view-vs-copy distinction; these are the most error-prone areas for beginners. Practice with the code examples to build muscle memory.
- **Pay attention to Pandas index alignment** (Middle–Late): This is a common source of confusion—experiment with mismatched indices to see how `NaN` appears, and learn when to use `loc` vs. `iloc`.
- **Use the visualization chapter as a reference** (Ending): Don't memorize every plot type; instead, understand how to call `plot` with different `kind` parameters and tweak Matplotlib options when you need specific charts.
- **Watch for OCR artifacts**: The text has some garbled characters (e.g., "L 连接器" for "MySQL connector"); rely on the code logic and context rather than exact wording.
【Coverage Limits】
The excerpts do not cover advanced topics like time series analysis, performance optimization, or real-world case studies; the guide focuses on foundational setup, NumPy, and Pandas basics.
Passage locations
Excerpt 1
书名: NumPy 和 Pandas 数据分析实用指南 (初译) (it-ebooks) (Z-Library) 作者: it-ebooks 贡献指南 本项⽬需要校对,欢迎⼤家提交 Pull Request。 请您勇敢地去翻译和改进翻译。虽然我们追求卓越,但我们并不 要求您做到⼗全⼗美,因此请不要担⼼因为翻译上犯...
View in text
Excerpt 2
L 连接器 创建,使⽤和删除数据库 为了使 MySQL 和 Python ⼀起使⽤,MySQL 连接器是必需的。 存在 许多 SQL 数据库实现,尽管 MySQL 可能不是最简单的数据库管理系 统,但它功能⻬全,具有⼯业实⼒,在现实世界中很常⻅,⽽且它是 免费和开源的,这意味着它是⼀个很好的学习⼯具。 您可以从...
View in text
Excerpt 3
相同数据的新数组,则将需要使⽤ copy ⽅法,我们将看 到。 结果的顶部将被视为⼀个平板,⽽底部将被视为另⼀平板。 现在,让我们创建⼀个填充有随机数据的矩阵。 在这种情况下,我们 将使⽤ randn 函数创建⼀个具有三⾏三列的⽅阵,该函数是 NumPy 随机模块的⼀部分: 我们传递的第⼀个数字是⾏数,第⼆个...
View in text
Excerpt 4
Pandas 并研究了它的作⽤。 我们探索了 Pandas 序列数据帧并创建了它们。 我们还研究了如何将数据添加到序 列和数据帧中。 最后,我们介绍了保存数据帧。 在下⼀章中,我们将 讨论算术,函数应⽤和函数映射。 产⽣了两个 NaN。 这是因为,对于元素 0 到 3,两个序列中都有可以 匹配的元素,但是对于 4...
View in text
Recommended for You
{{#thumbnailUrl}}
{{/thumbnailUrl}}
{{^thumbnailUrl}}
{{/thumbnailUrl}}
Loading recommended books...
Failed to load, please try again later
Tip the Site
Scan the WeChat Pay or Alipay code to tip. No login required.
WeChat Pay
Alipay