欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

浅谈Pandas 排序之后索引的问题

时间:2021-07-30 08:13:21|栏目:Python代码|点击:

如下所示:

In [1]: import pandas as pd
 ...: df=pd.DataFrame({"a":[1,2,3,4,5],"b":[5,4,3,2,1]})
In [2]: df
Out[2]: 
 a b
0 1 5
1 2 4
2 3 3
3 4 2
4 5 1
In [3]: df=df.sort_values(by="b") # 按照b列排序
In [4]: df
Out[4]: 
 a b
4 5 1
3 4 2
2 3 3
1 2 4
0 1 5
In [5]: df.loc[0,:] # 按索引来索引所以得到了是排序末位
Out[5]: 
a 1
b 5
Name: 0, dtype: int64
In [6]: df.iloc[0,:] # 按照绝对的索引来索引,所以得到了第一位
Out[6]: 
a 5
b 1
Name: 4, dtype: int64
In [7]: df.iloc[0,"b"] # 因为是绝对位置,所以列的参数不能是列名
ValueError: Location based indexing can only have [integer, integer slice (START point is INCLUDED, END point is EXCLUDED), listlike of integers, boolean array] types
In [8]: df.iloc[0,1] # “b”列的绝对位置是1,所以这就是索引了“b”列
Out[8]: 1
In [9]: df.iloc[0,:]["b"] # 和上述方法是一样的,不过这个更加容易懂一些
Out[9]: 1

上一篇:Python中文分词工具之结巴分词用法实例总结【经典案例】

栏    目:Python代码

下一篇:Linux(Redhat)安装python3.6虚拟环境(推荐)

本文标题:浅谈Pandas 排序之后索引的问题

本文地址:http://www.codeinn.net/misctech/162612.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有