Home > Net >  replace word that are in a list for every row with Pandas
replace word that are in a list for every row with Pandas

Time:01-06

I have this list of words:

word = ['dog', 'cat', 'fish']

And this dataframe:

dfa = pd.DataFrame({'a': ['big dog', 'good food', 'dog and cat', 'little fish', 'intense light', 'nice cat']})

I'd like to replace the text in my word list when it appears in my dataframe column. I've tried doing this:

for row in dfa['a']:
   for w in word:
       if w in row:
           dfa['a'] = dfa['a'].replace(w, "animal")

I'm not getting any error. But it just doesn't work

This is what I'd like to have in the end:

    a
big animal
good food
animal and animal
little animal
intense light
nice animal

How can I achieve this?

CodePudding user response:

You can just iterate over the words to replace instead of each row for each word. Note the regex argument, which will allow finding and replacing of substrings. You can find the more information in the docs for pandas.DataFrame.replace here.

import pandas as pd


word = ['dog', 'cat', 'fish']

dfa = pd.DataFrame({'a': ['big dog', 'good food', 'dog and cat', 'little fish', 'intense light', 'nice cat']})

for w in word:
    dfa = dfa.replace(w, "animal", regex=True)

print(dfa)

This outputs

                   a
0         big animal
1          good food
2  animal and animal
3      little animal
4      intense light
5        nice animal

CodePudding user response:

import pandas as pd

dfa = pd.DataFrame({'a': ['big dog', 'good food', 'dog and cat', 'little fish', 'intense light', 'nice cat']})
word = ['dog', 'cat', 'fish']

out = dfa["a"].str.replace(f"({'|'.join(word)})", "animal", regex=True)
0           big animal
1            good food
2    animal and animal
3        little animal
4        intense light
5          nice animal
Name: a, dtype: object

CodePudding user response:

Use

   dfa['a'] = dfa['a'].str.replace(w, "animal")
  • Related