0

I have a dataframe that has two columns:

name, [sizes]

Example

one, [1,2,3]
two, [4,5,6]

I would like to convert values to array for further processing.

x_data = list(set(df.name.values))
y_data = [df.query('name == @h').sizes.values  for h in x_data]

This returns:

[array([list([1, 2, 3])], dtype=object), array([list([4,5,6])], dtype=object)]

Is there a way to flatten this out and have it as a simple array[] instead of array[list[]]?

1 Answer 1

1

Use tolist():

np.array(df['sizes'].tolist())

Output:

array([[1, 2, 3],
       [4, 5, 6]])

Note it only works if all the lists are of the same size within the column. For different sizes, we would get something like:

array([list([1, 2, 3]), list([4, 5, 6, 7])], dtype=object)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.