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 版
322
|
10
如果没有安装 statsmodels,则可以使用
conda
进行安装:
现在,为了计算 AAPL SPX 收益率的年化线性回归,执行如下代码:
10.4
分组转换和“展开式”
GroupBy
运算
10.3
节中,我们学习了用于分组运算和转换的apply 方法。还有另一个内置的
transform 方法,它类似于 apply,但对能使用的函数有更多限制:
·
生成标量值,能传播到分组形状。
·
生成与输入分组具有相同形状的对象。
·
无法修改输入。
来看一个简单的示例:
数据聚合与分组操作
|
323
按照键计算分组平均值:
假设我们想创建一个
Series
,它的形状与 df['value'] 相同,但值替换为按照 'key'
分组平均值。我们可以向 transform 传入一个计算单个分组平均值的函数:
对于内置的聚合函数,我们可以像
GroupBy
agg 方法那样,传入函数的字符串别名:
类似于 applytransform 可以使用能返回
Series
的函数,但结果必须与输入具有相同
324
|
10
的大小。例如,我们可以使用一个辅助函数将各分组乘以
2
再复杂一点,我们可以计算各个分组的降序排名:
再考虑一个由简单聚合方法构成的分组转换函数:
对于这种情况,使用 transform ...
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