Home > other >  Remove characters after "\" in a Dataframe
Remove characters after "\" in a Dataframe

Time:12-01

How can I remove all the characters in the column "Player" after the names? For example: Max Aarons\774cf58b. I only want to have the first part: Max Aarons and delete the rest "\774cf58b"

Table

CodePudding user response:

Try using pandas.DataFrame.apply()

def remove_after_slash(string):
    if '/' not in string: return string
    return string.split('/')[0]

df['names'].apply(remove_after_slash)

CodePudding user response:

Thanks a lot -- that worked. I just had to add a second backslash.

def remove_after_slash(string):
    if '\\' not in string: return string
    return string.split('\\')[0]

df['Player'] = df['Player'].apply(remove_after_slash)
  • Related