Reputation: 241
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?
Upvotes: 1
Views: 48
Reputation: 863781
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
Upvotes: 3