Python Forum
And and Or Filters on Same Line
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
And and Or Filters on Same Line
#1
Hello, I am trying to filter two columns in a dataset. One filter is a selection type, the other filter will be to include key words in a description. I do not understand the error codes that I am getting. Any ideas how I could get the program to show the filtered results?


filt =[ (df['Type of breach']!='HACK') & (df['Description of incident'].str.contains('bank account' or 'social security number', na=False))]

df[filt]
Error:
ValueError Traceback (most recent call last) <ipython-input-15-fe2f66bc4df2> in <module> ----> 1 df[filt] ~\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key) 2789 # Do we have a (boolean) 1d indexer? 2790 if com.is_bool_indexer(key): -> 2791 return self._getitem_bool_array(key) 2792 2793 # We are left with two options: a single key, and a collection of keys, ~\anaconda3\lib\site-packages\pandas\core\frame.py in _getitem_bool_array(self, key) 2836 elif len(key) != len(self.index): 2837 raise ValueError( -> 2838 f"Item wrong length {len(key)} instead of {len(self.index)}." 2839 ) 2840 ValueError: Item wrong length 1 instead of 9015.
Reply
#2
Just remove square brackets. 'bank account' or 'social security number' will be evaluated to the first value, i.e. 'bank account', that is not what you really want. You probably want something like the following:

filt =(df['Type of breach']!='HACK') & (df['Description of incident'].str.contains('bank account', na=False) | df['Description of incident'].str.contains('social security number', na=False))
Reply
#3
Thank you very much. That seemed to work.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020