0
0

More than 3 years have passed since last update.

存在しない列名(行名)にアクセスしたときに出力されるエラーと対処方法について

Last updated at Posted at 2020-08-14

1 この記事は何?

下記のエラーが表示されたときの原因と対処法についてメモします。

エラー
KeyError: 'Passing list-likes to .loc or [] with any missing labels is no longer supported, see https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike'について

2 内容

現象の説明

今話題にしているエラーは、ありえない列名を抽出しようとしたときに発生するエラーです。
下記の例ですと、B列,C列,D列を列として備えていますが、F列は存在しません。存在しないにもかかわらず
F列を抽出しようとしているのでエラーが発生します。

sample.py
import pandas as pd
import numpy as np

dat = [
    ['2019-07-01','9997','740','78'],
    ['2019-07-02','9997','749','45'],
    ['2019-07-03','9997','757','12'],
    ['2019-07-04','9997','769','45'],
    ['2019-07-05','9997','762','8'],
    ['2019-07-08','9997','760','8']
]
df0 = pd.DataFrame(dat,columns=["DATE","B","C","D"])

#列Aをindexに指定する。
df0.set_index("DATE",inplace=True) 
print(df0)


#df0から列'B','F'を取り出すが、F列はdf0には存在しないのでエラーが出力される。
df2 = df0.loc[:,['B', 'F']]

実行結果

console
               B    C   D
DATE                     
2019-07-01  9997  740  78
2019-07-02  9997  749  45
2019-07-03  9997  757  12
2019-07-04  9997  769  45
2019-07-05  9997  762   8
2019-07-08  9997  760   8
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-100-4f9da40f40c4> in <module>
     18 
     19 #df0から列'B','F'を取り出すが、F列はdf0には存在しないのでエラーが出力される。
---> 20 df2 = df0.loc[:,['B', 'F']]
     21 
     22 
途中略

~\anaconda3\lib\site-packages\pandas\core\indexing.py in _validate_read_indexer(self, key, indexer, axis, raise_missing)
   1653             if not (ax.is_categorical() or ax.is_interval()):
   1654                 raise KeyError(
-> 1655                     "Passing list-likes to .loc or [] with any missing labels "
   1656                     "is no longer supported, see "
   1657                     "https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike"  # noqa:E501

KeyError: 'Passing list-likes to .loc or [] with any missing labels is no longer supported, see https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike'

対策

reindexを使い、今まで存在しなかったF列を追加してください。

sample.py
import pandas as pd
import numpy as np

dat = [
    ['2019-07-01','9997','740','78'],
    ['2019-07-02','9997','749','45'],
    ['2019-07-03','9997','757','12'],
    ['2019-07-04','9997','769','45'],
    ['2019-07-05','9997','762','8'],
    ['2019-07-08','9997','760','8']
]
df0 = pd.DataFrame(dat,columns=["DATE","B","C","D"])

#列Aをindexに指定する。
df0.set_index("DATE",inplace=True) 
print(df0)


#df0から列'F'をreindexで追加する。
print("reindexを使い列Fを追加した状態で列名を再定義します。")
df3 = df0.reindex(["B","C","D","F"], axis=1)
print(df3)

print("列Fが追加されたので列B,列Fが正しく抽出されています。")
df4 = df3.loc[:,['B', 'F']]
print(df4)

実行結果

console
                B    C   D
DATE                     
2019-07-01  9997  740  78
2019-07-02  9997  749  45
2019-07-03  9997  757  12
2019-07-04  9997  769  45
2019-07-05  9997  762   8
2019-07-08  9997  760   8

reindexを使い列Fを追加した状態で列名を再定義します。
               B    C   D   F
DATE                         
2019-07-01  9997  740  78 NaN
2019-07-02  9997  749  45 NaN
2019-07-03  9997  757  12 NaN
2019-07-04  9997  769  45 NaN
2019-07-05  9997  762   8 NaN
2019-07-08  9997  760   8 NaN

列Fが追加されたので列B,列Fが正しく抽出されています。
               B   F
DATE                
2019-07-01  9997 NaN
2019-07-02  9997 NaN
2019-07-03  9997 NaN
2019-07-04  9997 NaN
2019-07-05  9997 NaN
2019-07-08  9997 NaN
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0