County |
---|
Davis County |
Ark County |
Clay County Party |
I want to delete County from the County column. This is what I have tried so far.
def county(df):
df['County'].replace(['\bCounty\b'], '', regex = True, inplace = True)
Use a raw string by prefixing with r
:
def county(df):
df['County'].replace([r'\bCounty\b'], '', regex = True, inplace = True)
The problem is that normally, \b
is interpreted as a backspace character. You can similarly escape this character by writing \\b
instead, but it is easier here to just use the raw string.
You also don't have to pass your regex inside of a list.
Try it like this:
def county(df):
df['County'].replace(' County', '', regex = True, inplace = True)