Search code examples
pandaswarnings

Ignore warning Pandas KeyError: value not in index


Is there a way to suppress the pandas KeyError: '[x]' not in index? For example, if I have a data frame with columns A B C, and I call df[['A','B','C','D']], is it possible to have it just return A,B,C and ignore D if it does not exist?

Example code

import pandas as pd
import numpy as np

a = np.matrix('[1,4,5];[1,2,2];[9,7,5]')

df = pd.DataFrame(a,columns=['A','B','C'])

df[['A','B','C','D']]

Here's the error message

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/pandas/core/frame.py", line 2133, in __getitem__
    return self._getitem_array(key)
  File "/usr/lib/python3/dist-packages/pandas/core/frame.py", line 2177, in _getitem_array
    indexer = self.loc._convert_to_indexer(key, axis=1)
  File "/usr/lib/python3/dist-packages/pandas/core/indexing.py", line 1269, in _convert_to_indexer
    .format(mask=objarr[mask]))
KeyError: "['D'] not in index"

Solution

  • Use the column intersection with your desired list when selecting columns. You get all columns when they exist and only the subset that exists with fewer columns, without any errors.

    l = ['A', 'B', 'C', 'D']
    df[df.columns.intersection(l)]
    
       A  B  C
    0  1  4  5
    1  1  2  2
    2  9  7  5