pandas--排序和排名
obj=Series(range(4),index=['d','a','b','c']) obj.sort_index()
Out[18]:
a 1
b 2
c 3
d 0
dtype: int64
2、按值对Series进行排序
obj=Series([4,7,-3,2]) obj
Out[10]:
0 4
1 7
2 -3
3 2
dtype: int64 obj.sort_values()
Out[11]:
2 -3
3 2
0 4
1 7
dtype: int64
在排序时,任何缺失值默认都会被放到Series的末尾:
obj=Series([4,np.nan,7,np.nan,-3,2]) obj
Out[13]:
0 4.0
1 NaN
2 7.0
3 NaN
4 -3.0
5 2.0
dtype: float64 obj.sort_values()
Out[14]:
4 -3.0
5 2.0
0 4.0
2 7.0
1 NaN
3 NaN
dtype: float64
frame=DataFrame(np.arange(8).reshape((2,4)),index=['three','one'],
columns=['d','a','b','c']) frame
Out[20]:
d a b c
three 0 1 2 3
one 4 5 6 7 frame.sort_index()
Out[21]:
d a b c
one 4 5 6 7
three 0 1 2 3 #指定轴
frame.sort_index(axis=1)
Out[22]:
a b c d
three 1 2 3 0
one 5 6 7 4
数据默认是升序排序,但也可以降序排序
frame.sort_index(axis=1,ascending=False)
Out[23]:
d c b a
three 0 3 2 1
one 4 7 6 5
2、在DataFrame上,根据一个或多个列中的值来进行排序
frame=DataFrame({'b':[4,7,-3,2],'a':[0,1,0,1]})
frame
Out[16]:
a b
0 0 4
1 1 7
2 0 -3
3 1 2
frame.sort_values(by='b')
Out[17]:
a b
2 0 -3
3 1 2
0 0 4
1 1 7
#根据多个列进行排序,传入名称的列表即可
frame.sort_values(by=['a','b'])
Out[18]:
a b
2 0 -3
0 0 4
3 1 2
1 1 7
obj=Series([7,-5,7,4,2,0,4]) obj.rank()
Out[20]:
0 6.5
1 1.0
2 6.5
3 4.5
4 3.0
5 2.0
6 4.5
dtype: float64 #将数值进行排序,对于相同数据排名,取排名的平均值
根据值在原数据中出现的顺序给出排名:
obj.rank(method='first')
Out[21]:
0 6.0
1 1.0
2 7.0
3 4.0
4 3.0
5 2.0
6 5.0
dtype: float64
可以进行降序排名:
#max使用整个分组的最大排名
obj.rank(ascending=False,method='max')
Out[22]:
0 2.0
1 7.0
2 2.0
3 4.0
4 5.0
5 6.0
6 4.0
dtype: float64
frame=DataFrame({'b':[4.3,7,-3,2],'a':[0,1,0,1],
'c':[-2,5,-8,-2.5]})
frame
Out[25]:
a b c
0 0 4.3 -2.0
1 1 7.0 5.0
2 0 -3.0 -8.0
3 1 2.0 -2.5
#指定轴
frame.rank(axis=1)
Out[26]:
a b c
0 2.0 3.0 1.0
1 1.0 3.0 2.0
2 3.0 2.0 1.0
3 2.0 3.0 1.0
| 'average' | 默认:在相等分组中,为各值分配平均排名 |
| 'min' | 使用整个分组的最小排名 |
| 'max' | 使用整个分组的最大排名 |
| 'first' | 按值在原数据中的出现顺序分配排名 |
pandas--排序和排名的更多相关文章
- Pandas基本功能之算术运算、排序和排名
算术运算和数据对齐 Series和DataFrame中行运算和列运算有种特征叫做广播 在将对象相加时,如果存在不同的索引对,则结果的索引就是该索引对的并集.自动的数据对齐操作在不重叠的索引处引入了NA ...
- Pandas的排序和排名(Series, DataFrame) + groupby
根据条件对数据集排序(sorting)也是一种重要的内置运算.要对行或列索引进行排序(按字典顺序), 可使用sort_index 方法, 它将返回一个已排序的新对象: 而DataFrame, 则可以根 ...
- Pandas排序
Pandas有两种排序方式,它们分别是 - 按标签 按实际值 下面来看看一个输出的例子. import pandas as pd import numpy as np unsorted_df=pd.D ...
- 第七节:pandas排序
pandas具有两种排序方式:sort_index()和sort_values().
- PAT A1012 The Best Rank (25 分)——多次排序,排名
To evaluate the performance of our first year CS majored students, we consider their grades of three ...
- pandas 排序之 sort_values,reindex,reset_index, sort_index
如果想按照自己的方式排序ind = 行索引data= data[ind] ind = data.sum(axis=1).sort_values(ascending=False).index data ...
- Cognos开发自定义排序规则的报表和自定义排名报表
场景:有一个简单的销售数据分析,可以按照日期,按照商品类型来分析订单笔数和订单金额. 目的:用户可以自定义查看按照不同指标排序的数据,用户可以查看按照不同指标排名的前N名数据 一:功能及效果展示 效果 ...
- pandas 按照某一列进行排序
pandas排序的方法有很多,sort_values表示根据某一列排序 pd.sort_values("xxx",inplace=True) 表示pd按照xxx这个字段排序,inp ...
- 《利用python进行数据分析》读书笔记--第五章 pandas入门
http://www.cnblogs.com/batteryhp/p/5006274.html pandas是本书后续内容的首选库.pandas可以满足以下需求: 具备按轴自动或显式数据对齐功能的数据 ...
- 利用python进行数据分析之pandas库的应用(二)
本节介绍Series和DataFrame中的数据的基本手段 重新索引 pandas对象的一个重要方法就是reindex,作用是创建一个适应新索引的新对象 >>> from panda ...
随机推荐
- 获取图片地址url的后缀名
getNameFromLink(url){ if(url.indexOf('.cn/') !== -1){ return (url.split('.')[url.split('.') ...
- Python值正则表达式(RE)
要想在Python中使用正则表达式,首先要引入模块: import re . 匹配任意一个 + 匹配至少一个 * 匹配0个至多个 ? 1个或0个(可有可无) - 表范围 \ 转义 ^ 在首 $ ...
- Error in execution; nested exception is io.lettuce.core.RedisCommandExecutionException: ERR invalid longitude,latitude pair 111.110000,111.230000
io.lettuce.core.RedisCommandExecutionException: ERR invalid longitude,latitude pair 111.110000,111.2 ...
- change transformation file in PI interface
1. Jane extends the ZTMMASKU sap table 2. Jane write the program to write the new attribute to the t ...
- 小程序图片在安卓上拉伸的问题&导航&返回首页
今天提了一个bug,有几张图片在安卓上面加载会先变大拉伸再恢复正常 出现这样的问题应该是用widthFix造成的 具体原因还不是很清楚,因为都是本地图片,所以我就直接把高也设置好就暂时没有这个问题 ...
- postgresql like 中的转义
select * from tb_org where char_length(xdm)>8 and xdm not like '%*_%' ESCAPE '*' ESCAPE 后面的 * 是转 ...
- Ansible 和 Playbook 暂存
Ansible 和 Playbook 暂存 , 也是一个批量管理工具 自动化的批量管理工具 主机清单 HOST Inventory 模块插件 Playbooks 查看ansible的目录结构 ...
- 【Luogu】【关卡2-12】递推与递归二分(2017年10月)
任务说明:递推,层层递进,由基础推向顶层.二分不仅可以用来查找数据,还可以确定最合适的值. P1192 台阶问题 有N级的台阶,你一开始在底部,每次可以向上迈最多K级台阶(最少1级),问到达第N级台阶 ...
- KeepLived + nginx 高可用
. 环境准备 1. VMware; 2. 4台CentOs7虚拟主机:192.168.122.217, 192.168.122.165 3. 系统服务:LVS, Keepalived 4. Web服务 ...
- mutable and immutable
employees = ['Corey', 'John', 'Rick', 'Steve', 'Carl', 'Adam'] output = '<ul>\n' for employee ...