I have a Pandas DataFrame called 'dt', which has two columns called 'A' and 'B'. The values of column 'B' are numpy arrays; Something like this:
index A B
0 a [1,2,3]
1 b [2,3,4]
2 c [3,4,5]
Where:
type (dt["B"][0])
returns: numpy.ndarray
I want to filter this DataFrame to get another DataFrame, where only rows that have a certain element in the numpy array stored in 'B' are present.
I've tried this:
dt [element in dt["B"]]
So for example:
dt [2 in dt["B"]]
should return:
index A B
0 a [1,2,3]
1 b [2,3,4]
But this results in an error, namely "KeyError: True"
If the values of column "B" were strings, I could done the same with no error:
dt [dt["B"]==value]
So I wonder why my code doesn't work, and what does "KeyError: True" mean.
The complete error is this:
KeyError Traceback (most recent call last)
~/Applications/Conda/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)2645 try:
-> 2646 return self._engine.get_loc(key)2647 except KeyError:pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()KeyError: TrueDuring handling of the above exception, another exception occurred:KeyError Traceback (most recent call last)
<ipython-input-151-aa9ea046a48f> in <module>
----> 1 quotes_of_base["BTC" in quotes_of_base["quote"]]~/Applications/Conda/lib/python3.7/site-packages/pandas/core/frame.py in __getitem__(self, key)2798 if self.columns.nlevels > 1:2799 return self._getitem_multilevel(key)
-> 2800 indexer = self.columns.get_loc(key)2801 if is_integer(indexer):2802 indexer = [indexer]~/Applications/Conda/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)2646 return self._engine.get_loc(key)2647 except KeyError:
-> 2648 return self._engine.get_loc(self._maybe_cast_indexer(key))2649 indexer = self.get_indexer([key], method=method, tolerance=tolerance)2650 if indexer.ndim > 1 or indexer.size > 1:pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()KeyError: True