I have the following df:
df = pd.DataFrame({'Category': ['root:catA', 'root:catA:catB'], 'Age':[32, 34]})
Now I want to split the column Category by : into, in total 4, columns. Since there will be less columns because row0 has will result in 4 filled columns, row1 in 5 filled columns, the rest of the columns should be filled with NaN.
Expected output:
df = pd.DataFrame({'Category': ['root:catA', 'root:catA:catB'], 'Age':[32, 34], 'Cat1':['root', 'root'], 'Cat2':['catA', 'catA'], 'Cat3':['NaN', 'CatB'], 'Cat4':['NaN', 'NaN']})
I tried the following:
df[['cat1','cat2', "cat3", "cat4"]] = df.Category.str.split(":",expand=True)
But I am getting a
ValueError: Columns must be same length as key