Pandas DataFrame to List of Lists

匿名 (未验证) 提交于 2019-12-03 01:18:02

问题:

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

回答1:

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


回答2:

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


回答3:

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.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!