Skip to Content
利用 Python 进行数据分析:原书第 3 版
book

利用 Python 进行数据分析:原书第 3 版

by Wes McKinney
November 2023
Intermediate to advanced
512 pages
11h 53m
Chinese
China Machine Press
Content preview from 利用 Python 进行数据分析:原书第 3 版
234
8
数据规整:连接、联合和重塑
在许多应用中,数据可能分散在多个文件或数据库中,或者排列的形式不利于分析。本
章关注的是连接、联合、重塑数据的方法。
首先,我会介绍
pandas
的层次化索引,它广泛用于以上操作。然后,我将深入介绍一些
特殊的数据操作。在第
13
章,读者可以看到这些工具的具体运用。
8.1
层次化索引
层次化索引是
pandas
的一项重要功能,它使你能在一个轴上拥有多个(两个以上)索引
层级。用另一种说法,它使你能以低维度形式处理高维度数据。我们先来看一个简单的
例子:创建一个
Series
,并用一个由列表(或数组)构成的列表作为索引:
看到的结果是以 MultiIndex 作为索引的经过美化的
Series
视图。索引之间的“间隔”
表示“直接使用上面的标签”:
数据规整:连接、联合和重塑
|
235
对于层次化索引对象,可以使用部分索引,使用它选取数据子集更为简单:
在“内部”层级中进行选取也是可行的。这里我选取第二层索引值为
2
的数据:
层次化索引在数据重塑和基于分组的操作(如生成透视表)中发挥着关键的作用。例如,
可以通过 unstack 方法将下面的数据重排到
DataFrame
中:
236
|
8
unstack 的逆运算是 stack
stack unstack 将在
8.3
节中详细讲解。 ...
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

Effective Python 第3版 ―Pythonプログラムを改良する125項目

Effective Python 第3版 ―Pythonプログラムを改良する125項目

Brett Slatkin, 鈴木 駿

Publisher Resources

ISBN: 9787111726722