I am trying to replace certain strings within a column in a dataframe using a txt file.
I have a dataframe that looks like the following.
coffee_directions_df
Utterance Frequency
Directions to Starbucks 1045
Directions to Tullys 1034
Give me directions to Tullys 986
Directions to Seattles Best 875
Show me directions to Dunkin 812
Directions to Daily Dozen 789
Show me directions to Starbucks 754
Give me directions to Dunkin 612
Navigate me to Seattles Best 498
Display navigation to Starbucks 376
Direct me to Starbucks 201
The DF shows utterances made by people and the frequency of utterances.
I.e., "Directions to Starbucks" was uttered 1045 times.
I understand that I can create a dictionary to replace strings such as "Starbucks", "Tullys", and "Seattles Best" such as the following:
# define dictionary of mappings
rep_dict = {'Starbucks': 'Coffee', 'Tullys': 'Coffee', 'Seattles Best': 'Coffee'}
# apply substring mapping
df['Utterance'] = df['Utterance'].replace(rep_dict, regex=True).str.lower()
However, my dataframe is pretty big, and I am wondering if there is a way where I can save rep_dict
as a .txt file, import the .txt file, and apply or map that the words in that txt file to coffee_directions_df.Utterance
Ultimately, I don't want to create a bunch of dictionaries within the script and be able to import a txt file that contains these dictionaries.
Thanks!!