Pandas中map,applymap和apply方法之间的区别

编程入门 行业动态 更新时间:2024-10-20 11:24:33
本文介绍了Pandas中map,applymap和apply方法之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您能否通过基本示例告诉我何时使用这些矢量化方法?

Can you tell me when to use these vectorization methods with basic examples?

我看到map是Series方法,而其余的是DataFrame方法.我对apply和applymap方法感到困惑.为什么我们有两种将函数应用于DataFrame的方法?同样,简单的例子可以很好地说明用法!

I see that map is a Series method whereas the rest are DataFrame methods. I got confused about apply and applymap methods though. Why do we have two methods for applying a function to a DataFrame? Again, simple examples which illustrate the usage would be great!

推荐答案

Wes McKinney的 Python for数据分析书,第pg. 132(我强烈推荐这本书):

Straight from Wes McKinney's Python for Data Analysis book, pg. 132 (I highly recommended this book):

另一种常见的操作是将一维数组上的函数应用于每一列或每一行. DataFrame的apply方法可以做到这一点:

Another frequent operation is applying a function on 1D arrays to each column or row. DataFrame’s apply method does exactly this:

In [116]: frame = DataFrame(np.random.randn(4, 3), columns=list('bde'), index=['Utah', 'Ohio', 'Texas', 'Oregon']) In [117]: frame Out[117]: b d e Utah -0.029638 1.081563 1.280300 Ohio 0.647747 0.831136 -1.549481 Texas 0.513416 -0.884417 0.195343 Oregon -0.485454 -0.477388 -0.309548 In [118]: f = lambda x: x.max() - x.min() In [119]: frame.apply(f) Out[119]: b 1.133201 d 1.965980 e 2.829781 dtype: float64

许多最常见的数组统计信息(例如sum和mean)是DataFrame方法, 因此不必使用apply.

Many of the most common array statistics (like sum and mean) are DataFrame methods, so using apply is not necessary.

也可以使用基于元素的Python函数.假设您要根据帧中的每个浮点值来计算格式化的字符串.您可以使用applymap做到这一点:

Element-wise Python functions can be used, too. Suppose you wanted to compute a formatted string from each floating point value in frame. You can do this with applymap:

In [120]: format = lambda x: '%.2f' % x In [121]: frame.applymap(format) Out[121]: b d e Utah -0.03 1.08 1.28 Ohio 0.65 0.83 -1.55 Texas 0.51 -0.88 0.20 Oregon -0.49 -0.48 -0.31

之所以使用applymap之所以命名,是因为Series具有用于应用逐元素函数的map方法:

The reason for the name applymap is that Series has a map method for applying an element-wise function:

In [122]: frame['e'].map(format) Out[122]: Utah 1.28 Ohio -1.55 Texas 0.20 Oregon -0.31 Name: e, dtype: object

总结起来,apply在DataFrame的行/列基础上工作,applymap在DataFrame的元素基础上工作,而map在Series的元素基础上工作.

Summing up, apply works on a row / column basis of a DataFrame, applymap works element-wise on a DataFrame, and map works element-wise on a Series.

更多推荐

Pandas中map,applymap和apply方法之间的区别

本文发布于:2023-11-26 00:23:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631922.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:区别   方法   map   Pandas   apply

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!