title: How to add a nested column to a 3D Pandas DataFrame?
body: <p>New to Pandas, not very sure how the 3D DataFrame works. My dataframe, called <code>'new'</code> looks like this:</p>
  
  <pre><code>     unique    cat    numerical   
            a  b   c  d         e  f
     0      0  1   2  3         4  5
     1      0  1   2  3         4  5
  </code></pre>
  
  <p>I want to insert column <code>'z'</code> so that it ends up like this:</p>
  
  <pre><code>     unique       cat    numerical   
            a  b  z   c  d         e  f
     0      0  1  9   2  3         4  5
     1      0  1  9   2  3         4  5
  </code></pre>
  
  <p>I successfully made a new column after slicing out <code>'unique'</code> from my dataframe:</p>
  
  <p>Doing this:</p>
  
  <pre><code>new_column = new.loc[:,'unique'].assign(z=pd.Series([9,9]).values)
  </code></pre>
  
  <p>Gets me this:</p>
  
  <pre><code>   a  b  z
  0  0  1  9
  1  0  1  9
  </code></pre>
  
  <p>However I have no idea how to put it back into the dataframe. I tried:</p>
  
  <pre><code>new['unique'] = new_column
  </code></pre>
  
  <p>But I've since found out that it just tries to replace all the values in all the rows and columns found under <code>'unique'</code>, like this:</p>
  
  <pre><code>new['unique'] = 'a'
  </code></pre>
  
  <p>Gets:</p>
  
  <pre><code>  unique    cat    numerical   
         a  b   c  d         e  f
  0      a  a   2  3         4  5
  1      a  a   2  3         4  5
  </code></pre>
  
  <p>And using <code>.loc</code> gets this instead:</p>
  
  <pre><code>  unique     cat    numerical   
         a   b   c  d         e  f
  0    NaN NaN   2  3         4  5
  1    NaN NaN   2  3         4  5
  </code></pre>
  
  <p>Here's my full code:</p>
  
  <pre><code>import pandas as pd
  import numpy as np
  
  data=[[0,1,2,3,4,5],[0,1,2,3,4,5]]
  datatypes=np.array(['unique','unique','cat','cat','numerical','numerical'])
  columnnames=np.array(['a','b','c','d','e','f'])
  new = pd.DataFrame(data=data, columns=pd.MultiIndex.from_tuples(zip(datatypes,columnnames)))
  print('new: ')
  print(new)
  
  new_column = new.loc[:,'unique'].assign(z=pd.Series([9,9]).values)
  print('\nnew column:')
  print(new_column)
  
  new.loc[:,'unique'] = new_column
  print('\nattempt 1:')
  print(new)
  
  new['unique'] = new_column
  print('\nattempt 2:')
  print(new)
  </code></pre>