How to Access Pandas dataframe columns when the number of columns are unknown before hand
<p>I am new to python and data science altogether.  I am writing a program to read and analyze a csv with pandas. The problem is that the csv will be supplied by the user and it can have variable number of columns depending on the user.  I do  not have a prior knowledge of the column names.
I went about the problem by reading the csv using pandas and read the column names into a python list. However problem ensued when I attempted to access the dataframe column by supplying the indexed list as a column name. something like this:</p>

<pre><code>#List of column names, coln
coln = df.columns
df.ix[:, df.coln[0]] # to access the first column of the dataframe.
</code></pre>

<p>But this did not work. Please help how do I do this? PLEASE HELP!</p>

<p>You should be using iloc and not the method I corrected below as the other answers show, but to fix your original error:</p>

<pre><code>coln = df.columns
df.ix[:, coln[0]] # to access the first column of the dataframe. 
</code></pre>

<p>You wrote df.coln[0] instead of coln[0]. coln is a list, there is no such thing as df.coln. </p>

python pandas data-science