4 of 4
added 236 characters in body
Andrew
  • 73
  • 1
  • 9

str.replace unless string is followed by certain text

Revision of prior question:

How can I replace all ", " (i.e. comma then space) with "_" except when ", " (comma then space) is followed by the word "LLC" or "Inc" (then do nothing)?

I want to change:

  1. "TEXAS ENERGY MUTUAL, LLC, BOBBY GILLIAM, STEVE PEREIRA and ANDY STITT"
  2. "Grape, LLC, Andrea Gray, Jack Smith"
  3. "Stephen Winters, Apple, pear, Inc, Sarah Smith"

To this:

  1. "TEXAS ENERGY MUTUAL, LLC_BOBBY GILLIAM_STEVE PEREIRA_ANDY STITT"
  2. "Grape, LLC_Andrea Gray_Jack Smith"
  3. "Stephen Winters_Apple_pear, Inc_Sarah Smith"

I thought it would start with some variation of the code below but I cannot figure out the except conditions.

df['Column_Name'] = df['Column_Name'].str.replace(', ','_') Cheers!

Andrew
  • 73
  • 1
  • 9