coder 007
coder 007

Reputation: 1

Impute missing categorical data in pandas dataframe [python]

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.

Data

Upvotes: 0

Views: 123

Answers (1)

piRSquared
piRSquared

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]

enter image description here

Upvotes: 1

Related Questions