Pandas 排序之后索引问题

来源:互联网 发布:法律咨询软件哪个好 编辑:程序博客网 时间:2024/06/03 15:48
In [1]: import pandas as pd   ...: df=pd.DataFrame({"a":[1,2,3,4,5],"b":[5,4,3,2,1]})In [2]: dfOut[2]:    a  b0  1  51  2  42  3  33  4  24  5  1In [3]: df=df.sort_values(by="b") # 按照b列排序In [4]: dfOut[4]:    a  b4  5  13  4  22  3  31  2  40  1  5In [5]: df.loc[0,:] # 按索引来索引所以得到了是排序末位Out[5]: a    1b    5Name: 0, dtype: int64In [6]: df.iloc[0,:] # 按照绝对的索引来索引,所以得到了第一位Out[6]: a    5b    1Name: 4, dtype: int64In [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] typesIn [8]: df.iloc[0,1] # “b”列的绝对位置是1,所以这就是索引了“b”列Out[8]: 1In [9]: df.iloc[0,:]["b"] # 和上述方法是一样的,不过这个更加容易懂一些Out[9]: 1
原创粉丝点击