pandas 将数组列表转换为熊猫数据框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49540922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:22:58  来源:igfitidea点击:

Convert list of arrays to pandas dataframe

pythonpython-3.xpandasnumpydataframe

提问by Marcos Santana

I have a list of numpy arrays that I'm trying to convert to DataFrame. Each array should be a row of the dataframe.

我有一个要转换为 DataFrame 的 numpy 数组列表。每个数组应该是数据帧的一行。

Using pd.DataFrame() isn't working. It always gives the error: ValueError: Must pass 2-d input.

使用 pd.DataFrame() 不起作用。它总是给出错误:ValueError: Must pass 2-d input。

Is there a better way to do this?

有一个更好的方法吗?

This is my current code:

这是我当前的代码:

list_arrays = array([[0, 0, 0, 1, 0, 0, 0, 0, 00]], dtype=uint8), array([[0, 0, 3, 2, 0, 0, 0, 0, 00]], dtype=uint8)]

d = pd.DataFrame(list_of_arrays)

ValueError: Must pass 2-d input

回答by MaxU

Option 1:

选项1:

In [143]: pd.DataFrame(np.concatenate(list_arrays))
Out[143]:
   0  1  2  3  4  5  6  7  8
0  0  0  0  1  0  0  0  0  0
1  0  0  3  2  0  0  0  0  0

Option 2:

选项 2:

In [144]: pd.DataFrame(list(map(np.ravel, list_arrays)))
Out[144]:
   0  1  2  3  4  5  6  7  8
0  0  0  0  1  0  0  0  0  0
1  0  0  3  2  0  0  0  0  0


Why do I get:

ValueError: Must pass 2-d input

为什么我得到:

ValueError: Must pass 2-d input

I think pd.DataFrame()tries to convert it to NDArray like as follows:

我认为pd.DataFrame()尝试将其转换为 NDArray,如下所示:

In [148]: np.array(list_arrays)
Out[148]:
array([[[0, 0, 0, 1, 0, 0, 0, 0, 0]],

       [[0, 0, 3, 2, 0, 0, 0, 0, 0]]], dtype=uint8)

In [149]: np.array(list_arrays).shape
Out[149]: (2, 1, 9)     # <----- NOTE: 3D array

回答by piRSquared

Alt 1

替代 1

pd.DataFrame(sum(map(list, list_arrays), []))

   0  1  2  3  4  5  6  7  8
0  0  0  0  1  0  0  0  0  0
1  0  0  3  2  0  0  0  0  0

Alt 2

替代选项 2

pd.DataFrame(np.row_stack(list_arrays))

   0  1  2  3  4  5  6  7  8
0  0  0  0  1  0  0  0  0  0
1  0  0  3  2  0  0  0  0  0

回答by jpp

Here is one way.

这是一种方法。

import numpy as np, pandas as pd

lst = [np.array([[0, 0, 0, 1, 0, 0, 0, 0, 0]], dtype=int),
       np.array([[0, 0, 3, 2, 0, 0, 0, 0, 0]], dtype=int)]

df = pd.DataFrame(np.vstack(lst))

#    0  1  2  3  4  5  6  7  8
# 0  0  0  0  1  0  0  0  0  0
# 1  0  0  3  2  0  0  0  0  0

回答by YOBEN_S

You can using pd.Series

你可以使用 pd.Series

pd.Series(l).apply(lambda x : pd.Series(x[0]))
Out[294]: 
   0  1  2  3  4  5  6  7  8
0  0  0  0  1  0  0  0  0  0
1  0  0  3  2  0  0  0  0  0