Pandas DataFrame to List of Lists

python list to dataframe row
convert dataframe column to list python
pandas series to list
nested list to dataframe python
convert row to column python list
list to rows pandas
pandas series of lists to dataframe
list of lists to excel python

It's easy to turn a list of lists into a pandas dataframe:

import pandas as pd
df = pd.DataFrame([[1,2,3],[3,4,5]])

But how do I turn df back into a list of lists?

lol = df.what_to_do_now?
print lol
# [[1,2,3],[3,4,5]]

You could access the underlying array and call its tolist method:

>>> df = pd.DataFrame([[1,2,3],[3,4,5]])
>>> lol = df.values.tolist()
>>> lol
[[1L, 2L, 3L], [3L, 4L, 5L]]

Creating Pandas dataframe using list of lists, Creating Pandas dataframe using list of lists. Pandas DataFrame is a 2-​dimensional labeled data structure with columns of potentially different types. Creating Pandas dataframe using list of lists Pandas DataFrame is a 2-dimensional labeled data structure with columns of potentially different types. It is generally the most commonly used pandas object.


If the data has column and index labels that you want to preserve, there are a few options.

Example data:

>>> df = pd.DataFrame([[1,2,3],[3,4,5]], \
       columns=('first', 'second', 'third'), \
       index=('alpha', 'beta')) 
>>> df
       first  second  third
alpha      1       2      3
beta       3       4      5

The tolist() method described in other answers is useful but yields only the core data - which may not be enough, depending on your needs.

>>> df.values.tolist()
[[1, 2, 3], [3, 4, 5]]

One approach is to convert the DataFrame to json using df.to_json() and then parse it again. This is cumbersome but does have some advantages, because the to_json() method has some useful options.

>>> df.to_json()
{
  "first":{"alpha":1,"beta":3},
  "second":{"alpha":2,"beta":4},"third":{"alpha":3,"beta":5}
}

>>> df.to_json(orient='split')
{
 "columns":["first","second","third"],
 "index":["alpha","beta"],
 "data":[[1,2,3],[3,4,5]]
}

Cumbersome but may be useful.

The good news is that it's pretty straightforward to build lists for the columns and rows:

>>> columns = [df.index.name] + [i for i in df.columns]
>>> rows = [[i for i in row] for row in df.itertuples()]

This yields:

>>> print(f"columns: {columns}\nrows: {rows}") 
columns: [None, 'first', 'second', 'third']
rows: [['alpha', 1, 2, 3], ['beta', 3, 4, 5]]

If the None as the name of the index is bothersome, rename it:

df = df.rename_axis('stage')

Then:

>>> columns = [df.index.name] + [i for i in df.columns]
>>> print(f"columns: {columns}\nrows: {rows}") 

columns: ['stage', 'first', 'second', 'third']
rows: [['alpha', 1, 2, 3], ['beta', 3, 4, 5]]

How to Convert Pandas DataFrame into a List, At times, you may need to convert pandas DataFrame into a list. And once you run the code, you'll get the following multi-dimensional list (i.e., list of lists):. In this article, we will discuss how to convert a dataframe into a list of lists, by converting either each row or column into a list and create a python list of lists from them. Contents of the dataframe object studentDfObj are, In the above-created dataframe, we have to fetch each row as a list and create a list of these lists.


I don't know if it will fit your needs, but you can also do:

>>> lol = df.values
>>> lol
array([[1, 2, 3],
       [3, 4, 5]])

This is just a numpy array from the ndarray module, which lets you do all the usual numpy array things.

Pandas : Convert a DataFrame into a list of rows or columns in , In this article, we will discuss how to convert a dataframe into a list of lists, by converting either each row or column into a list and create a  Convert an Individual Column in the DataFrame into a List. Let’s say that you’d like to convert the ‘Product’ column into a list. You can then use the following template in order to convert an individual column in the DataFrame into a list: df['column name'].values.tolist()


Most of the time, the real answer to this question is quite simple: Don't.

To quote a comment by @jpp:

In practice, there's often no need to convert the NumPy array into a list of lists.

Unfortunately I have seen far too many cases where people will convert Pandas Series to NumPy arrays or a list when it's simply unnecessary.

Not only that, converting a DataFrame to a list resembling the one in the OP leads to the loss of a lot of information. If that information isn't important, then it's possible that you shouldn't be using a DataFrame in the first place.


For those rare situations where it does make sense to convert the DataFrame to another data structure, try using the NumPy array returned by DataFrame.to_numpy. If for some reason that won't do, you can simply call the ndarray.tolist method.

How to convert a list of lists into a Pandas DataFrame in Python, Kite is a free autocomplete for Python developers. Code faster with the Kite plugin for your code editor, featuring Line-of-Code Completions and cloudless  In this article we will discuss how to convert a single or multiple lists to a DataFrame. Python’s pandas library provide a constructor of DataFrame to create a Dataframe by passing objects i.e. Here data parameter can be a numpy ndarray , dict, or an other DataFrame. Also, columns and index are for column and index labels.


Maybe something changed but this gave back a list of ndarrays which did what I needed.

list(df.values)

Pandas DataFrame to List of Lists, 1. Access the underlying array. 2.Then, call its tolist method. >>> df = pd.​DataFrame([[1,2,3],[3,4,5]]). >>> lol = df.values.tolist(). >>> lol. [[1L, 2L  I want convert this dataframe to a list of lists, likes this: [['2016-12-01 00:00:00', NaN],['2016-12-01 00:01:00', 2],['2016-12-01 00:02:00', 1]] I tried df.values or df.values.tolist() and always receive:


Convert a list of lists into a Pandas Dataframe, import pandas as pd data = [['New York Yankees', 'Acevedo Juan', 900000, '​Pitcher'], ['New York Yankees', 'Anderson Jason', 300000, 'Pitcher'], ['New York  This is because tst[lookupValue] is a dataframe, and slicing it with [['SomeCol']] asks for a list of columns (that list that happens to have a length of 1), resulting in a dataframe being returned. If you remove the extra set of brackets, as in tst[lookupValue]['SomeCol'] , then you are asking for just that one column rather than a list of


pandas.DataFrame.explode, Explode a DataFrame from list-like columns to long format. Notes. This routine will explode list-likes including lists, tuples, Series, and np.ndarray. The result  Python list is easy to work with and also list has a lot of in-built functions to do a whole lot of operations on lists. Pandas dataframe’s columns consist of series but unlike the columns, Pandas dataframe rows are not having any similar association.


Pandas: Convert a given list of lists into a Dataframe, Write a Pandas program to convert a given list of lists into a Dataframe. Sample Solution : Python Code : import pandas as pd my_lists = [['col1', '  Most pandas users quickly get familiar with ingesting spreadsheets, CSVs and SQL data. However, there are times when you will have data in a basic list or dictionary and want to populate a DataFrame. Pandas offers several options but it may not always be immediately clear on when to use which ones.