Skip to content Skip to sidebar Skip to footer

Pandas: Dataframe.replace() With Regex

I have a table which looks like this: df_raw = pd.DataFrame(dict(A = pd.Series(['1.00','-1']), B = pd.Series(['1.0','-45.00','-']))) A B 0 1.00 1.0 1 -1 -45.

Solution 1:

Your regex is matching on all - characters:

In[48]:
df_raw.replace(['-','\*'], ['0.00','0.00'], regex=True)

Out[48]:
       AB01.001.010.0010.0045.002NaN0.00

If you put additional boundaries so that it only matches that single character with a termination then it works as expected:

In[47]:
df_raw.replace(['^-$'], ['0.00'], regex=True)

Out[47]:
      AB01.001.01-1-45.002NaN0.00

Here ^ means start of string and $ means end of string so it will only match on that single character.

Or you can just use replace which will only match on exact matches:

In[29]:

df_raw.replace('-',0)
Out[29]:
      AB01.001.01-1-45.002NaN0

Post a Comment for "Pandas: Dataframe.replace() With Regex"