Jun-22-2020, 08:24 AM
I have successfully solved this set by introducing a function which searches stings in the df using a list, that initially joins all the elements I would want to pass through the data frame and then returns the value on a new column of the element that matched in the df. The code is as below:
iddf = pd.read_csv('idd.csv', names=['country', 'prefix', 'rate', '1']) iddf = iddf.drop(['1'], axis=1) iddf = iddf.astype(str) iddf['prefix'] = '00' + iddf['prefix'] prefix = iddf['prefix'].values.tolist() #Developing Pattern for IDD Regex Search pattern = '|'.join(prefix) def pattern_searcher(search_str:str, search_list:str): search_obj = re.match(search_list, search_str) if search_obj : return_str = search_str[search_obj.start(): search_obj.end()] else: return_str = 'NaN' return return_str df['prefix'] = df['Called number'].apply(lambda x: pattern_searcher(search_str=x, search_list=pattern))The above code returns a new column called prefix which first matches prefix to the column 'Called number' and then returns the prefix which was matched. Using this prefix I could then map a dictionary and get more values if I want.