【发布时间】:2020-08-27 10:16:44
【问题描述】:
我正在尝试展平 Numpy 数组,但展平、拉维尔和 hstack 没有这样做。我需要能够从数组中创建一个 Pandas 系列,但我似乎无法让 Pandas 接受它:
print(type(centroids))
# <class 'numpy.matrix'>
print(centroids.shape)
# (5, 9328)
centroid = centroids[i]
centroid = np.hstack(centroid)
print(centroid)
# [[ 0.98487911 0.7483803 11.80978353 ... 0.97687837 0.21988038
3.33842549]] <-- still enclosed by two brackets
print(centroid.shape)
# (1, 9328)
centroid = pd.Series(centroid, name='value') <-- throws exception: Exception: Data must be 1-dimensional
【问题讨论】:
-
pd.Series(centroid[0], name='value')
-
centroid = centroids[i]就足够了。 -
@KevinWelch 不幸的是,没有,我试过了,但没有用。即使确实如此,我也在寻找一种基于矩阵的方法……而不是一种迭代的方法。不过还是谢谢。
标签: python pandas numpy matrix