0

I have this function here which I use to replace the a string like this 'ABCDe.CO' with 'ABCD-E.CO' in a pandas dataframe.

I don't understand how the group(0) part works, I can't find any documentation on it. Could someone explain to me what this function does or where I can read up on it?

(df.loc[df.country.eq('ST'), 'ticker'].str.replace('([a-z])', lambda x: '-'+x.group(0).upper()))

anarchy
  • 3,709
  • 2
  • 16
  • 48
  • `x` is the match data object found with a `([a-z])` regex. The whole matched text is turned to uppercase and prepended with `-`. Read [`re.sub` docs](https://docs.python.org/3/library/re.html#re.sub), you will find *If repl is a function, it is called for every non-overlapping occurrence of pattern. The function takes a single match object argument, and returns the replacement string.* – Wiktor Stribiżew Jun 04 '20 at 18:48
  • I tried doing `'ABCDe.CO'.replace('([a-z])',lambda x: '-'+x.group(0).upper())` but I get an error, it only seems to work in pandas, any idea why ? – anarchy Jun 04 '20 at 18:49
  • Because you need a regex replacement, `re.sub` – Wiktor Stribiżew Jun 04 '20 at 18:50

0 Answers0