2. 构建多个布尔条件

  1. In[11]: movie = pd.read_csv('data/movie.csv', index_col='movie_title')
  2. movie.head()
  3. Out[11]:

2. 构建多个布尔条件 - 图1

  1. # 创建多个布尔条件
  2. In[12]: criteria1 = movie.imdb_score > 8
  3. criteria2 = movie.content_rating == 'PG-13'
  4. criteria3 = (movie.title_year < 2000) | (movie.title_year >= 2010)
  5. criteria2.head()
  6. Out[12]: movie_title
  7. Avatar True
  8. Pirates of the Caribbean: At World's End True
  9. Spectre True
  10. The Dark Knight Rises True
  11. Star Wars: Episode VII - The Force Awakens False
  12. Name: content_rating, dtype: bool
  1. # 将这些布尔条件合并成一个
  2. In[13]: criteria_final = criteria1 & criteria2 & criteria3
  3. criteria_final.head()
  4. Out[13]: movie_title
  5. Avatar False
  6. Pirates of the Caribbean: At World's End False
  7. Spectre False
  8. The Dark Knight Rises True
  9. Star Wars: Episode VII - The Force Awakens False
  10. Name: content_rating, dtype: bool

更多

  1. # 在Pandas中,位运算符(&, |, ~)的优先级高于比较运算符,因此如过前面的条件3不加括号,就会报错
  2. In[14]: movie.title_year < 2000 | movie.title_year > 2009
  3. ---------------------------------------------------------------------------
  4. TypeError Traceback (most recent call last)
  5. /Users/Ted/anaconda/lib/python3.6/site-packages/pandas/core/ops.py in na_op(x, y)
  6. 882 try:
  7. --> 883 result = op(x, y)
  8. 884 except TypeError:
  9. /Users/Ted/anaconda/lib/python3.6/site-packages/pandas/core/ops.py in <lambda>(x, y)
  10. 130 names('rand_'), op('&')),
  11. --> 131 ror_=bool_method(lambda x, y: operator.or_(y, x),
  12. 132 names('ror_'), op('|')),
  13. TypeError: ufunc 'bitwise_or' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
  14. During handling of the above exception, another exception occurred:
  15. ValueError Traceback (most recent call last)
  16. /Users/Ted/anaconda/lib/python3.6/site-packages/pandas/core/ops.py in na_op(x, y)
  17. 900 y = bool(y)
  18. --> 901 result = lib.scalar_binop(x, y, op)
  19. 902 except:
  20. pandas/_libs/lib.pyx in pandas._libs.lib.scalar_binop (pandas/_libs/lib.c:15035)()
  21. ValueError: Buffer dtype mismatch, expected 'Python object' but got 'double'
  22. During handling of the above exception, another exception occurred:
  23. TypeError Traceback (most recent call last)
  24. <ipython-input-14-1e7ee3f1401c> in <module>()
  25. ----> 1 movie.title_year < 2000 | movie.title_year > 2009
  26. /Users/Ted/anaconda/lib/python3.6/site-packages/pandas/core/ops.py in wrapper(self, other)
  27. 933 is_integer_dtype(np.asarray(other)) else fill_bool)
  28. 934 return filler(self._constructor(
  29. --> 935 na_op(self.values, other),
  30. 936 index=self.index)).__finalize__(self)
  31. 937
  32. /Users/Ted/anaconda/lib/python3.6/site-packages/pandas/core/ops.py in na_op(x, y)
  33. 903 raise TypeError("cannot compare a dtyped [{0}] array with "
  34. 904 "a scalar of type [{1}]".format(
  35. --> 905 x.dtype, type(y).__name__))
  36. 906
  37. 907 return result
  38. TypeError: cannot compare a dtyped [float64] array with a scalar of type [bool]