i am trying to make an np.array from my dataframe column which contains float arrays of different lengths example of column:
[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
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
pandas version 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
.
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])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.