[英]How to find the location index of a given Pandas dataframe index?
在 Python Pandas 中,如何找到数据帧的数字索引?
考虑以下示例。
import pandas as pd
df = pd.DataFrame(np.arange(20).reshape(5,4), index = np.arange(2,7),columns=["A","B","C","D"])
Output:
A B C D
2 0 1 2 3
3 4 5 6 7
4 8 9 10 11
5 12 13 14 15
6 16 17 18 19
如何找到df.index == 4的位置索引,答案应该是2。
尝试.get_loc
In [4]: df.index.get_loc(4)
Out[4]: 2
检查get_indexer
注意这里的不同之处在于get_loc
会为每个调用做一个值, get_indexer
你可以传递一个列表
df.index.get_indexer([4])
array([2], dtype=int64)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.