Try:
df['tumor-size'] = df['tumor-size'].replace("^'0-4'$", "'00-04'")
Answer from U13-Forward on Stack OverflowTry:
df['tumor-size'] = df['tumor-size'].replace("^'0-4'$", "'00-04'")
You can use $:
df = pd.DataFrame(data={'tumor-size': ['15-19', '35-39', '30-34', '25-29',
'40-44', '10-14', '0-4', '20-24',
'45-49', '50-54', '5-9']})
df['tumor-size'] = df['tumor-size'].str.replace(r'^0-4$', '00-04', regex=True)
Output:
tumor-size
0 15-19
1 35-39
2 30-34
3 25-29
4 40-44
5 10-14
6 00-04
7 20-24
8 45-49
9 50-54
10 5-9
python - Pandas str.replace Exact Match Repeating Character - Stack Overflow
regex - How to replace exact matches from a list of strings with special characters python? - Stack Overflow
python - Pandas: Using .replace in a Dataframe but only replace on an exact match - Stack Overflow
python - How to replace a exact matching string in Pandas string column - Stack Overflow
You can use a regex to do this replacement, looking for one of
- a
/followed by another/; - a
/at the start of the string; or - a
/at the end of the string
df['a'] = df['a'].str.replace(r'/(?=/)|^/|/$', '')
Output:
a
0 a/b
1
2 a/b
3 a/b/c
Method by split and stack groupby
df.a.str.split('/',expand=True).stack().loc[lambda x : x!=''].groupby(level=0).agg('/'.join).reindex(df.index,fill_value='')
0 a/b
1
2 a/b
3 a/b/c
dtype: object
In regex, some symbol have a meaning and trigger some functionality, when you want to explicitly match the symbol without triggering its function, you escape it.
Now re.escape is simply a method to avoid escaping a list of character manually.
instead of escaping (adding \) manually like this :
"\$\[\]\^"
You can simply do like the function you write.
pattern = "|".join(map(re.escape, "[
|\[|\]|\^"
To see what do your code, simply print p.
list_of_strings = ['can we: remove', 'with @#
#%@/}\p special characters!!!!','EXACT']
p = '|'.join(map(re.escape, list_of_strings))
print(p)
As you will see all characters have been escaped \.
Use for loop:
for i in list_of_strings:
df['name'] = df['name'].str.replace(i, '', regex=False)
print(df)
ID name
0 1 I have a %$$#form
1 2 the matches !#$#%$^%$&^(*&*)(*&)_&#
Maybe there is an easier way:
df.name.str.replace(list_of_strings[0],'', regex=False)\
.str.replace(list_of_strings[1],'', regex=False)\
.str.replace(list_of_strings[2],'', regex=False)
Output:
0 I have a %$$#form
1 the matches !#$#%$^%$&^(*&*)(*&)_&#
Name: name, dtype: object
Discovered the solution:
df['Brand'] = df['Brand'].str.replace(r'(?i)stack\b', r'stackoverflow')
This should do n would be useful if you have multiple replacements to do:
replace_dict = {'stack' : 'stackoverflow'}
replacement = {rf'\b{k}\b': v for k, v in replace_dict.items()}
df['Brand'] = df['Brand'].replace(replacement, regex=True)
You can escape the $ (it's special character in regex) or use regex=False:
data = {
"Column1": [
"Income recorded on books this year not included on Schedule K, lines 1 through 11 (itemize):",
"a Tax-exempt interest $ Statement #36",
"Statement #36",
],
"Column2": [254, 258, 356],
}
df = pd.DataFrame(data)
df["Column1"] = df["Column1"].str.replace(" $ Statement #36", "", regex=False)
print(df)
Prints:
Column1 Column2
0 Income recorded on books this year not include... 254
1 a Tax-exempt interest 258
2 Statement #36 356
The '$' character in regex is reserved so it needs to be escaped by using \$\.
I also set the regex flag to True.
import pandas as pd
data = {"Column1" : ["Income recorded on books this year not included on Schedule K, lines 1 through 11 (itemize):",
"a Tax-exempt interest $ Statement #36",
"Statement #36"],
"Column2" : [254, 258, 356]}
df = pd.DataFrame(data)
df['Key'] = df['Column1'].str.replace(r'\$\ Statement #36', '', regex=True)
print(df['Key'])
output of print(df['Key'])
0 Income recorded on books this year not include...
1 a Tax-exempt interest
2 Statement #36
Is there a way to match a list of strings exactly with the strings in a pandas column to filter out the ones that do not have?
Say, words = ['ab', 'ml']
df =
| data |
|---|
| 'example string ab' |
| 'absolute value' |
After filtering, I must get only the row with value 'example string ab' for it contains exact string 'ab' from the list 'words'.