<p dir="auto">Lately I've been working with DataFrames with duplicate column names. I've found that if I have duplicate column names and there're different types of data in these columns, I cannot get values property from DataFrame (there're some other exceptions too, with iloc for example):</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content=" # no duplicates - ok

&gt;&gt;&gt; df = pd.DataFrame([range(2) + ['x'], range(2) + ['c']], columns=['a','b','c'])
&gt;&gt;&gt; df
   a  b  c
0  0  1  x
1  0  1  c
&gt;&gt;&gt; df.values
array([[0L, 1L, 'x'],
       [0L, 1L, 'c']], dtype=object)

# duplicate columns, same type - ok

&gt;&gt;&gt; df = pd.DataFrame([range(3), range(3)], columns=['a','b','b'])
&gt;&gt;&gt; df
   a  b  b
0  0  1  2
1  0  1  2
&gt;&gt;&gt; df.values
array([[0, 1, 2],
       [0, 1, 2]], dtype=int64)

# duplicate columns, diff types - error

&gt;&gt;&gt; df = pd.DataFrame([range(2) + ['x'], range(2) + ['c']], columns=['a','b','b'])
&gt;&gt;&gt; df
   a  b  b
0  0  1  x
1  0  1  c
&gt;&gt;&gt; df.values
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;C:\Python27\lib\site-packages\pandas-0.12.0-py2.7-win32.egg\pandas\core\frame.py&quot;, line 1748, in as_matrix
    return self._data.as_matrix(columns).T
  File &quot;C:\Python27\lib\site-packages\pandas-0.12.0-py2.7-win32.egg\pandas\core\internals.py&quot;, line 1517, in as_matrix
    mat = self._interleave(self.items)
  File &quot;C:\Python27\lib\site-packages\pandas-0.12.0-py2.7-win32.egg\pandas\core\internals.py&quot;, line 1548, in _interleave
    raise AssertionError('All items must be in block items')
AssertionError: All items must be in block items"><pre class="notranslate"><code class="notranslate"> # no duplicates - ok

&gt;&gt;&gt; df = pd.DataFrame([range(2) + ['x'], range(2) + ['c']], columns=['a','b','c'])
&gt;&gt;&gt; df
   a  b  c
0  0  1  x
1  0  1  c
&gt;&gt;&gt; df.values
array([[0L, 1L, 'x'],
       [0L, 1L, 'c']], dtype=object)

# duplicate columns, same type - ok

&gt;&gt;&gt; df = pd.DataFrame([range(3), range(3)], columns=['a','b','b'])
&gt;&gt;&gt; df
   a  b  b
0  0  1  2
1  0  1  2
&gt;&gt;&gt; df.values
array([[0, 1, 2],
       [0, 1, 2]], dtype=int64)

# duplicate columns, diff types - error

&gt;&gt;&gt; df = pd.DataFrame([range(2) + ['x'], range(2) + ['c']], columns=['a','b','b'])
&gt;&gt;&gt; df
   a  b  b
0  0  1  x
1  0  1  c
&gt;&gt;&gt; df.values
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "C:\Python27\lib\site-packages\pandas-0.12.0-py2.7-win32.egg\pandas\core\frame.py", line 1748, in as_matrix
    return self._data.as_matrix(columns).T
  File "C:\Python27\lib\site-packages\pandas-0.12.0-py2.7-win32.egg\pandas\core\internals.py", line 1517, in as_matrix
    mat = self._interleave(self.items)
  File "C:\Python27\lib\site-packages\pandas-0.12.0-py2.7-win32.egg\pandas\core\internals.py", line 1548, in _interleave
    raise AssertionError('All items must be in block items')
AssertionError: All items must be in block items
</code></pre></div>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="In [3]: df1 = pd.DataFrame(np.random.randint(0,10,(4,3)), columns=['a','b','c'])

In [4]: df1
Out[4]: 
   a  b  c
0  9  3  2
1  9  0  2
2  7  7  6
3  3  4  2

In [5]: df2 = df1.reindex(columns=['c','a','d'])

In [6]: df2
Out[6]: 
   c  a   d
0  2  9 NaN
1  2  9 NaN
2  6  7 NaN
3  2  3 NaN

In [7]: df2.columns
Out[7]: Index([u'c', u'a', u'd'], dtype='object')

In [8]: ci = pd.MultiIndex.from_product([['x','y'],['a','b','c']])

In [10]: df3 = pd.DataFrame(np.random.randint(0,10,(4,6)), columns=ci)

In [11]: df3
Out[11]: 
   x        y      
   a  b  c  a  b  c
0  3  1  5  3  8  7
1  2  7  6  8  7  4
2  8  3  5  7  1  1
3  7  5  8  7  8  7

In [12]: df4 = df3.reindex(columns=['c','a','d'], level=1)

In [13]: df4
Out[13]: 
   x     y   
   c  a  c  a
0  5  3  7  3
1  6  2  4  8
2  5  8  1  7
3  8  7  7  7

In [14]: df4.columns
Out[14]: 
MultiIndex(levels=[[u'x', u'y'], [u'c', u'a', u'd']],
           labels=[[0, 0, 1, 1], [0, 1, 0, 1]])"><pre class="notranslate"><span class="pl-v">In</span> [<span class="pl-c1">3</span>]: <span class="pl-s1">df1</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>(<span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">randint</span>(<span class="pl-c1">0</span>,<span class="pl-c1">10</span>,(<span class="pl-c1">4</span>,<span class="pl-c1">3</span>)), <span class="pl-s1">columns</span><span class="pl-c1">=</span>[<span class="pl-s">'a'</span>,<span class="pl-s">'b'</span>,<span class="pl-s">'c'</span>])

<span class="pl-v">In</span> [<span class="pl-c1">4</span>]: <span class="pl-s1">df1</span>
<span class="pl-v">Out</span>[<span class="pl-c1">4</span>]: 
   <span class="pl-s1">a</span>  <span class="pl-s1">b</span>  <span class="pl-s1">c</span>
<span class="pl-c1">0</span>  <span class="pl-c1">9</span>  <span class="pl-c1">3</span>  <span class="pl-c1">2</span>
<span class="pl-c1">1</span>  <span class="pl-c1">9</span>  <span class="pl-c1">0</span>  <span class="pl-c1">2</span>
<span class="pl-c1">2</span>  <span class="pl-c1">7</span>  <span class="pl-c1">7</span>  <span class="pl-c1">6</span>
<span class="pl-c1">3</span>  <span class="pl-c1">3</span>  <span class="pl-c1">4</span>  <span class="pl-c1">2</span>

<span class="pl-v">In</span> [<span class="pl-c1">5</span>]: <span class="pl-s1">df2</span> <span class="pl-c1">=</span> <span class="pl-s1">df1</span>.<span class="pl-en">reindex</span>(<span class="pl-s1">columns</span><span class="pl-c1">=</span>[<span class="pl-s">'c'</span>,<span class="pl-s">'a'</span>,<span class="pl-s">'d'</span>])

<span class="pl-v">In</span> [<span class="pl-c1">6</span>]: <span class="pl-s1">df2</span>
<span class="pl-v">Out</span>[<span class="pl-c1">6</span>]: 
   <span class="pl-s1">c</span>  <span class="pl-s1">a</span>   <span class="pl-s1">d</span>
<span class="pl-c1">0</span>  <span class="pl-c1">2</span>  <span class="pl-c1">9</span> <span class="pl-v">NaN</span>
<span class="pl-c1">1</span>  <span class="pl-c1">2</span>  <span class="pl-c1">9</span> <span class="pl-v">NaN</span>
<span class="pl-c1">2</span>  <span class="pl-c1">6</span>  <span class="pl-c1">7</span> <span class="pl-v">NaN</span>
<span class="pl-c1">3</span>  <span class="pl-c1">2</span>  <span class="pl-c1">3</span> <span class="pl-v">NaN</span>

<span class="pl-v">In</span> [<span class="pl-c1">7</span>]: <span class="pl-s1">df2</span>.<span class="pl-s1">columns</span>
<span class="pl-v">Out</span>[<span class="pl-c1">7</span>]: <span class="pl-v">Index</span>([<span class="pl-s">u'c'</span>, <span class="pl-s">u'a'</span>, <span class="pl-s">u'd'</span>], <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s">'object'</span>)

<span class="pl-v">In</span> [<span class="pl-c1">8</span>]: <span class="pl-s1">ci</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">MultiIndex</span>.<span class="pl-en">from_product</span>([[<span class="pl-s">'x'</span>,<span class="pl-s">'y'</span>],[<span class="pl-s">'a'</span>,<span class="pl-s">'b'</span>,<span class="pl-s">'c'</span>]])

<span class="pl-v">In</span> [<span class="pl-c1">10</span>]: <span class="pl-s1">df3</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>(<span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">randint</span>(<span class="pl-c1">0</span>,<span class="pl-c1">10</span>,(<span class="pl-c1">4</span>,<span class="pl-c1">6</span>)), <span class="pl-s1">columns</span><span class="pl-c1">=</span><span class="pl-s1">ci</span>)

<span class="pl-v">In</span> [<span class="pl-c1">11</span>]: <span class="pl-s1">df3</span>
<span class="pl-v">Out</span>[<span class="pl-c1">11</span>]: 
   <span class="pl-s1">x</span>        <span class="pl-s1">y</span>      
   <span class="pl-s1">a</span>  <span class="pl-s1">b</span>  <span class="pl-s1">c</span>  <span class="pl-s1">a</span>  <span class="pl-s1">b</span>  <span class="pl-s1">c</span>
<span class="pl-c1">0</span>  <span class="pl-c1">3</span>  <span class="pl-c1">1</span>  <span class="pl-c1">5</span>  <span class="pl-c1">3</span>  <span class="pl-c1">8</span>  <span class="pl-c1">7</span>
<span class="pl-c1">1</span>  <span class="pl-c1">2</span>  <span class="pl-c1">7</span>  <span class="pl-c1">6</span>  <span class="pl-c1">8</span>  <span class="pl-c1">7</span>  <span class="pl-c1">4</span>
<span class="pl-c1">2</span>  <span class="pl-c1">8</span>  <span class="pl-c1">3</span>  <span class="pl-c1">5</span>  <span class="pl-c1">7</span>  <span class="pl-c1">1</span>  <span class="pl-c1">1</span>
<span class="pl-c1">3</span>  <span class="pl-c1">7</span>  <span class="pl-c1">5</span>  <span class="pl-c1">8</span>  <span class="pl-c1">7</span>  <span class="pl-c1">8</span>  <span class="pl-c1">7</span>

<span class="pl-v">In</span> [<span class="pl-c1">12</span>]: <span class="pl-s1">df4</span> <span class="pl-c1">=</span> <span class="pl-s1">df3</span>.<span class="pl-en">reindex</span>(<span class="pl-s1">columns</span><span class="pl-c1">=</span>[<span class="pl-s">'c'</span>,<span class="pl-s">'a'</span>,<span class="pl-s">'d'</span>], <span class="pl-s1">level</span><span class="pl-c1">=</span><span class="pl-c1">1</span>)

<span class="pl-v">In</span> [<span class="pl-c1">13</span>]: <span class="pl-s1">df4</span>
<span class="pl-v">Out</span>[<span class="pl-c1">13</span>]: 
   <span class="pl-s1">x</span>     <span class="pl-s1">y</span>   
   <span class="pl-s1">c</span>  <span class="pl-s1">a</span>  <span class="pl-s1">c</span>  <span class="pl-s1">a</span>
<span class="pl-c1">0</span>  <span class="pl-c1">5</span>  <span class="pl-c1">3</span>  <span class="pl-c1">7</span>  <span class="pl-c1">3</span>
<span class="pl-c1">1</span>  <span class="pl-c1">6</span>  <span class="pl-c1">2</span>  <span class="pl-c1">4</span>  <span class="pl-c1">8</span>
<span class="pl-c1">2</span>  <span class="pl-c1">5</span>  <span class="pl-c1">8</span>  <span class="pl-c1">1</span>  <span class="pl-c1">7</span>
<span class="pl-c1">3</span>  <span class="pl-c1">8</span>  <span class="pl-c1">7</span>  <span class="pl-c1">7</span>  <span class="pl-c1">7</span>

<span class="pl-v">In</span> [<span class="pl-c1">14</span>]: <span class="pl-s1">df4</span>.<span class="pl-s1">columns</span>
<span class="pl-v">Out</span>[<span class="pl-c1">14</span>]: 
<span class="pl-v">MultiIndex</span>(<span class="pl-s1">levels</span><span class="pl-c1">=</span>[[<span class="pl-s">u'x'</span>, <span class="pl-s">u'y'</span>], [<span class="pl-s">u'c'</span>, <span class="pl-s">u'a'</span>, <span class="pl-s">u'd'</span>]],
           <span class="pl-s1">labels</span><span class="pl-c1">=</span>[[<span class="pl-c1">0</span>, <span class="pl-c1">0</span>, <span class="pl-c1">1</span>, <span class="pl-c1">1</span>], [<span class="pl-c1">0</span>, <span class="pl-c1">1</span>, <span class="pl-c1">0</span>, <span class="pl-c1">1</span>]])</pre></div>
<p dir="auto">When passing a <em>nonexistent</em> column name to <code class="notranslate">reindex</code> on a dataframe without multiindex columns, the result is:</p>
<ul dir="auto">
<li>a <code class="notranslate">NaN</code> column with the "new" column name</li>
<li>the <code class="notranslate">columns</code> attribute matches the columns in the dataframe</li>
</ul>
<p dir="auto">The same action on a multiindex dataframe produces different results:</p>
<ul dir="auto">
<li>there are no <code class="notranslate">NaN</code> columns (this may not be a problem)</li>
<li>the <code class="notranslate">columns</code> attribute of the resulting dataframe does not match the dateframe column names  (this appears to be a bug)</li>
</ul>
<details>
INSTALLED VERSIONS
------------------
commit: None
python: 2.7.12.final.0
python-bits: 64
OS: Darwin
OS-release: 16.3.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: None.None
<p dir="auto">pandas: 0.19.1<br>
nose: 1.3.7<br>
pip: 9.0.1<br>
setuptools: 27.2.0<br>
Cython: 0.25.1<br>
numpy: 1.11.2<br>
scipy: 0.18.1<br>
statsmodels: 0.6.1<br>
xarray: None<br>
IPython: 5.1.0<br>
sphinx: 1.4.8<br>
patsy: 0.4.1<br>
dateutil: 2.6.0<br>
pytz: 2016.7<br>
blosc: None<br>
bottleneck: 1.1.0<br>
tables: 3.3.0<br>
numexpr: 2.6.1<br>
matplotlib: 1.5.3<br>
openpyxl: 2.4.0<br>
xlrd: 1.0.0<br>
xlwt: 1.1.2<br>
xlsxwriter: 0.9.3<br>
lxml: 3.6.4<br>
bs4: 4.5.1<br>
html5lib: None<br>
httplib2: None<br>
apiclient: None<br>
sqlalchemy: 1.1.4<br>
pymysql: None<br>
psycopg2: 2.6.2 (dt dec pq3 ext lo64)<br>
jinja2: 2.8<br>
boto: 2.43.0<br>
pandas_datareader: None</p>
</details>