簡體   English   中英

Pandas 根據條件獲取行ID

[英]Pandas get the row ID based on condition

我有以下 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

有任何想法嗎?

為此,您需要訪問DataFrame的索引並獲取其 ID。 按照https://stackoverflow.com/a/21800319/3921457的解釋並假設您的 object 被命名為df您可以執行以下操作:

ids = df.index[df['Date'] == '1992-01-03'].tolist()
print(ids)
# [2]

如果Date已經是DataFrame的索引,您可以更改為數字索引,只需鍵入: df = df.reset_index()

您還可以使用np.where

np.where(df.Date=='1992-01-03')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM