0

I have a pandas dataframe db3 where I'm trying to come up with a function that could be applied to each row in ColA that would replace oldStrings (if found) with a newString. The output would be shown in new ColB. Here is where I've gotten so far:

oldString = [' ','-']
newString = '_'

def replaceCharacters(x):
    return x.replace(oldString,newString)

for index, row in db3.iterrows():
    if any(x in row['colA'] for x in oldString):
        db3['ColB'] = db3.apply(lambda row: replaceCharacters(db3['ColA']), axis=1)

This is throwing an error "wrong number of items passed.." Any ideas what I could be doing wrong or how to accomplish this in python?

whada
  • 306
  • 3
  • 15
  • @Wen, possible [additional dup](https://stackoverflow.com/questions/33157643/pandas-replace-erase-different-characters-from-strings) to clear up the list issue.. – DJK Aug 27 '18 at 13:58
  • DJK thanks, I'm getting the TypeError: unhashable type: 'list' though. am I missing anything – whada Aug 27 '18 at 14:02
  • Look at the last question in the comments, replace your list `oldString` as an or statement `oldString = '\s{1}|-'` – DJK Aug 27 '18 at 14:15
  • 1
    Perfect - that worked now. Thanks! – whada Aug 27 '18 at 14:20

0 Answers0