What are the reasons why are regex replacment doesn’t work? I have tried ensuring no excess spaces.
JavaScript
x
8
1
df.column
2
0 Test_With_Him
3
1 And_another option with him
4
2 and_another reason with her
5
6
replacement = {'_':' ',Test With': 'test with', 'and another': 'And another& AND'}
7
df['column'] = df.column.replace('s+',' ' , regex=True).str.strip().replace(replacement, regex=True)
8
When I do df.loc[df['column']=="and another reason with her"]
nothing has changed.
Advertisement
Answer
Please use df.replace(regex=dict)
JavaScript
1
11
11
1
df=pd.DataFrame({'test':["Test With Him","And Another option with him",'and another reason with her']})
2
3
replacement = {r'Test With': 'test with', r'And Another': 'And another& AND'}
4
5
df=df.replace(regex=replacement)
6
7
test
8
0 test with Him
9
1 And another& AND option with him
10
2 and another reason with her
11