How to replace a tag %%article%% by letter a

Viewed 22

I have this dataframe:

pd.DataFrame({'text': ['I have %%article%% car', '%%article%%fter dawn', 'D%%article%%t%%article%%Fr%%article%%me']})

I am trying to replace %%article%% by letter a to have as output:

pd.DataFrame({'text': ['I have a car', 'after dawn', 'DataFrame']})

I tried to create a dict ={'%%article%%':'a'} and then:

df['text'] = df['text'].map(dict)

But it's not working, it returns NaN

2 Answers

When passing a dict to Series.map, it uses table lookup so that only elements that exactly match '%%article%%' will be replaced by 'a'.

An example from doc:

>>> s = pd.Series(['cat', 'dog', np.nan, 'rabbit'])
>>> s
0      cat
1      dog
2      NaN
3   rabbit
>>> s.map({'cat': 'kitten', 'dog': 'puppy'})
0   kitten
1    puppy
2      NaN
3      NaN

An element with something like 'ccat' will not be replaced. Instead, you can use a function to replace them:

>>> df = pd.DataFrame({'text': ['I have %%article%% car', '%%article%%fter dawn', 'D%%article%%t%%article%%Fr%%article%%me']})
>>> df.text = df.text.map(lambda i: i.replace('%%article%%', 'a'))
>>> df
           text
0  I have a car
1    after dawn
2     DataFrame

But the better is probably Series.replace:

>>> df.replace('%%article%%', 'a')
           text
0  I have a car
1    after dawn
2     DataFrame

Use:

df['text'].str.replace('%%article%%', 'a')

Output:

0    I have a car
1      after dawn
2       DataFrame
Name: text, dtype: object
Related