Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert dataFrame to list

I have a pandas dataframe that I convert to numpy array as follows:

df.values

which gives the following output:

array([[2],
       [0],
       [1],
       ..., 
       [0],
       [1],
       [0]], dtype=int64)

However I want to obtain the list as follows:

[0, 2, 3]

Any idea how to do this?

like image 627
Pau Folch Codera Avatar asked Feb 10 '16 15:02

Pau Folch Codera


People also ask

How do you convert a DataFrame to a list?

To convert Pandas DataFrame to List in Python, use the df. values(). tolist() function. Pandas series can be converted to a list using tolist() or type casting method.

Can a DataFrame be a list?

The short answer is No - dataframes are not lists of lists.

How do I convert a DataFrame to a column in one list?

tolist() to get a list of a specified column. From the dataframe, we select the column “Name” using a [] operator that returns a Series object. Next, we will use the function Series. to_list() provided by the Series class to convert the series object and return a list.

How convert pandas row to list?

The command to convert Dataframe to list is pd. DataFrame. values. tolist().


2 Answers

Maybe you can use iloc or loc for selecting column and then tolist:

print df
   a
0  2
1  0
2  1
3  0
4  1
5  0

print df.values
[[2]
 [0]
 [1]
 [0]
 [1]
 [0]]

print df.iloc[:, 0].tolist()
[2, 0, 1, 0, 1, 0]

Or maybe:

print df.values.tolist()
[[2L], [0L], [1L], [0L], [1L], [0L]]

print df.iloc[:, 0].values.tolist()
[2L, 0L, 1L, 0L, 1L, 0L]

print df.loc[:, 'a'].tolist()
[2, 0, 1, 0, 1, 0]

print df['a'].tolist()
[2, 0, 1, 0, 1, 0]

But maybe you need flatten:

print df.values.flatten()
[2 0 1 0 1 0]

print df.iloc[:, 0].values.flatten()
[2 0 1 0 1 0]
like image 194
jezrael Avatar answered Oct 01 '22 10:10

jezrael


Looks like you have a dataframe with one column and several rows. Remember that this is a two dimensional array, you have to slice the first column then list the values within that column.

This should do it:

df[0].values.tolist()

df[0] - This selects all values in the first column. For the second column you'd use df[1] third df[2] and so on.

You can tell the shape of your dataframe by running df.shape. This will tell you how many rows and columns exist in your dataframe e.g. (9,1) which means 9 rows and 1 column

like image 30
OkezieE Avatar answered Oct 01 '22 11:10

OkezieE