Pandas str.split without stripping split pattern Pandas str.split without stripping split pattern pandas pandas

Pandas str.split without stripping split pattern


You could split by using a positive look ahead. So the split point will be the point just before the postivie look ahead expression.

import pandas as pdserie = pd.Series(['this#is#a#test', 'another#test'])print(serie.str.split('(?=#)', expand=True))

OUTPUT

         0      1     2      30     this    #is    #a  #test1  another  #test  None   None


Try str.split('(#[a-z]+)', expand=True)

Ex:

serie = pd.Series(['this#is#a#test', 'another#test'])print(serie.str.split('(#[a-z]+)', expand=True)


Just simply add it at each line:

In [1]: import pandas as pdIn [2]: serie = pd.Series(['this#is#a#test', 'another#test'])In [3]: serie.str.split('#', expand=True) + '#'Out[3]:          0      1    2      30     this#    is#   a#  test#1  another#  test#  NaN    NaNIn [4]: '#' + serie.str.split('#', expand=True)Out[4]:          0      1    2      30     #this    #is   #a  #test1  #another  #test  NaN    NaN