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?

4
  • @Wen, possible additional dup to clear up the list issue.. Commented Aug 27, 2018 at 13:58
  • DJK thanks, I'm getting the TypeError: unhashable type: 'list' though. am I missing anything Commented Aug 27, 2018 at 14:02
  • Look at the last question in the comments, replace your list oldString as an or statement oldString = '\s{1}|-' Commented Aug 27, 2018 at 14:15
  • 1
    Perfect - that worked now. Thanks! Commented Aug 27, 2018 at 14:20

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.