I just wanted to post this in case anyone else experiences this error. I was trying to filter on a previously filtered dataset, on multiple conditions when I received the above error.
Asked
Active
Viewed 95 times
-2
-
As you can see from the sidebar, this `ambiguity` error has come up often. While the error mentions `any/and`, the actual fix depends on the context. It results from creating a boolean array (or Pandas Series) in a statement that expects a scalar boolean. Python `if, `and` and `or` are common problem contexts. Understanding the problem is the first step to fixing it. – hpaulj Jun 14 '21 at 21:35
1 Answers
0
After some searching and trial and error, I realized I had to use np.logical_and instead of the and.
bad code:
filter_RM = (boston_df_filtered_CHAS['RM'].values > 5 and boston_df_filtered_CHAS['RM'].values < 7.5)
boston_df_filtered_RM = boston_df_filtered_CHAS[filter_RM] boston_df_filtered_RM
good code:
filter_RM = np.logical_and(boston_df_filtered_CHAS['RM'].values > 5, boston_df_filtered_CHAS['RM'].values < 7.5)
boston_df_filtered_RM = boston_df_filtered_CHAS[filter_RM] boston_df_filtered_RM
I hope this might be useful to someone.

daniness
- 363
- 1
- 4
- 21