[SOLVED] Remove a particular character ("-") and characters after it in a column of dataframe


From my dataframe column, I want to remove the "-" character and the letters following it.

For eg.

Input dataframe

Name    Subset
Apple   AP-, GP-
Bat     BT-01A, KL
Cat     CT-L, OK-01

Output desired

Name    Subset
Apple   AP,GP
Bat     BT,KL
Cat     CT,OK    


You can use -[^,]* to match everything from - till a comma, here we use negation [^,] to match a non comma character:

df['Subset'] =  df.Subset.str.replace('-[^,]*', '', regex=True)

    Name  Subset
0  Apple  AP, GP
1    Bat  BT, KL
2    Cat  CT, OK

Answered By – Psidom

Answer Checked By – Dawn Plyler (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *