Home > Software design >  How to split text with certain punctuation with specific exception
How to split text with certain punctuation with specific exception

Time:05-13

To make easier to analyze I need to split text to folder and sub folder that have separation > but not in every folder like this

Here's my input

        Defaults    
205     News and Media
206     Vehicles > Motorcycles
207     Vehicles > Motorsports
208     Vehicles > Other Vehicles

Here's my expected output

        Defaults                       Folder             Sub Folder
205     News and Media                 News and Media     News and Media
206     Vehicles > Motorcycles         Vehicles           Motorcycles
207     Vehicles > Motorsports         Vehicles           Motorsports
208     Vehicles > Other Vehicles      Vehicles           Other Vehicles

CodePudding user response:

You could use regex replacement logic here:

df["Folder"] = df["Defaults"].str.replace(r'\s*>.*', '', regex=True)
df["Sub Folder"] = df["Defaults"].str.replace(r'.*>\s*', '', regex=True)
  • Related