Make a note of the cause and remedy when the following error is displayed.
Error </ b> 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 About -listlike'
The error we're talking about right now is the one that happens when you try to extract an impossible column name. In the example below, columns B, C, and D are provided as columns, but column F does not exist. Despite not existing I am getting an error because I am trying to extract column 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"])
#Specify column A as index.
df0.set_index("DATE",inplace=True)
print(df0)
#Columns from df0'B','F'However, since column F does not exist in df0, an error is output.
df2 = df0.loc[:,['B', 'F']]
Execution result
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 #Columns from df0'B','F'However, since column F does not exist in df0, an error is output.
---> 20 df2 = df0.loc[:,['B', 'F']]
21
22
Omitted
~\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'
Use reindex to add column F that didn't exist before.
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"])
#Specify column A as index.
df0.set_index("DATE",inplace=True)
print(df0)
#Columns from df0'F'With reindex.
print("Redefine the column name with column F added using reindex.")
df3 = df0.reindex(["B","C","D","F"], axis=1)
print(df3)
print("Column B has been added, so column B,Column F has been extracted correctly.")
df4 = df3.loc[:,['B', 'F']]
print(df4)
Execution result
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
Redefine the column name with column F added using reindex.
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
Column B has been added, so column B,Column F has been extracted correctly.
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
Recommended Posts