Pandas 根据条件获取行 ID

win*_*ing 3 python pandas

我有以下数据框:

Date          best    a    b    c    d
1990-01-01    a       5    4    7    2
1991-01-02    c       10   1    2    0
1992-01-03    d       2    1    4    12
1993-01-04    a       5    8    11   6
Run Code Online (Sandbox Code Playgroud)

我希望获得行 ID,其中df['Date' == '1992-01-03'].

预期回报是2.

有任何想法吗?

gar*_*des 8

为此,您需要访问 的DataFrame索引并获取其 id。按照/sf/answers/1526022361/的解释并假设您的对象已命名,df您可以执行以下操作:

ids = df.index[df['Date'] == '1992-01-03'].tolist()
print(ids)
# [2]
Run Code Online (Sandbox Code Playgroud)

如果Date已经是 的索引DataFrame,您只需键入:即可更改为数字索引df = df.reset_index()