Python: Split a String Field into 3 Separate Fields using Lambda

Viewed 5151

I have a Python data frame which includes a column called "SEGMENT". I want to break the column up into three columns. Please see my desired output highlighted in yellow.

enter image description here

Below is the code I have tried. Unfortunately I can't even get the first replace statement to work. The : does not get replaced by -. Any help is greatly appreciated!

df_stack_ranking['CURRENT_AUM_SEGMENT'] = df_stack_ranking['CURRENT_AUM_SEGMENT'].replace(':', '-')

s = df_stack_ranking['CURRENT_AUM_SEGMENT'].str.split(' ').apply(Series, 1).stack()

s.index = s.index.droplevel(-1)

s.name = 'SEGMENT'

df_stack_ranking.join(s.apply(lambda x: Series(x.split(':'))))
5 Answers
Related