Length mismatch error when assigning new column labels in pandas dataframe
<p>The tab file I'm working with is missing the final column name.  When I attempt to repair the header by appending the missing value, I get a mismatch error.  Here's an example to illustrate the problem:</p>

<p><strong>toy example</strong></p>

<p>There should be a '' as the last element of the first list:</p>

<pre><code>missingcol = [[gene, cell_1, '', cell_2]
               [MYC, 5.0, P, 4.0, A]
               [AKT, 3.0, A, 1.0, P]]
</code></pre>

<p>To fix this, I read the first line, appended a <code>''</code>, loaded <code>missingcol</code> into a pandas dataframe with <code>header=None</code> and skipping the first row, and redefined the column names with the modified header, like so:</p>

<pre><code>fullheader = missingcol[0].append('')
fullheader = missingcol[0]

missingcol_dropheader = missingcol[1:]

df = pd.DataFrame(missingcol_dropheader, columns=fullheader)
df
</code></pre>

<p>Which gives me the error:</p>

<pre><code>AssertionError: 4 columns passed, passed data had 5 columns
</code></pre>

<p>Last I checked, the new <code>fullheader</code> does, in fact, have 5 elements to match the five elements in the data frame.  <em>What is causing this continued mismatch and how do I fix it?</em></p>

<p><strong>real example</strong></p>

<p>I get a similar error when I repeat these same steps, but when using <code>read_csv</code> method with my actual test case.  I ignore the header at line 0, and the three blank lines from lines 1-3, and drop an unwanted first column, but otherwise it's similar:</p>

<pre><code>with open('CCLE_Expression_Entrez_2012-10-18.res', 'r') as f:
    header = f.readline().strip().split('\t')
header.append('') # missing empty colname over last A/P col

rnadf = pd.read_csv('CCLE_Expression_Entrez_2012-10-18.res', delimiter='\t', index_col=0, header=None, skiprows=[0,1,2,3])  
rnadf.columns = header
rnadf.drop([], axis=1, inplace=True)
rnadf.columns = header

ValueError: Length mismatch: Expected axis has 2073 elements, new values have 2074 elements
</code></pre>

<p>Very similar error to test case.  <em>What makes this error different to the test case and how do I fix it?</em></p>