2

I have a DataFrame with a column including numpy arrays:

df = pd.DataFrame({'Arrays': [np.array([1, 2, 3]), 
                           np.array([4, 5, 6]),
                           np.array([7, 8, 9])]})`

I need to extract the data in a way that I get a 2-dimensional array out of it. Like this:

array([[1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]])`

But df.values results in an array containing a list of arrays:

array([[array([1, 2, 3])],
    [array([4, 5, 6])],
    [array([7, 8, 9])]], dtype=object)`

Is there an operator for this kind of problem or do I need to loop over all entries?

yatu
  • 86,083
  • 12
  • 84
  • 139
MetalPesto
  • 49
  • 4
  • Are you sure it's a duplicate @coldspeed? The question you pointed to is about flattening into 1-dimensional array whereas the OP clearly mentions 2 dimensions. – ayorgo Dec 13 '18 at 17:44
  • @ayorgo Yes, I'm sure. – cs95 Dec 13 '18 at 17:45

2 Answers2

3

Use a combination of concatenate and flatten to get a single flattened array out of array of arrays

np.concatenate(df.values.flatten()) 

Alternative is to use ravel

np.concatenate(df.values.ravel())  

Example

import pandas as pd
import numpy as np
df = pd.DataFrame({'Arrays': [np.array([1, 2, 3]), 
                               np.array([4, 5, 6]),
                               np.array([7, 8, 9])]})

np.concatenate(df.values.flatten())

# array([1, 2, 3, 4, 5, 6, 7, 8, 9])

Finally, to get a 2d array, you can simply use reshape as

np.reshape(flattened_array, (len(df),len(df)))
Sheldore
  • 37,862
  • 7
  • 57
  • 71
0

Apart from

df.applymap(list).values.ravel()

you can also do

df.applymap(list).values.reshape((-1,))

giving you

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

or, otherwise

df.applymap(list)['Arrays'].tolist()

resulting in

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
ayorgo
  • 2,803
  • 2
  • 25
  • 35