浅谈Pandas 排序之后索引的问题
2019/7/15 0:40:32
本文主要是介绍浅谈Pandas 排序之后索引的问题,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
如下所示:
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
以上这篇浅谈Pandas 排序之后索引的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持找一找教程网。
这篇关于浅谈Pandas 排序之后索引的问题的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python