Get a DataFrame column as a list of values

I am trying to get pandas DataFrame columns as a list of values.

I can access the first column using iloc :

 df.ix[:,[0]].values 

However, this returns an array of lists:

 >>> df3.ix[:,[1]].values array([[ 0.], [ 0.], [ 0.], 

How can I return a list of numbers?

I can get what I want by calling the column by name and using tolist() :

 >>> df3['D-328'].tolist() [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 15.0, 

However, when calling a column by index, this method is not available:

 >>> df3.ix[:,[0]].tolist() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Anaconda\lib\site-packages\pandas\core\generic.py", line 2360, in __getattr__ (type(self).__name__, name)) AttributeError: 'DataFrame' object has no attribute 'tolist' 
+8
python pandas dataframe
source share
2 answers

I think you can try ix as follows:

 df.ix[:, 0].tolist() 

And as DSM mentioned in the comments, you can use iloc this way if you need to select the first column by position:

 df.iloc[:, 0].tolist() 
+7
source share

just call the list function, for example:

 list(df3['D-328']) 

or

 list(df3.ix[:,0]) 
0
source share

All Articles