python - Pandas 根据条件获取行 ID

标签 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

我希望获取 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/

相关文章:

Python 比较两个元组元素列表

python - SQLAlchemy .all() 只返回多连接的第一个结果

python - Pandas 排名方法密集但跳过数字

python - 根据条件获取数据帧行数

python - 如何将数据帧的每一列附加到 pandas 的系列中?

python - 使用箭头日期操作Python库以字符串形式解析日期

python - Tensorflow:使用clip_by_norm或clip_by_global_norm时如何确定clip_norm的值?

python - Pygame 类型错误 : missing 1 required positional argument:

Python 告诉我 66 <= 17

python - 快速高效的pandas groupby sum操作