1

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:

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

But this did not work. Please help how do I do this? PLEASE HELP!

Ola O
  • 25
  • 2
  • 7
  • Possible duplicate of [Selecting pandas column by location](http://stackoverflow.com/questions/14941097/selecting-pandas-column-by-location) – ayhan Sep 13 '16 at 19:17

3 Answers3

2

Better is use iloc:

df.iloc[:, 0]

output is same as:

coln = df.columns
print (df.ix[:, coln[0]])
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
0

You can use iloc

df.iloc[:,0]

Btw, df.coln does not exist you created coln as separate variable.

Alvaro Silvino
  • 9,441
  • 12
  • 52
  • 80
0

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

coln = df.columns
df.ix[:, coln[0]] # to access the first column of the dataframe. 

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

A.Kot
  • 7,615
  • 2
  • 22
  • 24