Ignore warning Pandas KeyError: value not in index

Ignore warning Pandas KeyError: value not in index

有没有办法抑制 pandas KeyError: '[x]' not in index?例如,如果我有一个包含 A B C 列的数据框,并且我调用 df[['A','B','C','D']],是否有可能它只是 return A,B,C 如果不存在则忽略 D?

示例代码

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']]

这是错误信息

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"

选择列时使用与所需列表相交的列。当它们存在时,您将获得所有列,并且仅存在列数较少的子集,没有任何错误。

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

如果你确实想要 D,你可以 reindex() on axis=1:

l = ['A', 'B', 'C', 'D']
df.reindex(l, axis=1)

   A  B  C   D
0  1  4  5 NaN
1  1  2  2 NaN
2  9  7  5 NaN