title: Including missing combination of values based on a group of grouped data
body: <p>I am expanding on earlier thread: <a href="https://stackoverflow.com/questions/42854801/including-missing-combinations-of-values-in-a-pandas-groupby-aggregation">Including missing combinations of values in a pandas groupby aggregation</a> </p>
  
  <p>In above thread, the accepted answer computes all possible combinations for the grouping variable. In this version, I'd like to compute combinations based on group of groups. </p>
  
  <p>Let's take an example.</p>
  
  <p>Here's input dataframe:</p>
  
  <p><a href="https://i.stack.imgur.com/5Zwot.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5Zwot.png" alt="enter image description here"></a></p>
  
  <p>Here, one group is <code>[Year,Quarter]</code> i.e. </p>
  
  <pre><code>Year  Quarter
  2014  Q1
  2015  Q2
  2015  Q3
  </code></pre>
  
  <p>Another set of group is <code>Name</code>: </p>
  
  <pre><code>Name
  Adam
  Smith
  </code></pre>
  
  <p>Now, I want to apply <code>groupby</code> and <code>sum</code> such that missing values of the combination of above groups is detected as <code>NaN</code></p>
  
  <p>Here's sample output:</p>
  
  <p><a href="https://i.stack.imgur.com/3bcZk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3bcZk.png" alt="enter image description here"></a></p>
  
  <p>I'd appreciate any help.</p>
  
  <hr>
  
  <p>Here's sample <code>input</code> and <code>output</code> in <code>dict</code> format:</p>
  
  <pre><code>input=
  {'Year': {0: 2014, 1: 2014, 2: 2015, 3: 2015, 4: 2015},
   'Quarter': {0: 'Q1', 1: 'Q1', 2: 'Q2', 3: 'Q2', 4: 'Q3'},
   'Name': {0: 'Adam', 1: 'Smith', 2: 'Adam', 3: 'Adam', 4: 'Smith'},
   'Value': {0: 2, 1: 3, 2: 4, 3: 5, 4: 5}}
  
  output=
  {'Year': {0: 2014, 1: 2014, 2: 2015, 3: 2015, 4: 2015, 5: 2015},
   'Quarter': {0: 'Q1', 1: 'Q1', 2: 'Q2', 3: 'Q2', 4: 'Q3', 5: 'Q3'},
   'Name': {0: 'Adam', 1: 'Smith', 2: 'Adam', 3: 'Smith', 4: 'Smith', 5: 'Adam'},
   'Value': {0: 2.0, 1: 3.0, 2: 9.0, 3: nan, 4: 5.0, 5: nan}}
  </code></pre>
  
  <p><strong>Clarification:</strong></p>
  
  <p>I am looking for a method without doing melt and cast. i.e. without playing around with long and wide format.</p>