有没有一种方法可以抑制熊猫KeyError:'[x]'不在索引中?例如,如果我有一个带有ABC列的数据框,并且我调用了df [[''A','B','C','D']],是否有可能只返回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"
答案 0 :(得分:1)
选择列时,将列交点与所需列表一起使用。当所有列都存在时,您将得到所有列,并且只有存在的列较少的子集,而不会出现任何错误。
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
答案 1 :(得分:1)
或者,如果您确实想要D
,则可以在axis=1
上reindex()
:
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