I am trying to replace the all row value with which contains particular substring. For example: In string 'up to 20 Years ÃÂ (Child, Adult)', I am trying to search substring '(Child' and replace all its occurrence with only 'Child'
Asked
Active
Viewed 59 times
-1
-
1So you want to remove "(" ? What is the expected result? – jose_bacoy Oct 24 '19 at 18:26
-
1Looks like you are looking to create a regex, but do not know where to get started. Please check [Reference - What does this regex mean](https://stackoverflow.com/questions/22937618) resource, it has plenty of hints. Once you get some expression ready and still have issues with the solution, please edit the question with the latest details and we'll be glad to help you fix the problem. – Wiktor Stribiżew Oct 24 '19 at 20:07
3 Answers
0
You can use mask
series = series.mask(series.str.contains('(Child'), 'Child')

Benoit de Menthière
- 713
- 4
- 13
0
If you're working with a pandas dataframe could look at all values in a column and ask if each entry has the string of interest and then replace those rows:
# Example df
df = pd.DataFrame([{'a': 'This contains child', 'b': 1}, {'a': 'this does not', 'b': 5}])
rows_with_child = df.a.str.contains('child')
df.loc[rows_with_child, 'a'] = 'child'

Michael Silverstein
- 1,653
- 15
- 17
0
Here's an example from pandas:
import pandas as pd
pd.Series(['f.o', 'fuz', np.nan]).str.replace('f.', 'ba', regex=False)

Elliot Henry
- 65
- 6