0

https://ibb.co/b7VHCc3 [DataFrames]

Whenever I try to run this I keep on getting KeyError

masterdata = cabdata.merge(transaction, on= 'Transaction ID').merge(customer, on ='Customer ID').merge(city, on = 'City').merge(holidaydata, on='Holidays')

The error :

KeyError                                  Traceback (most recent call last)
<ipython-input-33-93bf991ced3b> in <module>
----> 1 masterdata = cabdata.merge(transaction, on= 'Transaction ID').merge(customer, on ='Customer ID').merge(city, on = 'City').merge(holidaydata, on='Holidays')

~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/frame.py in merge(self, right, how, on, left_on, right_on, left_index, right_index, sort, suffixes, copy, indicator, validate)
   7295             copy=copy,
   7296             indicator=indicator,
-> 7297             validate=validate,
   7298         )
   7299 

~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/reshape/merge.py in merge(left, right, how, on, left_on, right_on, left_index, right_index, sort, suffixes, copy, indicator, validate)
     84         copy=copy,
     85         indicator=indicator,
---> 86         validate=validate,
     87     )
     88     return op.get_result()

~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/reshape/merge.py in __init__(self, left, right, how, on, left_on, right_on, axis, left_index, right_index, sort, suffixes, copy, indicator, validate)
    625             self.right_join_keys,
    626             self.join_names,
--> 627         ) = self._get_merge_keys()
    628 
    629         # validate the merge keys dtypes. We may need to coerce

~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/reshape/merge.py in _get_merge_keys(self)
    994                         right_keys.append(rk)
    995                     if lk is not None:
--> 996                         left_keys.append(left._get_label_or_level_values(lk))
    997                         join_names.append(lk)
    998                     else:

~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py in _get_label_or_level_values(self, key, axis)
   1690             values = self.axes[axis].get_level_values(key)._values
   1691         else:
-> 1692             raise KeyError(key)
   1693 
   1694         # Check for duplicates

KeyError: 'Holidays'```


  [1]: https://i.stack.imgur.com/amV9C.png
  [2]: https://i.stack.imgur.com/FGBF3.png
  • Without the dataframes it is hard to help. Please run the mergers one at a time to see which causes the error – gtomer Jun 12 '22 at 09:02

1 Answers1

0

It seems that at least two of the following DataFrames have the column Holidays: cabdata, transaction, customer, city

Remove the last merge and print the column names. You should see these two columns: Holidays1 and Holidays2

Amir Py
  • 121
  • 1
  • 4