I have a data frame that looks like below. Data type of Output is string.
ID Output
1 ab 1, bc 2, ac 5, at 0, abc 0
2 ab 0, ac 5, at 0
3 ac 5, bc 0, atn 0
As you can see, in row2, bc is skipped while the overall order stays the same. However, in row3, the order differs. How do I first insert the missing categories and then reorder the strings in the data frame? In other words, how may I get an intermediate data frame that looks like this:
ID Output
1 ab 1, bc 2, ac 5, at 0, abc 0, atn
2 ab 0, bc, ac 5, at 0, abc, atn
3 ab, bc 0, ac 5, at, abc, atn 0
So eventually I can perform the below operation:
x = df['Output'].str.split(",",expand=True,)
x.columns = x.iloc[0, :].str.extract(r"^(.*)\s+")[0]
x = x.apply(lambda x: x.str.replace(r"^(.*\s+)", ""))
df=pd.concat([df, x], axis=1)
To reach this ideal data frame:
ID ab bc ac at abc atn
1 1 2 5 0 0 None
2 0 None 5 0 None None
3 None 0 5 None None 0