pandas(二)函数应用和映射
NumPy的ufuncs也可以操作pandas对象
>>> frame
one two three four
a 0 1 2 3
b 4 5 6 7
c 8 9 10 11
d 12 13 14 15
>>> np.square(frame)#求平方
one two three four
a 0 1 4 9
b 16 25 36 49
c 64 81 100 121
d 144 169 196 225
>>>
用DataFrame的apply方法,可以将函数应用到由各列或行所形成的一维数组中。
>>> frame
one two three four
a 0 1 2 3
b 4 5 6 7
c 8 9 10 11
d 12 13 14 15
>>> func = lambda x : x.max()-x.min()
>>> frame.apply(func)
one 12
two 12
three 12
four 12
dtype: int64
>>> frame.apply(func,axis = 1)
a 3
b 3
c 3
d 3
dtype: int64
用DataFrame的applymap方法,可以将函数应用到元素级的数据上。
>>> f = lambda x : x+1
>>> frame
one two three four
a 0 1 2 3
b 4 5 6 7
c 8 9 10 11
d 12 13 14 15
>>> frame.applymap(f)
one two three four
a 1 2 3 4
b 5 6 7 8
c 9 10 11 12
d 13 14 15 16
Series也有一个元素级函数应用的方法map
>>> frame['one'] #获取dataframe的列为一个Series对象
a 0
b 4
c 8
d 12
Name: one, dtype: int32
>>> frame['one'].map(f)
a 1
b 5
c 9
d 13
Name: one, dtype: int64
>>>
排序和排名
用sort_index对行或列进行排序,返回一个排序好的新对象
>>> obj = Series(range(4),index=['d','b','a','c'])
>>> new_obj = obj.sort_index()
>>> new_obj
a 2
b 1
c 3
d 0
dtype: int64
>>> obj
d 0
b 1
a 2
c 3
dtype: int64
>>>
>>> new_obj = obj.sort_index(ascending = False)#默认是升序,通过参数ascending可以设置降序
>>> new_obj
d 0
c 3
b 1
a 2
dtype: int64
对于DataFrame可以根据任意轴进行排序
>>> frame = DataFrame(np.random.randn(4,4),columns = ['c','a','d','b'],index=[3,1,4,2])
>>> frame
c a d b
3 0.004950 -1.272352 1.050491 0.823530
1 1.198348 0.647114 0.154131 -0.636497
4 -0.358309 0.525307 -1.868459 0.867197
2 -0.021764 0.140501 1.459700 -0.090884
>>> frame.sort_index()
c a d b
1 1.198348 0.647114 0.154131 -0.636497
2 -0.021764 0.140501 1.459700 -0.090884
3 0.004950 -1.272352 1.050491 0.823530
4 -0.358309 0.525307 -1.868459 0.867197
>>> frame.sort_index(axis =1)
a b c d
3 -1.272352 0.823530 0.004950 1.050491
1 0.647114 -0.636497 1.198348 0.154131
4 0.525307 0.867197 -0.358309 -1.868459
2 0.140501 -0.090884 -0.021764 1.459700
除了按照索引排序之外,还可以按照值排序
按值对Series进行排序的时候,用sort_values方法。在老版本中是order方法。
>>> obj = Series([3,4,1,6])
>>> obj
0 3
1 4
2 1
3 6
dtype: int64
>>> obj.sort_values()
2 1
0 3
1 4
3 6
dtype: int64
在排序时,缺失值会默认放到末尾。
在DataFrame中,可能希望按照一个或多个列中的值进行排序
>>> frame = DataFrame({'a':[4,7,-3,2],'b':[1,0,0,1]})
>>> frame
a b
0 4 1
1 7 0
2 -3 0
3 2 1
>>> frame.sort_index(by='a')#这个方法将在不久之后废弃,可以使用sort_values方法
__main__:1: FutureWarning: by argument to sort_index is deprecated, please use .sort_values(by=...)
a b
2 -3 0
3 2 1
0 4 1
1 7 0
>>> frame.sort_values(by='a')
a b
2 -3 0
3 2 1
0 4 1
1 7 0
>>>
根据多个列排序
>>> frame.sort_values(by=['b','a'])
a b
2 -3 0
1 7 0
3 2 1
0 4 1
排名跟排序有紧密的联系,首先根据值排序,然后增设一个排名值(从1开始,直到有效值的数量。如果两个值相等,都取两个排名的均值)
>>> obj = Series([7,-5,7,4,2,0,4])
>>> obj
0 7
1 -5
2 7
3 4
4 2
5 0
6 4
dtype: int64
>>> obj.rank()
0 6.5
1 1.0
2 6.5
3 4.5
4 3.0
5 2.0
6 4.5
dtype: float64
>>>
也可以根据值在原来数据中出现的顺序,进行排名。如果某几个值相等,现在数据中出现的排名靠前,这需要借助于method选项
>>> obj.rank(method='first')
0 6.0
1 1.0
2 7.0
3 4.0
4 3.0
5 2.0
6 5.0
dtype: float64
当然也支持降序排列,ascending=False即可
dataframe对象默认按照行排名,设置轴选项axis=1,就会按照列排名
method选项的值有
| method | 说明 |
| average | 默认:在相等分组中,为各个值分配平均排名 |
| mix | 使用整个分组的最大排名 |
| min | 使用整个分组的最小排名 |
| first | 按照值在原始数据中出现的顺序分配排名 |
带有重复值的轴索引
许多pandas函数需要标签唯一,但这并不是强制性的。
可以通过索引的is_unique去判断是否唯一
>>> obj =Series(range(5),index=['a','a','b','b','c'])
>>> obj
a 0
a 1
b 2
b 3
c 4
dtype: int64
>>> obj.index.is_unique
False
带有重复值索引,数据的选取时,如果索引对应多个值,返回一个Series,否则返回单个值
>>> obj['a']
a 0
a 1
dtype: int64
>>> obj['c']
4
对于DataFrame也是如此
如果索引对应多行,返回的依然是一个dataframe对象,否则是一个Series对象
>>> df = DataFrame(np.random.randn(5,3),index=['a','a','b','b','c'])
>>> df.ix['a']
0 1 2
a -0.757846 0.713964 -0.674956
a 0.198044 1.093223 -0.342281
>>> df.ix['c']
0 -2.647372
1 -0.526367
2 -0.296859
Name: c, dtype: float64
>>> type(df.ix['a'])
<class 'pandas.core.frame.DataFrame'>
>>> type(df.ix['c'])
<class 'pandas.core.series.Series'>
pandas(二)函数应用和映射的更多相关文章
- Pandas DataFrame 函数应用和映射
apply Numpy 的ufuncs通用函数(元素级数组方法)也可用于操作pandas对象: 另一个常见的操作是,将函数应用到由各列或行所形成的一维数组上.Dataframe的apply方法即可实现 ...
- NeHe OpenGL教程 第二十二课:凹凸映射
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- MyBatis学习 之 二、SQL语句映射文件(2)增删改查、参数、缓存
目录(?)[-] 二SQL语句映射文件2增删改查参数缓存 select insert updatedelete sql parameters 基本类型参数 Java实体类型参数 Map参数 多参数的实 ...
- 单片机中printf函数的重映射
单片机中printf函数的重映射 一.源自于:大侠有话说 1.如果你在学习单片机之前学过C语言,那么一定知道printf这个函数.它最最好用的功能 除了打印你想要的字符到屏幕上外,还能把数字进行格式化 ...
- 【转载】pandas常用函数
原文链接:https://www.cnblogs.com/rexyan/p/7975707.html 一.import语句 import pandas as pd import numpy as np ...
- MyBatis学习 之 二、SQL语句映射文件(1)resultMap
目录(?)[-] 二SQL语句映射文件1resultMap resultMap idresult constructor association联合 使用select实现联合 使用resultMap实 ...
- Pandas的函数应用、层级索引、统计计算
1.Pandas的函数应用 1.apply 和 applymap 1. 可直接使用NumPy的函数 示例代码: # Numpy ufunc 函数 df = pd.DataFrame(np.random ...
- Spring MVC 使用介绍(六)—— 注解式控制器(二):请求映射与参数绑定
一.概述 注解式控制器支持: 请求的映射和限定 参数的自动绑定 参数的注解绑定 二.请求的映射和限定 http请求信息包含六部分信息: ①请求方法: ②URL: ③协议及版本: ④请求头信息(包括Co ...
- pandas常用函数之shift
shift函数是对数据进行移动的操作,假如现在有一个DataFrame数据df,如下所示: index value1 A 0 B 1 C 2 D 3 那么如果执行以下代码: df.shift() 就会 ...
- pandas常用函数之diff
diff函数是用来将数据进行某种移动之后与原数据进行比较得出的差异数据,举个例子,现在有一个DataFrame类型的数据df,如下: index value1 A 0 B 1 C 2 D 3 如果执行 ...
随机推荐
- swfupload 上传报 security error # 2049 (security) 安全错误问题
老外给出类似理由: 大致是说这个是flash播放器自身组件安全策略问题, 禁止跨域上传的. I believe this is due to the Flash Player's "same ...
- PHP上传类 图片上传 upload class实现image crop resize 缩略图
manage uploaded files, and manipulate images in many ways through an HTML form, a Flash uploader, XM ...
- 【BZOJ】1680: [Usaco2005 Mar]Yogurt factory(贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=1680 看不懂英文.. 题意是有n天,第i天生产的费用是c[i],要生产y[i]个产品,可以用当天的也 ...
- cobbler 修改 distro_signatures.json
edit file in /var/lib/cobbler/distro_signatures.json and restart cobblerd service 转自: https://lists. ...
- 蓝桥杯 第三届C/C++预赛真题(5) 转方阵(C基本功)
对一个方阵转置,就是把原来的行号变列号,原来的列号变行号 例如,如下的方阵: 1 2 3 4 5 6 7 8 9 10 11 1213 14 15 16 转置后变为: 1 5 9 13 2 6 10 ...
- 鼠标滑入滑出,输入框获得失去焦点后触发事件的N种方法之一二
熟悉position的用法 <!doctype html><html lang="en"> <head> <meta charset=&q ...
- 你一定喜欢看的 Webpack 2.× 入门实战
from:https://www.jianshu.com/p/b83a251d53db?utm_campaign=maleskine&utm_content=note&utm_medi ...
- util.inherits
util.inherits util.inherits(constructor, superConstructor)是一个实现对象间原型继承 的函数. JavaScript 的面向对象特性是基于原型的 ...
- 标C编程笔记day05 函数声明、文件读写、联合类型、枚举类型
函数声明: 1.隐式声明:在没有声明的情况下,系统可依据參数类型推断去调用函数(有可能出错) 2.显式声明:声明在被调用之前.如:double add(double,double); ...
- ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]]
今天第一次遇到Failed to start component [StandardEngine[Catalina].StandardHost[localhost].错误,并且在错误提示的后半段出现了 ...