Reputation: 25
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!
Upvotes: 1
Views: 3344
Reputation: 7913
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.
Upvotes: 0
Reputation: 863236
Better is use iloc
:
df.iloc[:, 0]
output is same as:
coln = df.columns
print (df.ix[:, coln[0]])
Upvotes: 2
Reputation: 9753
You can use iloc
df.iloc[:,0]
Btw, df.coln
does not exist you created coln
as separate variable.
Upvotes: 0