How can I safely select rows in pandas by a list of labels?
I want to get and Error
when list contains any non-existing label.
Method loc
doesn't raise a KeyError if at least 1 of the labels for which you ask is in the index. But this is not sufficient.
For example:
df = pd.DataFrame(index=list('abcde'), data={'A': np.arange(5) + 10})
df
A
a 10
b 11
c 12
d 13
e 14
# here I would like to get an Error as 'xx' and 'yy' are not in the index
df.loc[['b', 'xx', 'yy']]
A
b 11.0
xx NaN
yy NaN
Do pandas provide such a method that would raise a KeyError instead of returning me a bunch of NaNs for non-existing labels?