Pandas .loc without KeyError

前端 未结 5 2208
南方客
南方客 2020-12-17 23:57
>>> pd.DataFrame([1], index=[\'1\']).loc[\'2\']  # KeyError
>>> pd.DataFrame([1], index=[\'1\']).loc[[\'2\']]  # KeyError
>>> pd.DataFrame         


        
5条回答
  •  無奈伤痛
    2020-12-18 00:22

    This page https://pandas.pydata.org/pandas-docs/stable/indexing.html#deprecate-loc-reindex-listlike has the solution:

    In [8]: pd.DataFrame([1], index=['1']).reindex(['2']) Out[8]: 0 2 NaN

提交回复
热议问题