Pandas Multiple columns same name
<p>I am creating a <code>dataframe</code> from <code>csv</code>.I have gone thru the docs,multiple <code>SO</code> posts,links as i have just started <code>Pandas</code> but didnt get it.The csv has multiple columns with same names say <code>a</code>.</p>

<p>So after forming <code>dataframe</code> and when i do <code>df['a']</code> which value will it return? It does not return all values .</p>

<p>Also only one of the values will have a string rest will be <code>None</code> .How can i get that column?</p>

<p>the relevant parameter is <code>mangle_dupe_cols</code></p>

<p>from the <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html" rel="nofollow">docs</a></p>

<blockquote>
<pre><code>mangle_dupe_cols : boolean, default True
    Duplicate columns will be specified as 'X.0'...'X.N', rather than 'X'...'X'
</code></pre>
</blockquote>

<p>by default, all of your <code>'a'</code> columns get named <code>'a.0'...'a.N'</code> as specified above.</p>

<p>if you used <code>mangle_dupe_cols=False</code>, importing this <code>csv</code> would produce an error.</p>

<p>you can get all of your columns with </p>

<pre><code>df.filter(like='a')
</code></pre>

<hr>

<p><strong><em>demonstration</em></strong></p>

<pre><code>from StringIO import StringIO
import pandas as pd

txt = """a, a, a, b, c, d
1, 2, 3, 4, 5, 6
7, 8, 9, 10, 11, 12"""

df = pd.read_csv(StringIO(txt), skipinitialspace=True)
df
</code></pre>

<p><a href="https://i.stack.imgur.com/iQhUw.png" rel="nofollow"><img src="https://i.stack.imgur.com/iQhUw.png" alt="enter image description here"></a></p>

<pre><code>df.filter(like='a')
</code></pre>

<p><a href="https://i.stack.imgur.com/1jhmQ.png" rel="nofollow"><img src="https://i.stack.imgur.com/1jhmQ.png" alt="enter image description here"></a></p>

python python-2.7 csv pandas