[英]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.