2

so this is what i am stumbling now..

df = pd.DataFrame([{'a': 'newshoe', 'b': 'pig eat', 'c': 'dim sum'},
                   {'a': 'good', 'b': 'happy', 'c': 'nice'},
                   {'a': 'extreme', 'b': 'perfect', 'c': 'jeans'}])


    a           b        c
0  new shoe  pig eat  dim sum
1  good       happy    nice
2  extreme   perfect   jeans

How could i do for dropping out the string like ['ea','ce'] by using the pd.apply()? the following is what i expect:

    a           b        c
0  new shoe  pig t  dim sum
1  good       happy    ni
2  extreme   perfect   jns

The data is far bigger than i provide here though

sorry that i forgot i have more than a string which are needed to be replaced.. i update my question to be a list of strings to be replaceed instead of a simple string.

Tilei Liu
  • 155
  • 1
  • 6

2 Answers2

3

You can use df.replace with regex param set to True

df.replace('ea', '', regex=True)

         a        b        c
0  newshoe    pig t  dim sum
1     good    happy     nice
2  extreme  perfect      jns

EDIT:

If you've a list of word to replace then use this

words = ['ea','ce']
pat = '|'.join(words)
df.replace(pat, '', regex=True)
Ch3steR
  • 20,090
  • 4
  • 28
  • 58
2

DataFrame.apply by default goes through the columns. So you can use:

df.apply(lambda x: x.str.replace('ea', ''))
Paul H
  • 65,268
  • 20
  • 159
  • 136