python - Pandas DataFrame to List of Lists


Translate

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]]

All Answers
  • Translate

    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]]
    

  • Translate

    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]]
    

  • Translate

    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.


  • Translate

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

    list(df.values)
    

  • Translate

    I wanted to preserve the index, so I adapted the original answer to this solution:

    df.reset_index().values.tolist()
    

    Now to recreate it somewhere else (e.g. to past in a Stack Overflow question):

    pd.Dataframe(<data-printed-above>, columns=['name1', ...])
    pd.set_index(['name1'], inplace=True)