Python: How to split and re-join first and last item in series of strings
You can use pandas vectorized split
and then apply
method:
import pandas as pds = pd.Series(["1, 2, 6, 7, 6", "1, 3, 7, 9, 9", "1, 1, 3, 5, 6", "1, 2, 7, 7, 8", "1, 4, 6, 8, 9", "1, 2, 6, 8, 8"])s_split = s.str.split(',')r = s_split.apply(lambda r:','.join([r[0], r[-1]]))r0 1, 61 1, 92 1, 63 1, 84 1, 95 1, 8dtype: object