熊猫根据条件获取行ID

时间:2019-10-20 08:17:40

标签: python pandas

我有以下DataFrame:

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

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

为此,您需要访问double的索引并获取其ID。按照https://stackoverflow.com/a/21800319/3921457的说明并假设您的对象名为DataFrame,您可以执行以下操作:

df

如果ids = df.index[df['Date'] == '1992-01-03'].tolist() print(ids) # [2] 已经是Date的索引,则只需键入:DataFrame就可以更改为数字索引。

答案 1 :(得分:1)

您也可以使用np.where

np.where(df.Date=='1992-01-03')
相关问题