Skip to content
Advertisement

Replace text between two DataFrames in Pandas

I’m trying to replace/ablate terms within DataFrame if they appear within another DataFrame.

For example, below is the replace DataFrame that includes an ablate column and a replace column. I’m looking to replace any word that appears within the ablate row with whatever is in the replace row.

JavaScript

For example, if given the below DataFrame with comment text…

JavaScript

applying replace_df would result in the below:

JavaScript

Many thanks in advance!

Advertisement

Answer

A dict would be a more natural data structure for the replacements. With that in mind, how about:

JavaScript

which returns:

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