How do I use within / in operator in a Pandas DataFrame?

This question already has an answer here:

  • Filter dataframe rows if value in column is in a set list of values 7 answers
  • Finding the index of an item given a list containing it in Python 23 answers

  • Use the Series.isin() method to check if a series value is in a list of values. In your case -

    df[df['House'].isin([1, 2, 3, 4, 6, 7, 8, 9, 10, 13, 17, 18, 20, 21, 23, 26, 28, 30, 34, 46, 57, 58, 61, 86, 89, 102, 121, 156])]
    

    Example -

    In [77]: df
    Out[77]:
       A  B
    0  1  5
    1  2  6
    2  3  7
    3  4  8
    
    In [78]: df[df['A'].isin([1,2])]
    Out[78]:
       A  B
    0  1  5
    1  2  6
    
    链接地址: http://www.djcxy.com/p/28106.html

    上一篇: 在元组列表中查找精确的元组匹配并返回其索引

    下一篇: 如何在Pandas DataFrame中的/ in运算符中使用?