Istvan
Istvan

Reputation: 8582

How to convert a pandas list row to numpy array?

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[]]?

Upvotes: 0

Views: 258

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150805

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)

Upvotes: 1

Related Questions