繁体   English   中英

如何找到给定 Pandas 数据帧索引的位置索引?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM