当前位置: 移动技术网 > IT编程>脚本编程>Python > 浅谈Pandas 排序之后索引的问题

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

2018年08月19日  | 移动技术网IT编程  | 我要评论

独行天下,刻录炼金师,地狱男爵有几部

如下所示:

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 排序之后索引的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网