--------------------------------------------------------------------------- KeyError…
- User
KeyError Traceback (most recent call last) Cell In[2], line 10 6 df1 = df.iloc[2:,[0,5,6,7,10,11,17]] 9 country = pd.read_csv('Coutry_Region_Abbr.csv') ---> 10 df2 = df1.merge(country, left_on = 'Country/Region to Audit\n', right_on = 'Country', how = 'left') 13 ##GRQS 14 df0 = df2.rename(columns={df2.columns[4]: 'Risk Level & Violation Times'})
File ~/anaconda3/lib/python3.10/site-packages/pandas/core/frame.py:10093, in DataFrame.merge(self, right, how, on, left_on, right_on, left_index, right_index, sort, suffixes, copy, indicator, validate) 10074 @Substitution("") 10075 @Appender(_merge_doc, indents=2) 10076 def merge( (...) 10089 validate: str | None = None, 10090 ) -> DataFrame: 10091 from pandas.core.reshape.merge import merge
10093 return merge( 10094 self, 10095 right, 10096 how=how, 10097 on=on, 10098 left_on=left_on, 10099 right_on=right_on, 10100 left_index=left_index, 10101 right_index=right_index, 10102 sort=sort, 10103 suffixes=suffixes, 10104 copy=copy, 10105 indicator=indicator, 10106 validate=validate, 10107 )
File ~/anaconda3/lib/python3.10/site-packages/pandas/core/reshape/merge.py:110, in merge(left, right, how, on, left_on, right_on, left_index, right_index, sort, suffixes, copy, indicator, validate) 93 @Substitution("\nleft : DataFrame or named Series") 94 @Appender(_merge_doc, indents=0) 95 def merge( (...) 108 validate: str | None = None, 109 ) -> DataFrame: --> 110 op = _MergeOperation( 111 left, 112 right, 113 how=how, 114 on=on, 115 left_on=left_on, 116 right_on=right_on, 117 left_index=left_index, 118 right_index=right_index, 119 sort=sort, 120 suffixes=suffixes, 121 indicator=indicator, 122 validate=validate, 123 ) 124 return op.get_result(copy=copy)
File ~/anaconda3/lib/python3.10/site-packages/pandas/core/reshape/merge.py:703, in _MergeOperation.init(self, left, right, how, on, left_on, right_on, axis, left_index, right_index, sort, suffixes, indicator, validate) 696 self._cross = cross_col 698 # note this function has side effects 699 ( 700 self.left_join_keys, 701 self.right_join_keys, 702 self.join_names, --> 703 ) = self._get_merge_keys() 705 # validate the merge keys dtypes. We may need to coerce 706 # to avoid incompatible dtypes 707 self._maybe_coerce_merge_keys()
File ~/anaconda3/lib/python3.10/site-packages/pandas/core/reshape/merge.py:1179, in _MergeOperation._get_merge_keys(self) 1175 if lk is not None: 1176 # Then we're either Hashable or a wrong-length arraylike, 1177 # the latter of which will raise 1178 lk = cast(Hashable, lk) -> 1179 left_keys.append(left._get_label_or_level_values(lk)) 1180 join_names.append(lk) 1181 else: 1182 # work-around for merge_asof(left_index=True)
- Assistant
The error message indicates that there is a
KeyErroron the column 'Country/Region to Audit\n' when attempting to merge the data framesdf1andcountry. This means that the column name 'Country/Region to Audit\n' does not exist in the data framedf1.To resolve this issue, you need to make sure that the column name 'Country/Region to Audit\n' is correct and exists in
df1. You can check the column names ofdf1by printingdf1.columnsand make sure that the desired column is present. If the column name is different, you can update theleft_onparameter in themergefunction accordingly.If the column name is correct, then it is possible that there is a typo or formatting issue in the column name. You can manually inspect the data frame
dfto verify the column name and adjust it as needed.