Skip to content
Advertisement

Why doesn’t str.replace replace ALL values in selected pandas dataframe column?

I’m working on a huge file that has names in columns that contain extraneous values (like the “|” key) that I want to remove, but for some reason my str.replace function only seems to apply to some rows in the column.

My column in the dataframe summary looks something like this:

JavaScript

As you can see, some columns are already how i want them to be, only containing the name “test #”, but some have “test|” in front, which I want removed.

My function to remove them is like this:

JavaScript

It seems to work for most of the values, but when I check for pipes (“|”) in the dataframe (once i merged correction with summary), it says it finds 9330 of them:

JavaScript

Does anyone know why this is, and how i can fix it?

Advertisement

Answer

You were on the right track. Replace raw string as follows

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement