简体   繁体   English

ValueError:使用序列设置数组元素

[英]ValueError: Setting an array element with sequence

i am trying to make an np.array from my dataframe column which contains float arrays of different lengths example of column:我正在尝试从我的 dataframe 列创建一个 np.array,其中包含不同长度的浮点数 arrays 列示例:

[0.123123, 0.123123]
[1.123112]
[0.123123, 0.123123, 0.123123, 0.123123]

and i am getting ValueError: Setting an array element with sequence我得到ValueError: Setting an array element with sequence

i tried:我试过了:

np.array(df['vector'].tolist())
np.array(df['vector'].squeeze())
np.array(df['vector'].tolist(), dtype=object)

and they all lead to ValueError它们都会导致ValueError

pandas version 0.23.4 pandas 版本 0.23.4

If you want to concatenate all of nested elements in you data frame column to a single 1D array, you can use np.hstack .如果要将数据框列中的所有嵌套元素连接到单个一维数组,可以使用np.hstack

import numpy as np
import pandas as pd

df = pd.DataFrame({'vector': [
    [0.123123, 0.123123],
    [1.123112],
    [0.123123, 0.123123, 0.123123, 0.123123]
    ]}
)

np.hstack(df['vector'])
# returns:
array([0.123123, 0.123123, 1.123112, 0.123123, 0.123123, 0.123123, 0.123123])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM