2

I have the following dataframe :

df = pd.DataFrame(
{'a':[np.array([1,2,3]),np.array([4,5,6])]
}
)

i want to have a numpy array like following extracted from the a column:

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

i can do it like below:

 for i, row in df.iterrows():
     t_arr[i,:] = row["a"]

but i am looking for a solution without for loop. .values and to_numpy() don't return the desired results

1
  • You should show the un-desired result from values. It's a useful starting point for creating an answer. I expect it is an object dtype array with 2 array elements. In general they might differ in shape, but if they do match, they can then be joined into one array as demonstrated in the answers. Commented Mar 22, 2021 at 15:56

3 Answers 3

2

You could do this:

np.vstack(df['a'])

which returns:

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

Comments

0

Use np.stack to create a 2d numpy array.

Either from the series object of your a column: np.stack(df['a'].values)

Or from the whole DataFrame: np.stack(df.values.flatten())

Edit: You don't even need to call .values. You can just call np.stack on the series object.

1 Comment

np.stack iterates through the argument (such as a Series) and creates a 2d array from each. Skipping the values doesn't save time. Leaving it in probably makes the action clearer to humans.
0

Using tolist can work, followed by np.array will correctly return a (2,3) numpy array

np.array(df["a"].values.tolist())

returns

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

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.