IndexingError: Too many indexers for Python filter

2930 views python pandas filter
2

I was following the tutorial: Python Pandas Tutorial (Part 4): Filtering - Using Conditionals to Filter Rows and Columns. At around time 11.30, the author made a filter. I tried to do the same with sample data:

    id          Date        ITEM_ID TYP YearMonth   VALUE
0   13710750    2019-07-01  SLM607  O   2019-07     10
1   13710760    2019-07-01  SLM607  O   2019-07     10
2   13710770    2019-07-03  SLM607  O   2019-07     2
3   13710780    2019-09-03  SLM607  O   2019-09     5
4   13667449    2019-08-02  887643  O   2019-08     7
5   13667450    2019-08-02  792184  O   2019-08     1
6   13728171    2019-09-17  SLM607  I   2019-09     1
7   13667452    2019-08-02  794580  O   2019-08     3

reproducible example

data = {
    "id": [
        13710750,
        13710760,
        13710770,
        13710780,
        13667449,
        13667450,
        13728171,
        13667452,
    ],
    "Date": [
        "2019-07-01",
        "2019-07-01",
        "2019-07-03",
        "2019-09-03",
        "2019-08-02",
        "2019-08-02",
        "2019-09-17",
        "2019-08-02",
    ],
    "ITEM_ID": [
        "SLM607",
        "SLM607",
        "SLM607",
        "SLM607",
        "887643",
        "792184",
        "SLM607",
        "794580",
    ],
    "TYPE": ["O", "O", "O", "O", "O", "O", "I", "O"],
    "YearMonth": [
        "2019-07",
        "2019-07",
        "2019-07",
        "2019-09",
        "2019-08",
        "2019-08",
        "2019-09",
        "2019-08",
    ],
    "VALUE": [10, 10, 2, 5, 7, 1, 1, 3],
}

df = pd.DataFrame(data)
df

with code:

mask =(df['VALUE']>0)
dfx.loc[mask, ['ITEM_ID', 'TYPE']]

and caught error:

---------------------------------------------------------------------------
IndexingError                             Traceback (most recent call last)
<ipython-input-841-88806e1eb3d9> in <module>
      1 mask =(df['VALUE']>0)
----> 2 dfx.loc[mask, ['ITEM_ID', 'TYPE']]

~\Anaconda3\lib\site-packages\pandas\core\indexing.py in __getitem__(self, key)
   1759                 except (KeyError, IndexError, AttributeError):
   1760                     pass
-> 1761             return self._getitem_tuple(key)
   1762         else:
   1763             # we by definition only have the 0th axis

~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _getitem_tuple(self, tup)
   1274 
   1275         # no multi-index, so validate all of the indexers
-> 1276         self._has_valid_tuple(tup)
   1277 
   1278         # ugly hack for GH #836

~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _has_valid_tuple(self, key)
    698         for i, k in enumerate(key):
    699             if i >= self.ndim:
--> 700                 raise IndexingError("Too many indexers")
    701             try:
    702                 self._validate_key(k, i)

IndexingError: Too many indexers

Why isn't this working?

answered question

You are using dfx instead df is that typo?

I guess theres a typo, it should be df insteaddfx on error line. Also when you are accessing columns we don't use .loc its used for indexing rows. So remove .loc from the line and then try placing mask.

1 Answer

4

IIUC you filter another DataFrame, dfx instead df:

mask =(df['VALUE']>0)
df = df.loc[mask, ['ITEM_ID', 'TYP']]
print (df)
  ITEM_ID TYP
0  SLM607   O
1  SLM607   O
2  SLM607   O
3  SLM607   O
4  887643   O
5  792184   O
6  SLM607   I
7  794580   O

posted this

Have an answer?

JD

Please login first before posting an answer.