title: While unstacking, getting error 'ValueError: Index contains duplicate entries, cannot reshape'
body: <p>I have a dataframe on which I need to apply condition based on same values of rows in few columns and update other columns.</p>
  <p>Input Dataframe initialization:</p>
  <pre><code>df = pd.DataFrame({
      'exp':['1y','1y','1y','1y','1y','1y','1y','1y','1y',],
      'mat':['1y','1y','1y','2y','2y','2y','1y','1y','1y',],
      'reg':['in', 'in', 'in', 'in', 'in', 'in','in','in','in'],
      'con':['5w','5s','5c','5w','5s','5c','10w','10s','10c'],
      'pcon':['w','s','c','w','s','c','w','s','c'],
      'val':[2.5,0,-2.5,2.5,0,-2.5,5,0,-5],
      'rs':[6, 10, 4, 12, 30, 6,30,25,10]
  })
  df
  </code></pre>
  <p>Input DataFrame:</p>
  <pre><code>    exp mat reg con pcon    val   rs
  0   1y  1y  in  5w  w       2.5    6
  1   1y  1y  in  5s  s       0.0   10
  2   1y  1y  in  5c  c      -2.5    4
  3   1y  2y  in  5w  w       2.5   12
  4   1y  2y  in  5s  s       0.0   30
  5   1y  2y  in  5c  c      -2.5    6
  6   1y  1y  in  10w w       5.0   30
  7   1y  1y  in  10s s       0.0   25
  8   1y  1y  in  10c c      -5.0   10
  </code></pre>
  <p>Expected Output DataFrame</p>
  <pre><code>    exp mat reg con pcon    val  rs
  0   1y  1y  in  5w  w       2.5   5     # (6+4)/2 from input
  1   1y  1y  in  5s  s       0.0  10     # Same as input
  2   1y  1y  in  5c  c      -2.5   1     # (6-4)/2 from input
  3   1y  2y  in  5w  w       2.5   9 
  4   1y  2y  in  5s  s       0.0  30
  5   1y  2y  in  5c  c      -2.5   3
  6   1y  1y  in  10w w       5.0  20
  7   1y  1y  in  10s s       0.0  25
  8   1y  1y  in  10c c      -5.0  10
  </code></pre>
  <p>Multiple conditions are based on rows and columns:</p>
  <ol>
  <li><p>For same values in rows of columns<br />
  like for all 3 rows having exp=1y, mat=1y, reg=in.</p>
  <p>a. Update column 'rs' based on col 'con'.</p>
  <pre><code>  if 'con' == 5w, then rs = (current_rs + rs(con == 5c)) / 2  i.e (6+4)/2 
    if 'con' == 5s, then no changes
    if 'con' == 5c, then rs = (rs(con == 5w) - current_rs)) / 2 i.e (6-4)/2
    (Similarly for group of 10w, 10s, 10c in above example, in my dataset I have values till 100000w, 100000s, 100000c)
  </code></pre>
  </li>
  <li><p>Apply same conditions from group of 3 rows, where exp=1y, mat=2y, reg=in</p>
  </li>
  </ol>
  <p>My dataset is quite large and there any many other regions and many more combinations of 'exp', 'mat' and 'reg' columns.</p>
  <p>When I'm trying below code,
  If I unstack the data so that 'pcon' becomes the column, I'm getting below error</p>
  <pre><code>&quot;ValueError: Index contains duplicate entries, cannot reshape&quot; error
  </code></pre>
  <p>Code:</p>
  <pre><code>tmp = df.set_index(['exp','mat','reg', 'pcon']).unstack() ## Failing here
  tmp[('rs','w')],tmp[('rs','c')] = ((tmp[('rs','w')] + tmp[('rs','c')])/2,
                                    (tmp[('rs','w')] - tmp[('rs','c')])/2)
  tmp = tmp.stack().reset_index()
  </code></pre>
  <p>If I unstack the data so that 'con' becomes the column, I'm getting below error</p>
  <pre><code>KeyError: ('rs', 'w')
  </code></pre>
  <p>Code</p>
  <pre><code>tmp = df.set_index(['exp','mat','reg', 'con']).unstack() ## Failing here
  </code></pre>