Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TypeError: unhashable type: 'Series' when attempting to use the replace function

I am having problems trying to remove substrings from a column that are contained within the string of another column:

df = pd.DataFrame(
    {
        "substring": ["This", "sentence."],
        "id": [1, 1],
        "string": ["This is a sentence.", "This is another sentence."],
    }
)
df["replaced"] = df["string"].str.replace(df["substring"], "")

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Use list comprehension:

df["replaced"] = [x.replace(y, "") for x,  y in zip(df["string"], df["substring"])]


print (df)
   substring  id                     string          replaced
0       This   1        This is a sentence.    is a sentence.
1  sentence.   1  This is another sentence.  This is another 
    
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading