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:
pd.Series(",".join(s.tolist()).split(","))
, but I am looking for a generalizable solution.You can achieve this in two steps:
This can be achieved in a single line that combines the map with the flatmap:
s.str.split(',').explode()