浅谈Pandas 排序之后索引的问题
作者:Claroja 时间:2022-03-18 12:28:32
如下所示:
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
来源:https://blog.csdn.net/claroja/article/details/76153152
标签:Pandas,排序,索引
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python 数据类型--集合set
2021-11-23 21:17:54
![](https://img.aspxhome.com/file/2023/4/68494_0s.png)
python装饰器代替set get方法实例
2023-01-25 15:40:37
一直闪烁变色的超级链接代码
2008-02-27 13:08:00
Python中True(真)和False(假)判断详解
2021-12-16 11:22:36
使用Python对微信好友进行数据分析
2021-04-12 18:10:58
![](https://img.aspxhome.com/file/2023/9/101479_0s.png)
Python中np.percentile和df.quantile分位数详解
2021-11-25 17:26:00
![](https://img.aspxhome.com/file/2023/7/97527_0s.png)
浅谈PHP中常用的3种设计模式
2023-10-18 06:23:02
python3 循环读取excel文件并写入json操作
2022-11-07 15:40:11
![](https://img.aspxhome.com/file/2023/3/82673_0s.jpg)
《色彩解答》系列之一 色彩层次
2008-02-17 14:26:00
![](https://img.aspxhome.com/file/UploadPic/20082/17/2008217143752778s.gif)
什么是Python变量作用域
2021-12-25 21:36:24
python内置函数之eval函数详解
2022-07-22 12:39:29
Python正则表达式学习小例子
2023-01-30 21:57:42
![](https://img.aspxhome.com/file/2023/3/79553_0s.png)
python同步两个文件夹下的内容
2022-09-14 13:54:13
登录与注册两者的距离
2009-01-02 16:48:00
![](https://img.aspxhome.com/file/UploadPic/20091/2/xanyou1-36s.png)
asp 多关键词搜索的简单实现方法
2011-04-11 10:45:00
【JavaScript实用系列】-- 胖页面载入效果
2009-10-19 22:48:00
Python根据成绩分析系统浅析
2023-08-02 20:25:07
![](https://img.aspxhome.com/file/2023/4/62024_0s.png)
学习ASP.NET八天入门:第一天
2007-08-07 13:08:00
![](https://img.aspxhome.com/file/UploadPic/up/2007080714434946.jpg)
2行Python代码实现给pdf文件添加水印
2022-04-03 00:29:26
![](https://img.aspxhome.com/file/2023/1/105031_0s.png)
python解决pandas处理缺失值为空字符串的问题
2021-10-21 09:01:38