Pandas: Create missing combination rows with zero values

Use pivot , then stack

df.pivot(*df.columns).fillna(0).stack().to_frame('values').reset_index()
Out[564]: 
   col1 col2  values
0     1    A     2.0
1     1    B     4.0
2     1    C     0.0
3     2    A     6.0
4     2    B     8.0
5     2    C    10.0

Leave a Comment