Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将numpy数组作为列添加到数据帧_Python_Numpy_Pandas - Fatal编程技术网

Python 将numpy数组作为列添加到数据帧

Python 将numpy数组作为列添加到数据帧,python,numpy,pandas,Python,Numpy,Pandas,我有一个形状为(X,Y)的熊猫数据框对象,如下所示: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] [[1, 2, 3, [0, 1, 0]], [4, 5, 6, [0, 0, 1]], [7, 8, 9, [1, 0, 0]]] 还有一个形状(X,Z)的numpy稀疏矩阵(CSC),看起来像这样 [[0, 1, 0], [0, 0, 1], [1, 0, 0]] 如何将矩阵中的内容添加到新命名列中的数据框中,以使数据框的结果如下所示: [[1, 2, 3], [4

我有一个形状为(X,Y)的熊猫数据框对象,如下所示:

[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
[[1, 2, 3, [0, 1, 0]],
[4, 5, 6, [0, 0, 1]],
[7, 8, 9, [1, 0, 0]]]
还有一个形状(X,Z)的numpy稀疏矩阵(CSC),看起来像这样

[[0, 1, 0],
[0, 0, 1],
[1, 0, 0]]
如何将矩阵中的内容添加到新命名列中的数据框中,以使数据框的结果如下所示:

[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
[[1, 2, 3, [0, 1, 0]],
[4, 5, 6, [0, 0, 1]],
[7, 8, 9, [1, 0, 0]]]
请注意,数据框现在具有形状(X,Y+1),矩阵中的行是数据框中的元素

import numpy as np
import pandas as pd
import scipy.sparse as sparse

df = pd.DataFrame(np.arange(1,10).reshape(3,3))
arr = sparse.coo_matrix(([1,1,1], ([0,1,2], [1,2,0])), shape=(3,3))
df['newcol'] = arr.toarray().tolist()
print(df)
屈服

   0  1  2     newcol
0  1  2  3  [0, 1, 0]
1  4  5  6  [0, 0, 1]
2  7  8  9  [1, 0, 0]

考虑使用高维数据结构(a),而不是在列中存储数组:

In [11]: p = pd.Panel({'df': df, 'csc': csc})

In [12]: p.df
Out[12]: 
   0  1  2
0  1  2  3
1  4  5  6
2  7  8  9

In [13]: p.csc
Out[13]: 
   0  1  2
0  0  1  0
1  0  0  1
2  1  0  0
看看横截面等等

In [14]: p.xs(0)
Out[14]: 
   csc  df
0    0   1
1    1   2
2    0   3
.

这里是另一个例子:

import numpy as np
import pandas as pd

""" This just creates a list of touples, and each element of the touple is an array"""
a = [ (np.random.randint(1,10,10), np.array([0,1,2,3,4,5,6,7,8,9]))  for i in 
range(0,10) ]

""" Panda DataFrame will allocate each of the arrays , contained as a touple 
element , as column"""
df = pd.DataFrame(data =a,columns=['random_num','sequential_num'])
通常的秘密是以a=[(数组_11,数组_12,…,数组_1n),…,(数组_m1,数组_m2,…,数组_mn)]的形式分配数据,panda DataFrame将按数组的n列对数据进行排序。当然,可以使用数组的数组来代替touples,在这种情况下,其形式是: a=[[array_11,array_12,…,array_1n],…,[array_m1,array_m2,…,array_mn]]

这是根据上述代码打印(df)的输出:

                       random_num                  sequential_num
0  [7, 9, 2, 2, 5, 3, 5, 3, 1, 4]  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
1  [8, 7, 9, 8, 1, 2, 2, 6, 6, 3]  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
2  [3, 4, 1, 2, 2, 1, 4, 2, 6, 1]  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
3  [3, 1, 1, 1, 6, 2, 8, 6, 7, 9]  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
4  [4, 2, 8, 5, 4, 1, 2, 2, 3, 3]  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
5  [3, 2, 7, 4, 1, 5, 1, 4, 6, 3]  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
6  [5, 7, 3, 9, 7, 8, 4, 1, 3, 1]  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
7  [7, 4, 7, 6, 2, 6, 3, 2, 5, 6]  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
8  [3, 1, 6, 3, 2, 1, 5, 2, 2, 9]  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
9  [7, 2, 3, 9, 5, 5, 8, 6, 9, 8]  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
上述示例的其他变化:

b = [ (i,"text",[14, 5,], np.array([0,1,2,3,4,5,6,7,8,9]))  for i in 
range(0,10) ]
df = pd.DataFrame(data=b,columns=['Number','Text','2Elemnt_array','10Element_array'])
df的输出:

   Number  Text 2Elemnt_array                 10Element_array
0       0  text       [14, 5]  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
1       1  text       [14, 5]  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
2       2  text       [14, 5]  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
3       3  text       [14, 5]  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
4       4  text       [14, 5]  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
5       5  text       [14, 5]  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
6       6  text       [14, 5]  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
7       7  text       [14, 5]  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
8       8  text       [14, 5]  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
9       9  text       [14, 5]  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
如果要添加其他数组列,请执行以下操作:

df['3Element_array']=[([1,2,3]),([1,2,3]),([1,2,3]),([1,2,3]),([1,2,3]),([1,2,3]),([1,2,3]),([1,2,3]),([1,2,3]),([1,2,3])]
df的最终输出为:

   Number  Text 2Elemnt_array                 10Element_array 3Element_array
0       0  text       [14, 5]  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]      [1, 2, 3]
1       1  text       [14, 5]  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]      [1, 2, 3]
2       2  text       [14, 5]  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]      [1, 2, 3]
3       3  text       [14, 5]  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]      [1, 2, 3]
4       4  text       [14, 5]  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]      [1, 2, 3]
5       5  text       [14, 5]  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]      [1, 2, 3]
6       6  text       [14, 5]  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]      [1, 2, 3]
7       7  text       [14, 5]  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]      [1, 2, 3]
8       8  text       [14, 5]  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]      [1, 2, 3]
9       9  text       [14, 5]  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]      [1, 2, 3]

您可以使用以下方法从dataframe添加和检索numpy数组:

import numpy as np
import pandas as pd

df = pd.DataFrame({'b':range(10)}) # target dataframe
a = np.random.normal(size=(10,2)) # numpy array
df['a']=a.tolist() # save array
np.array(df['a'].tolist()) # retrieve array

这是建立在前面的答案之上的,因为稀疏部分让我感到困惑,这对非稀疏的numpy arrray很有效。

不鼓励这种嵌套。为什么需要这样做?请看这个问题:我想保留在合并后通过单个列名选择矩阵先前内容的可能性。为什么不使用两个
DataFrame
s呢?我想我们不能为坚持这样做的用户提供防弹鞋,所以我不认为这一定是个坏主意。虽然我同意这种可能性很高,但这是
pandas
灵活性的一个极好的例子。在这个问题中,数据已经是具有相同形状行的同质数字类型,而在该示例中,它们是不同长度的
list
s。我同意你可以做一些有趣的事情。然而,当你已经有了一个矩阵,为什么要把它变成一个列表呢。。。使它不再是列表的一列(所以它很有用)!当有创造力的人被允许做别人认为愚蠢的事情时,世界会变得更美好现在不推荐使用面板,通常推荐使用多索引。例如,通过
pd.concat([df,csc],axis=1,key=[“df”,“csc]”)创建
A=np.eye(3);df=pd.concat([A,A],axis=1)
->TypeError:无法在20.2中连接非NDFrame对象?(一个“熊猫现在不推荐使用这个”的wiki会很好。)@denis try
A=pd.DataFrame(np.eye(3));df=pd.concat([A,A],axis=1,key=[“A”,“B”])
谢谢,
df.columns多索引(levels=[[u'A',u'B'],[0,1,2]
(拍拍额头)
df = pd.DataFrame(np.arange(1,10).reshape(3,3))
df['newcol'] = pd.Series(your_2d_numpy_array)