我有以下数据框:
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
我希望获取 df['Date' == '1992-01-03']
的行 ID。
预期返回是2
。
有什么想法吗?
最佳答案
为此,您需要访问 DataFrame
的索引并获取其 ID。按照https://stackoverflow.com/a/21800319/3921457的解释假设你的对象被命名为 df
你可以这样做:
ids = df.index[df['Date'] == '1992-01-03'].tolist()
print(ids)
# [2]
如果 Date
已经是 DataFrame
的索引,您可以更改为数字索引,只需键入:df = df.reset_index()
.
关于python - Pandas 根据条件获取行 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58471161/