0

I have the data file which looks like this - wrong country names

And I have another data file which has all the correct country names. For matching both the files that, I am using below:

from fuzzywuzzy import process
import pandas as pd




names_array=[]
ratio_array=[]
def match_names(wrong_names,correct_names):
    for row in wrong_names:
         x=process.extractOne(row, correct_names)
         names_array.append(x[0])
         ratio_array.append(x[1])
    return names_array,ratio_array



#Wrong country names dataset
df=pd.read_csv("wrong-country-names.csv",encoding="ISO-8859-1")
wrong_names=df['name'].dropna().values

#Correct country names dataset
choices_df=pd.read_csv("country-names.csv",encoding="ISO-8859-1")
correct_names=choices_df['name'].values

name_match,ratio_match=match_names(wrong_names,correct_names)



df['correct_country_name']=pd.Series(name_match)
df['country_names_ratio']=pd.Series(ratio_match)

df.to_csv("string_matched_country_names.csv")

print(df[['name','correct_country_name','country_names_ratio']].head(10))

I get the below error:

runfile('C:/Users/Drashti Bhatt/Desktop/untitled0.py', wdir='C:/Users/Drashti Bhatt/Desktop')
Traceback (most recent call last):

  File "<ipython-input-155-a1fd87d9f661>", line 1, in <module>
    runfile('C:/Users/Drashti Bhatt/Desktop/untitled0.py', wdir='C:/Users/Drashti Bhatt/Desktop')

  File "C:\Users\Drashti Bhatt\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "C:\Users\Drashti Bhatt\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/Drashti Bhatt/Desktop/untitled0.py", line 17, in <module>
    wrong_names=df['name'].dropna().values

  File "C:\Users\Drashti Bhatt\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2927, in __getitem__
    indexer = self.columns.get_loc(key)

  File "C:\Users\Drashti Bhatt\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2659, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))

  File "pandas/_libs/index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc

  File "pandas/_libs/index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc

  File "pandas/_libs/hashtable_class_helper.pxi", line 1601, in pandas._libs.hashtable.PyObjectHashTable.get_item

      File "pandas/_libs/hashtable_class_helper.pxi", line 1608, in pandas._libs.hashtable.PyObjectHashTable.get_item

    KeyError: 'name'

Any help on this will be much appreciated! Thanks much!

Mohit Motwani
  • 4,662
  • 3
  • 17
  • 45

1 Answers1

0

Your code contains e.g. wrong_names=df['name'].dropna().values (it is mentioned in your Traceback as the "offending" line).

And now look at the picture presenting your DataFrame:

  • it does not contain name column,
  • it contains Country column.

Go back to the Traceback: At the very end there is the error message: KeyError: 'name'.

So you attempt to access a non-existing column.

I noticed also another detail: values attribute contains the underlying Numpy array, whereas process.extractOne requires "ordinary" Python lists (of strings, to perform the match).

So probably you should change the instruction above to:

wrong_names=df['Country'].dropna().values.tolist()

The same for the other instruction.

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41