Reputation: 1
Below image has the sample data. Since 50 is missing for Col2 in the 3rd sub group, I would like to insert a new row with Col1=3 and Col2=50.
Upvotes: 0
Views: 123
Reputation: 294536
bad way
cols = ['Col1', 'Col2']
df.set_index(
cols, drop=False
).Col1.unstack().stack(dropna=False).reset_index()[cols]
better way
ref = df.stack().groupby(level=1).unique()
pd.MultiIndex.from_product(
ref.tolist(), names=ref.index
).to_series().reset_index().iloc[:, :-1]
Upvotes: 1