Concatenate results of `apply` in pandas

I would like to apply a function to each element/row of a pandas Series/DataFrame and concatenate/stack the results into a single DataFrame.
E.g., I may start with a Series s = pd.Series(["a", "b,c", "d,e,f"]), and I would like to obtain as a final result res = pd.Series(["a", "b", "c", "d", "e", "f"])

A slow way of doing this would be:

res = []
for _, x in s.items():
    res.append(pd.Series(s.split(",")))
res = pd.concat(res, ignore_index=True)

I would like to explore the internal functionality of pandas. It seems that there should be a way of doing this by starting with something like s.apply(lambda x: x.split(",")) or s.str.split(), which gives a series of lists…

Remarks:

  • The simple example above could be actually solved using something like pd.Series(",".join(s.tolist()).split(",")), but I am looking for a generalizable solution.
  • Note that this is not a duplicate of Apply elementwise, concatenate resulting rows into a DataFrame (where apply is used in a generic sense rather than as a name of pandas function.)

>Solution :

You can achieve this in two steps:

  • split each string into a list of strings delimiting by comma
  • explode the column made up of lists of strings into a series of strings

This can be achieved in a single line that combines the map with the flatmap:

s.str.split(',').explode()

Leave a Reply