1

I am trying to do a replace across one column of a pandas dataframe like the below.

From:

a          b
house      ho
cheese     ee
king       ng

To:

a        b
use      ho
chse     ee
ki       ng

My attempt is to use:

df['a'] = df['a'].str.replace(df['b'], "")

but I get TypeError: 'Series' objects are mutable, thus they cannot be hashed

I have done it by iterating row by row across the dataframe but its 200,000 rows so would take hours. Does anyone know how I can make this work?

0

1 Answer 1

3

Because performance is important here is possible use list comprehension with replace for replace per rows:

df['a'] = [a.replace(b, "") for a, b in df[['a','b']].values]

Another solution is slowier with DataFrame.apply:

df['a'] = df.apply(lambda x: x.a.replace(x.b, ""), axis=1)

print (df)
      a   b
0   use  ho
1  chse  ee
2    ki  ng
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.