Python函数中apply、map、applymap的区别
作者:小小程序员ol 时间:2022-08-11 04:20:36
一、总结
apply
—— 应用在dataFrame
上,用于对row
或者column
进行计算applymap
—— 应用在dataFrame
上,元素级别的操作map
——python
系统自带函数,应用在series
上, 元素级别的操作
二、实操对比
构建测试数据框:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randint(0, 10, (4, 3)),
columns=list('abc'),
index=range(4))
df
'''
abc
0548
1752
2122
3162
'''
apply
作用在 dataframe
上的一行或者一列上
#Python学习交流群:531509025
# 默认按列操作 axis=0
# 求每列的最大值、最小值之差
df.apply(lambda x: x.max() - x.min()) # axis=0
# 求每行的最大值、最小值之差
df.apply(lambda x: x.max() - x.min(), axis=1)
applymap
作用在 dataframe
的每一个元素上
# 偶数放大10倍
df.applymap(lambda x: x*10 if x%2 == 0 else x)
map
函数作用在 series
上的每一个元素
# 单独的序列
df['b'].map(lambda x: 1 if x%2 == 0 else 0)
总的来说,要对数据进行应用函数操作时,考虑数据结构是 DataFrame
还是 Series
,再考虑是要按行执行还是按列执行,进行函数的选择。
来源:https://www.cnblogs.com/python960410445/p/15463099.html
标签:Python,函数,apply,map,applymap
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python 爬虫 实现增量去重和定时爬取实例
2022-01-26 01:01:37
Python编译为二进制so可执行文件实例
2023-03-05 06:30:36
Python基于百度AI实现OCR文字识别
2023-09-26 22:14:29
![](https://img.aspxhome.com/file/2023/3/90503_0s.png)
用asp判断某IP是否属于某网段的另类算法
2007-09-28 12:33:00
Python numpy大矩阵运算内存不足如何解决
2022-08-06 22:38:21
![](https://img.aspxhome.com/file/2023/9/92509_0s.png)
彻底理解Python list切片原理
2023-10-19 03:34:43
关于Python 多重继承时metaclass conflict问题解决与原理探究
2022-04-17 04:50:10
Access下如何使用通用对话框
2008-11-20 16:41:00
ASP中如何判断一个字符是不是汉字
2008-05-04 12:47:00
Python3实现自定义比较排序/运算符
2023-04-15 23:32:25
python类继承用法实例分析
2022-01-25 16:44:13
WEB手绘稿常用元素 之 箭头
2009-05-08 12:33:00
Python如何获取多线程返回结果
2024-01-01 23:34:28
Django实现简单的分页功能
2021-08-08 20:53:14
![](https://img.aspxhome.com/file/2023/9/72109_0s.jpg)
Django后台admin的使用详解
2023-11-21 14:57:50
![](https://img.aspxhome.com/file/2023/2/83942_0s.png)
Django 拆分model和view的实现方法
2022-03-28 02:35:34
![](https://img.aspxhome.com/file/2023/5/80595_0s.jpg)
Qzoneing主题视觉设计分享
2009-07-21 18:12:00
![](https://img.aspxhome.com/file/UploadPic/20097/21/1240_top-99s.jpg)
如何在2003系统注册fso组件
2010-11-29 19:55:00
python使用SMTP发送qq或sina邮件
2021-03-08 01:06:55
![](https://img.aspxhome.com/file/2023/4/78454_0s.png)
CSS 超链接图标规范 V1.0
2007-12-28 12:05:00
![](https://img.aspxhome.com/file/UploadPic/200712/28/2007122813031671s.gif)