Skip to Content
Python for Excel (Chinese Edition), 2nd Edition
book

Python for Excel (Chinese Edition), 2nd Edition

by Felix Zumstein
May 2026
Intermediate
418 pages
5h 50m
Chinese
O'Reilly Media, Inc.
Content preview from Python for Excel (Chinese Edition), 2nd Edition

第 4 章. NumPy 基础

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

正如你在第 1 章中所了解的,NumPy 是 Python 科学计算的核心包,支持基于数组的计算和线性代数。由于 NumPy 是 pandas 的关键依赖项,本章将介绍其基础知识:在解释什么是 NumPy 数组之后,我们将探讨向量化和广播这两个重要概念,它们能让你编写简洁的数学代码,并且在 pandas 中也会再次用到。 随后,我们将探讨 NumPy 为何提供被称为通用函数的特殊函数,最后通过学习如何获取和设置数组的值,并解释数组视图与副本之间的区别,来结束本章。即使本书中我们几乎不会直接使用 NumPy,了解其基础知识也将有助于你在下一章更轻松地学习 pandas。

NumPy入门

在本节中,您将了解一维和二维 NumPy 数组,以及“向量化”“广播”和“通用函数”这些技术术语背后的原理。

NumPy 数组

要使用对嵌套列表进行基于数组的计算,你必须编写某种循环。例如,你可以使用嵌套列表推导式向matrix中的每个元素添加一个数字:

In [1]: matrix = [[1, 2, 3],
                  [4, 5, 6],
                  [7, 8, 9]]
In [2]: [[element + 1 for element in row] for row in matrix]
Out[2]: [[2, 3, 4], [5, 6, 7], [8, 9, 10]]

这种写法可读性不高,更重要的是,对于大型数组,遍历每个元素的速度很慢。根据具体用例和数组大小,使用 NumPy 数组代替 Python 列表,可以使计算速度提高数倍至约百倍。 NumPy 通过使用 C 和 Fortran 编写的代码来实现这一性能——这些是编译型编程语言,比 Python 快得多。NumPy 数组是一个 N 维数组 ,用于处理同质数据同质意味着数组中的所有元素必须具有相同的数据类型。最常见的情况是处理如图 4-1 所示的单维和二维浮点数组。

Diagram illustrating one-dimensional and two-dimensional NumPy arrays with labeled axes and indexes.
图 4-1. 一维和二维 NumPy 数组

让我们创建一个一维和二维数组,以便在本章中进行操作:

In [3]: # Import NumPy with the "np" alias
        import numpy as np
In [4]: # Constructing an array with a simple list results in a 1d array
        array1 = np.array([10, 100, 1000.0])
        array1
Out[4]: array([  10.,  100., 1000.])
In [5]: # Constructing an array with a nested list results in a 2d array
        array2 = np.array([[1.0, 2.0, 3.0],
                           [4.0, 5.0, 6.0]])
        array2
Out[5]: array([[1., 2., 3.],
               [4., 5., 6.]])

即使array1包含两个整数(10 和 100)和一个浮点数(1,000),NumPy 数组的同质性也会强制将数组的数据类型设为float64 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

学习勒索软件响应和恢复 (Chinese Edition)

学习勒索软件响应和恢复 (Chinese Edition)

W. Curtis Preston, Michael Saylor
Prometheus:快速入门,第二版

Prometheus:快速入门,第二版

Julien Pivotto, Brian Brazil

Publisher Resources

ISBN: 0642572396008