import pandas as pd
import re
# Source data
df = pd.DataFrame( data={'A': ['abc: aaa 123: 111', 'edf: 111', 'ghi: a11 324: aaa', 'jkm: bn2 jsk: 1f4']})
df['A'] = df['A'].re.sub("(^|[:])\s*([a-zA-Z])", lambda p: p.group(0).upper(), s)
Result:
AttributeError: 'Series' object has no attribute 're'
I would like to upcase all letters after colon and space ": "
df = pd.DataFrame( data={'A': ['abc: Aaa 123: 111', 'edf: 111', 'ghi: A11 324: Aaa', 'jkm: Bn2 jsk: 1f4']})
CodePudding user response:
You can make your code work using:
def substitute(x):
return re.sub("([: ])\s*([a-zA-Z])", lambda p: p.group(0).upper(), x)
df['A'] = df['A'].apply(lambda x: substitute(x))
OUTPUT:
A
0 abc: Aaa 123: 111
1 edf: 111
2 ghi: A11 324: Aaa
3 jkm: Bn2 Jsk: 1f4
CodePudding user response:
Try this:
df['A'] = df.A.str.replace(r':\s [a-zA-Z]', lambda p: p.group(0).upper())
# output
A
0 abc: Aaa 123: 111
1 edf: 111
2 ghi: A11 324: Aaa
3 jkm: Bn2 jsk: 1f4