4

I have a column of lists, and each list contains the same number of values.

If I do

df['column'].to_numpy()

I get an array of lists:

array([list([0, 4688, 11, 43486, 40508, 13, 5,...
       list([0, 40928, 17707, 22705, 9, 38312, 2..
       list([0, 6766, 368, 3551, 28837,..
      dtype=object)

How do I get a 2D array instead?

2
  • That's odd. Can you share the content of your df["column"] ? Commented May 12, 2020 at 3:42
  • Show how to construct this dataframe. Does any list have a non-numeric element? Commented May 12, 2020 at 3:56

3 Answers 3

7

You can do this:

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

Or you can simply stack them:

np.stack(df['column'].to_numpy())

This will stack your lists on top of each other and output is a 2-D array. You have to make sure lists are of the same length. Numpy arrays are rectangular.

Sign up to request clarification or add additional context in comments.

7 Comments

Technically true, but likely doesn't address the root of the problem
@MadPhysicist Not sure what you mean. It is one of many solutions. I added another one that deals with pandas first if that is what you mean by root of the problem :D
Getting a dataframe with lists is usually weird
That being said, +1 for the cleanest solution based on the information provided
@MadPhysicist I agree a better practice is to not include same length lists in a column, but I can see applications were having it sounds reasonable too. Thanks for the upvote :)
|
1

You can use list list comprehension

>>> df
                 A
0  [0, 1, 2, 3, 4]
1  [0, 1, 2, 3, 4]
2  [0, 1, 2, 3, 4]

>>> np.array([x for x in df['A']])
array([[0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4]])

Comments

0
data=np.array(df['column']).reshape(2,2)
  • It will convert your desire columns into array

for 2d array you can use reshape function on numpy But you should have proper value in data to corvert eg (2,2) is used if you have 4 numbers of data it indicate (row,columns)

2 Comments

This actually should return 1-D array of lists again. OP wants a 2-D array.
The shape is not necessarily (2,2) either. The example in the question shows you that it is actually not of shape (2,2). You might have been confusing shape with dimensions.

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.