Home > front end >  Remove a particular character ("-") and characters after it in a column of dataframe
Remove a particular character ("-") and characters after it in a column of dataframe

Time:03-06

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    

CodePudding user response:

dataFrame = dataFrame.str.replace('-.*', '')

CodePudding user response:

You can use -[^,]* to match everything from - till a comma, where [^,] matches any character that's not a comma:

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

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