/ python

pandas DataFrame Replace

Source: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.replace.html

The pandas DataFrame description of the replace function makes you think that it would search for a string and replace it (my poor assumption)

However, this is incorrect. It will search for an exact match of the to_replace value within a whole cell of the DataFrame.

So if a cell contained My Blog and you did df.replace(to_replace='My', value='Your', inplace=True), it won't do anything because there is no exact match for My.

You should instead do df.replace(to_replace='My', value='Your', regex=True)

This will now use regex to search for the exact string My and replace it with Your.

pandas DataFrame Replace
Share this