Search code examples
pythonpandasjupyter-notebookkeyerror

Pandas KeyError while adding more than one column from another DataFrame


I got this huge csv with backlog data. I want to take some columns from it and add them to another DataFrame. Looks like this:

Backlog = r'C:\Users\Desktop\personalized reports\Backlog Case\R1_Ext_Report.csv'
df_backlog = pd.read_csv(Backlog, sep=',')
df2 = df_backlog['PROMOTION CODE', 'MODEL DESCRIPTION']

The names of columns above are exactly the same as in the csv. The code works if I run it with a single column, such as 'Promotion Code', but once I add 2 or more of them, I receive a KeyError:

KeyError                                  Traceback (most recent call last)
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   3077             try:
-> 3078                 return self._engine.get_loc(key)
   3079             except KeyError:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: ('PROMOTION CODE', 'MODEL DESCRIPTION')

Will appreciate any help.


Solution

  • You need to supply a list for multiple columns:

    df2 = df_backlog[['PROMOTION CODE', 'MODEL DESCRIPTION']]