如何使用多个 numpy 1d 数组创建一个 Pandas DataFrame?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45399950/
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
How to create a pandas DataFrame with several numpy 1d arrays?
提问by laurenz
I've created some np.arrays to do some calculation with them. (All have the same size [100,1]) Now I want to create a pandas Dataframe and each array shoud be one column of that DF. The Names of the arrays should be the header of the DataFrame.
我创建了一些 np.arrays 来对它们进行一些计算。(都具有相同的大小 [100,1])现在我想创建一个 Pandas Dataframe,并且每个数组都应该是该 DF 的一列。数组的名称应该是 DataFrame 的标题。
In Matlab I would easily do it like that:
在 Matlab 中,我会很容易地这样做:
Table = table(array1, array2, array3, ... );
Table = table(array1, array2, array3, ... );
How can I do this in Python?
我怎样才能在 Python 中做到这一点?
thanks in advance!
提前致谢!
回答by ayhan
Let's say these are your arrays:
假设这些是您的数组:
arr1, arr2, arr3 = np.zeros((3, 100, 1))
arr1.shape
Out: (100, 1)
You can use hstackto stack them and pass the resulting 2D array to the DataFrame constructor:
您可以使用hstack来堆叠它们并将生成的二维数组传递给 DataFrame 构造函数:
df = pd.DataFrame(np.hstack((arr1, arr2, arr3)))
df.head()
Out:
0 1 2
0 0.0 0.0 0.0
1 0.0 0.0 0.0
2 0.0 0.0 0.0
3 0.0 0.0 0.0
4 0.0 0.0 0.0
Or name the columns as arr1
, arr2
, ...:
或将列命名为arr1
, arr2
, ...:
df = pd.DataFrame(np.hstack((arr1, arr2, arr3)),
columns=['arr{}'.format(i+1) for i in range(3)])
which gives
这使
df.head()
Out:
arr1 arr2 arr3
0 0.0 0.0 0.0
1 0.0 0.0 0.0
2 0.0 0.0 0.0
3 0.0 0.0 0.0
4 0.0 0.0 0.0
回答by jezrael
Solution with numpy.concatenate
for 2d array and DataFrame
constructor:
numpy.concatenate
二维数组和DataFrame
构造函数的解决方案:
df = pd.DataFrame(np.concatenate([arr1, arr2, arr3], axis=1), columns= ['a','b','c'])