所以我有一个带有X,Y属性数字的数据帧activities_and_attractions
。我想创建一个numpy数组
[[X1,Y1]
[X2,Y2]
[X3,Y3]]
但事实证明它只打印最后一行
data = np.array([activities_and_attractions.X[0], activities_and_attractions.Y[0]])
for i in range(len(activities_and_attractions.index)):
np.vstack((data, np.array([activities_and_attractions.X[i], activities_and_attractions.Y[i]])))
print(data)
答案 0 :(得分:1)
您需要在循环中将vstack
的输出分配给data
:
for i in range(len(activities_and_attractions.index)):
data = np.vstack((data, np.array([activities_and_attractions.X[i], activities_and_attractions.Y[i]]))) # assign to data
考虑使用numpy.append
执行此任务。以这种方式发展ndarray效率不高。
出了什么问题:
data = np.array( (activities_and_attractions.X, activities_and_attractions.Y) )