问题:Pandas DataFrame到列表列表

将列表列表转换为pandas数据框很容易:

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

但是,如何将df重新变成列表列表?

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

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

回答 0

您可以访问基础数组并调用其tolist方法:

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

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

回答 1

如果数据具有要保留的列标签和索引标签,则有一些选项。

示例数据:

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

tolist()其他答案中描述方法很有用,但仅生成核心数据-可能还不够,具体取决于您的需求。

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

一种方法是使用将转换DataFrame为json df.to_json(),然后再次解析。这很麻烦,但确实具有一些优点,因为该to_json()方法具有一些有用的选项。

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

繁琐,但可能有用。

好消息是,为列和行建立列表非常简单:

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

这样生成:

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

如果None索引的名称令人讨厌,则将其重命名:

df = df.rename_axis('stage')

然后:

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

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

回答 2

我不知道它是否适合您的需求,但您也可以这样做:

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

这只是ndarray模块中的一个numpy数组,可让您执行所有常见的numpy数组操作。

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.


回答 3

我想保留索引,因此我针对该解决方案调整了原始答案:

list_df = df.reset_index().values.tolist()

现在,您可以将其粘贴到其他位置(例如,粘贴到“堆栈溢出”问题中),然后重新创建它:

pd.Dataframe(list_df, columns=['name1', ...])
pd.set_index(['name1'], inplace=True)

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

list_df = df.reset_index().values.tolist()

Now you can paste it somewhere else (e.g. to paste into a Stack Overflow question) and latter recreate it:

pd.Dataframe(list_df, columns=['name1', ...])
pd.set_index(['name1'], inplace=True)

回答 4

也许情况有所改变,但这返回了ndarrays列表,可以满足我的需要。

list(df.values)

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

list(df.values)

回答 5

注意:我在堆栈溢出中看到了很多情况,其中完全不需要将Pandas Series或DataFrame转换为NumPy数组或纯Python列表。如果您不熟悉该库,请考虑仔细检查那些Pandas对象是否已经提供了所需的功能。

引用@jpp 的评论

在实践中,通常不需要将NumPy数组转换为列表列表。


如果Pandas DataFrame / Series不起作用,则可以使用内置的DataFrame.to_numpySeries.to_numpy方法。

Note: I have seen many cases on Stack Overflow where converting a Pandas Series or DataFrame to a NumPy array or plain Python lists is entirely unecessary. If you’re new to the library, consider double-checking whether the functionality you need is already offered by those Pandas objects.

To quote a comment by @jpp:

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


If a Pandas DataFrame/Series won’t work, you can use the built-in DataFrame.to_numpy and Series.to_numpy methods.


回答 6

这很简单:

import numpy as np

list_of_lists = np.array(df)

This is very simple:

import numpy as np

list_of_lists = np.array(df)

回答 7

我们可以使用DataFrame.iterrows()函数遍历给定Dataframe的每一行,并根据每一行的数据构造一个列表:

# Empty list 
row_list =[] 

# Iterate over each row 
for index, rows in df.iterrows(): 
    # Create list for the current row 
    my_list =[rows.Date, rows.Event, rows.Cost] 

    # append the list to the final list 
    row_list.append(my_list) 

# Print 
print(row_list) 

我们可以成功地将给定数据帧的每一行提取到一个列表中

We can use the DataFrame.iterrows() function to iterate over each of the rows of the given Dataframe and construct a list out of the data of each row:

# Empty list 
row_list =[] 

# Iterate over each row 
for index, rows in df.iterrows(): 
    # Create list for the current row 
    my_list =[rows.Date, rows.Event, rows.Cost] 

    # append the list to the final list 
    row_list.append(my_list) 

# Print 
print(row_list) 

We can successfully extract each row of the given data frame into a list


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。