Home > Net >  Pandas convert DataFrame values from array of arrays to array
Pandas convert DataFrame values from array of arrays to array

Time:08-05

Sometimes DataFrame columns is an array of arrays:

df['value'].values
    array([[1.51808096e 11],
           [1.49119648e 11],
    ...
           [1.18009284e 11],
           [1.44851665e 11]])

And sometimes a regular array:

df['value'].values
    array([1.51808096e 11,
           1.49119648e 11,
    ...
           1.18009284e 11,
           1.44851665e 11])

DataFrame created with a csv will sometimes give one format, and sometimes the other. This causes issues. Using df['value'].values = df['value'].values.flatten() does not work.

CodePudding user response:

I was having a heck of a time recreating your data to have an array of arrays print out from my column. But maybe using reshape and then grabbing the first index will work for you. Like:

# With just an array
arr = np.array([[1,2,3,4,5], [1,2,3,4,5], [1,2,3,4,5]])
arr.reshape(1,-1)[0]

Output:

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

Or with a dataframe example:

lst = [['tom', 'reacher', 25], ['krish', 'pete', 30], 
       ['nick', 'wilson', 26], ['juli', 'williams', 22]] 
      
df = pd.DataFrame(lst, columns =['FName', 'LName', 'Age']) 
df.values.reshape(1,-1)[0]

Output:

array(['tom', 'reacher', 25, 'krish', 'pete', 30, 'nick', 'wilson', 26,
       'juli', 'williams', 22], dtype=object)

If this doesn't work, could you add a minimal working example to your question to recreate your dataframe?

CodePudding user response:

list comprehension would do the job

array = [v for e in [x for x in df['values']] for v in e]
  • Related