在已知id索引的情況下,如何獲取所需要的行呢?已經不止一次遇到這樣的情況,經歷過重重篩選,所得到的最終結果是一串滿足所有條件的id列表。
pandas 的isin 能很好的解決這個問題,
import pandas as pd a = [1,2,4] b = pd.DataFrame([[1,2], [2,4], [3,4]],index=[1,2,3],columns=["A","B"]) b["A"].isin(a) 1 True 2 True 3 False Name: A, dtype: bool # 取反 ~b["A"].isin(a) 1 False 2 False 3 True Name: A, dtype: bool