title: What is the most efficient way to fill missing values in this data frame?
body: <p>I have the following pandas dataframe :</p>
  <pre><code>df = pd.DataFrame([
      ['A', 2017, 1],
      ['A', 2019, 1],
      ['B', 2017, 1],
      ['B', 2018, 1],
      ['C', 2016, 1],
      ['C', 2019, 1],
  ], columns=['ID', 'year', 'number'])
  </code></pre>
  <p>and am looking for the most efficient way to fill the missing years with a default value of 0 for the column <code>number</code></p>
  <p>The expected output is:</p>
  <pre><code>  ID  year  number
  0  A  2017       1
  1  A  2018       0
  2  A  2019       1
  3  B  2017       1
  4  B  2018       1
  5  C  2016       1
  6  C  2017       0
  7  C  2018       0
  8  C  2019       1
  </code></pre>
  <p>The dataframe that I have is relatively big, so I am looking for an efficient solution.</p>
  <p>Edit:</p>
  <p>This is the code that I have so far:</p>
  <pre><code>min_max_dict = df[['ID', 'year']].groupby('ID').agg([min, max]).to_dict('index')
  
  new_ix = [[], []]
  for id_ in df['ID'].unique():
      for year in range(min_max_dict[id_][('year', 'min')], min_max_dict[id_][('year', 'max')]+1): 
          new_ix[0].append(id_)
          new_ix[1].append(year)
  
  
  df.set_index(['ID', 'year'], inplace=True)
  df = df.reindex(new_ix, fill_value=0).reset_index()
  </code></pre>
  <p>Result</p>
  <pre><code>  ID  year  number
  0  A  2017       1
  1  A  2018       0
  2  A  2019       1
  3  B  2017       1
  4  B  2018       1
  5  C  2016       1
  6  C  2017       0
  7  C  2018       0
  8  C  2019       1
  </code></pre>