python中的apply(),applymap(),map() 的用法和区别
平时在处理df series格式的时候并没有注意 map和apply的差异
总感觉没啥却别。不过还是有区别的。下面总结一下:
import pandas as pd
df1= pd.DataFrame({
"sales1":[-,,],
"sales2":[,-,],
})
1.apply
1、当我们要对数据框(DataFrame)的数据进行按行或按列操作时用apply()
note:操作的原子是行和列 ,可以用行列统计描述符 min max mean ......
当axis=0的时候是对“列”进行操作
df2=df1.apply(lambda x: x.max()-x.min(),axis=0)
print(type(df2),"\n",df2)
axis=1的时候是对“行”进行操作
df3=df1.apply(lambda x: x.max()-x.min(),axis=1)
print(type(df3),"\n",df3)
2.也可以直接选定一列series,或者df直接操作
2.applymap
1.applymap函数之后,自动对DataFrame每一个元素进行处理,判断之后输出结果
df4=df1.applymap(lambda x: x>0)
print(type(df4),"\n",df4)
2.applymap是对 DataFrame 进行每个元素的单独操作
ie:不能添加列统计函数,因为是只针对单个元素的操作
df5=df1.applymap(lambda x: x.min())
print(type(df5),"\n",df5)
3.'Series' object has no attribute 'applymap'
df4=df1["sales1"].applymap(lambda x: x>0)
print(type(df4),"\n",df4)
3.map
1.'DataFrame' object has no attribute 'map'
df4=df1.map(lambda x: x**2)
print(type(df4),"\n",df4)
2.map其实是对 列,series 等 进行每个元素的单独操作
ie:不能添加列统计函数,因为是只针对单个元素的操作
df3=df1["sales1"].map(lambda x: x.max()-x.min())
print(type(df3),"\n",df3)
3.正常
df4=df1["sales1"].map(lambda x: x**2)
print(type(df4),"\n",df4)
python中的apply(),applymap(),map() 的用法和区别的更多相关文章
- python中的filter、map、reduce、apply用法
1. filter 功能: filter的功能是过滤掉序列中不符合函数条件的元素,当序列中要删减的元素可以用某些函数描述时,就应该想起filter函数. 调用: filter(function,seq ...
- apply(), applymap(), map()
Pandas 中map, applymap and apply的区别 https://blog.csdn.net/u010814042/article/details/76401133/ Panda ...
- python中urllib, urllib2,urllib3, httplib,httplib2, request的区别
permike原文python中urllib, urllib2,urllib3, httplib,httplib2, request的区别 若只使用python3.X, 下面可以不看了, 记住有个ur ...
- Java中集合List,Map和Set的区别
Java中集合List,Map和Set的区别 1.List和Set的父接口是Collection,而Map不是 2.List中的元素是有序的,可以重复的 3.Map是Key-Value映射关系,且Ke ...
- python中生成器对象和return 还有循环的区别
python中生成器对象和return 还有循环的区别 在python中存在这么一个关键字yield,这个关键字在项目中经常被用到,比如我写一个函数不想它只返回一次就结束那我们就不能用return,因 ...
- Python中%r和%s的详解及区别_python_脚本之家
Python中%r和%s的详解及区别_python_脚本之家 https://www.jb51.net/article/108589.htm
- python中os.path.abspath与os.path.realpath 区别
python中os.path.abspath与os.path.realpath 区别cd /homemkdir amkdir btouch a/1.txtln -s /home/a/1.txt /ho ...
- 脚本引用中的defer和async的用法和区别
之前的博客漫谈前端优化中的引用资源优化曾经提到过脚本引用异步设置defer.async,没有细说,这里展开一下,谈谈它们的作用和区别,先上张图来个针对没用过的小伙伴有个初始印象: 是的,就是在页面脚本 ...
- 浅谈JS中的!=、== 、!==、===的用法和区别 JS中Null与Undefined的区别 读取XML文件 获取路径的方式 C#中Cookie,Session,Application的用法与区别? c#反射 抽象工厂
浅谈JS中的!=.== .!==.===的用法和区别 var num = 1; var str = '1'; var test = 1; test == num //tr ...
随机推荐
- speedtest-cli 命令
speedtest-cli是一个使用python编写的命令行脚本,通过调用speedtest.net测试上下行的接口来完成速度测试,项目地址:https://github.com/sivel/spee ...
- B-tree 和 B+tree过程
https://blog.csdn.net/baiyan3212/article/details/91043695 https://www.jianshu.com/p/0371c9569736
- 19.通过MAPREDUCE 把收集数据进行清洗
在eclipse软件里创建一个maven项目 jdk要换成本地安装的1.8版本的 加载pom.xml文件 <project xmlns="http://maven.apache.org ...
- mysql函数使用报错
This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its de 错误解决办法 解决办法也有两种,第一种是在创建 ...
- python列表与字符串、元组的区别以及列表引用的方式
一.字符串 字符串也可以用下标取值.切片.for循环.len()取长度以及 in 和 not in 来进行操作. 但字符串是不可变的,不能被更改.只能构造一个“新的”字符串来存取你想要修改后的数据. ...
- mysql5.6 Centos6.6安装
1.检查防火墙 是否关闭service iptables status service iptables stopchkconfig iptables off 2. SELINUXvim /etc/s ...
- 区间DP(入门)括号匹配
https://www.nitacm.com/problem_show.php?pid=8314 思路:类似于https://blog.csdn.net/MIKASA3/article/details ...
- 简单Kibana命令
1 查看健康状态 GET _cat/health?v epoch timestamp cluster status node.total node.data shards 1531290005 14: ...
- java 异常捕捉 ( try catch finally ) 你真的掌握了吗?
掌握下面几条原则就可以完全解决“当try.catch.finally遭遇return”的问题. 原则:1.finally语句块中的代码是一定会执行的,而catch块中的代码只有发生异常时才会执行. 2 ...
- C#中判断文件夹或文件是否存在的方法
一.根据虚拟路径获取文件物理路径: string savePath = Server.MapPath("~/Uploads/RemoteDatum/"); 二.判断文件夹是否存在 ...