Home > Mobile >  Regex to cut last one or two digits from string in dataframe
Regex to cut last one or two digits from string in dataframe

Time:07-12

I have data like that:
App2Aa1
Cry10Aa4
Cry2Ac10

I want output like:
App2Aa Cry10Aa
Cry2Ac

I tried regex

data['id'] = data['id'].str.replace(r'[a-zA-Z][0-9][0-9][a-zA-Z]\d ', r'[a-zA-Z] [0-9][0-9]\w\w', regex=False)

My case is to Cut one or two digits and make record shorter

CodePudding user response:

I think you want to just strip all trailing digits. If so, then we can use str.replace with the regex pattern \d $:

data["id"] = data["id"].str.replace(r'\d $', '', regex=True)
  • Related