Skip to content Skip to sidebar Skip to footer

Importing Txt File To Replace Certain Strings In A Dataframe (pandas)

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

Solution 1:

I mean something simple as this:

import pandas as pd

data = '''\
Starbucks,Coffee
Tullys,Coffee
Seattles Best,Coffee'''

# Create a map from a file 
m = pd.read_csv(pd.compat.StringIO(data), header=None, index_col=[0])[1]

And then:

df['Utterance'] = df['Utterance'].replace(m, regex=True).str.lower()

Post a Comment for "Importing Txt File To Replace Certain Strings In A Dataframe (pandas)"