pandas ValueError:无法将大小为 5 的序列复制到维度为 2 的数组轴
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32948867/
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
ValueError: cannot copy sequence with size 5 to array axis with dimension 2
提问by nanda kumar gowda
using numpy 1.7.1 the below code works and produces the result as shown,
使用 numpy 1.7.1 下面的代码工作并产生如图所示的结果,
import pandas as pd
import numpy as np
d1 = pd.DataFrame({'Name': [1, 1, 1, 1, 1],'number': [1, 1, 1, 1, 1]})
d2 = pd.DataFrame({'Name': [1, 1, 1, 1, 1], 'number': [1, 1, 1, 1, 1]})
result = np.array([d1,d2])
Value of result is,
array([ Name number
0 1 1
1 1 1
2 1 1
3 1 1
4 1 1,
Name number
0 1 1
1 1 1
2 1 1
3 1 1
4 1 1], dtype=object)
But, In numpy 1.9.2 the same input produces exception as below,
但是,在 numpy 1.9.2 中,相同的输入产生如下异常,
"ValueError: cannot copy sequence with size 5 to array axis with dimension 2"
“ValueError:无法将大小为 5 的序列复制到维度为 2 的数组轴”
Need to know the reason that numpy not supporting this operation or some generic fix that can be used in both the version. I want the same kind of output as i get in 1.7.1, in both versions of numpy.
需要知道 numpy 不支持此操作的原因或可以在两个版本中使用的一些通用修复。我想要在 1.7.1 中得到的相同类型的输出,在 numpy 的两个版本中。
回答by Manish
I was able to reproduce your issue with numpy 1.9.2. It seems that numpy is trying to do a vstack. when the shape are same. I tried the following approach and it worked.
我能够用 numpy 1.9.2 重现您的问题。似乎 numpy 正在尝试做一个 vstack。当形状相同时。我尝试了以下方法并且奏效了。
result = np.empty(2, dtype=object)
result[:]= [d1, d2]
result
array([ Name number
0 1 1
1 1 1
2 1 1
3 1 1
4 1 1,
Name number
0 1 1
1 1 1
2 1 1
3 1 1
4 1 1], dtype=object)

