0

I am using Pandas and I would like to split the following strings:

pd.DataFrame(data = ['String1 - String2',
                     'This > That',
                     'One <-> Two'], columns = ["Value"])
Index Value
0 String1 - String2
1 String1 > String2
2 One <-> Two

The string splitter could be - or >. How could I split the string such that I would get the following result:

Index Value String1 String2
0 String1 - String2 String1 String2
1 This > That This That
2 One <-> Two One Two

Thanks in advance

Winston
  • 1,308
  • 5
  • 16
  • 34
  • try re.split instead also possible duplicate https://stackoverflow.com/questions/4998629/split-string-with-multiple-delimiters-in-python – mj_codec Aug 30 '22 at 05:38

1 Answers1

3

Use Series.str.split with regex \s+ for spaces before separators - or >:

df[['String1','String2']] = df['Value'].str.split('\s+[->]\s+', expand=True)
print (df)
               Value  String1  String2
0  String1 - String2  String1  String2
1        This > That     This     That
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252