Python Forum
How to concatenate nested numpy arrays?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to concatenate nested numpy arrays?
#1
I am trying to work with matlab files on python and I managed to extract the data needed and it is loaded in 1D array form. But I now want to combine these 1d array vertically to form a table so that I can then transform them into a Dataframe.

How can I best do this? Below is how 2 of the arrays look like.. I have 15 arrays like this which I wish to combine.

array 1
array([[[[1.00000e+00],
[2.00000e+00],
[3.00000e+00],
...,
[6.09766e+05],
[6.09767e+05],
[6.09768e+05]]]])

shape: (1, 1, 609768, 1)


array([[[[3.89786921e-02],
[5.04933377e+00],
[1.00494220e+01],
...,
[1.18832843e+06],
[1.18833845e+06],
[1.18834845e+06]]]])
shape: (1, 1, 609768, 1)

Any ideas on how best I can process this data so that I get a nice looking table in the end is most welcome :) Thank you.
Reply
#2
Try:
np.concatenate((first_array, second_array), 2)
   
You can also flatten both arrays:

shape = (1, 1, 609768, 1)
flat_a = np.zeros(shape).flatten()
flat_b = np.ones(shape).flatten()
flat_ab = np.concatenate((flat_a, flat_b))
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
(Apr-16-2019, 06:08 PM)DeaD_EyE Wrote: Try:
np.concatenate((first_array, second_array), 2)
You can also flatten both arrays:

shape = (1, 1, 609768, 1)
flat_a = np.zeros(shape).flatten()
flat_b = np.ones(shape).flatten()
flat_ab = np.concatenate((flat_a, flat_b))

Thanks for the suggestion but when I tried both ways it seems to be resulting in 1 dimensional array with now shape (1219536,) whereas what I am trying to achieve is to have the vertically combined so it should ideally have a shape of (1219536,2)

(Apr-16-2019, 07:00 PM)python_newbie09 Wrote:
(Apr-16-2019, 06:08 PM)DeaD_EyE Wrote: Try:
 np.concatenate((first_array, second_array), 2)
You can also flatten both arrays:
 shape = (1, 1, 609768, 1) flat_a = np.zeros(shape).flatten() flat_b = np.ones(shape).flatten() flat_ab = np.concatenate((flat_a, flat_b)) 
Thanks for the suggestion but when I tried both ways it seems to be resulting in 1 dimensional array with now shape (1219536,) whereas what I am trying to achieve is to have the vertically combined so it should ideally have a shape of (609768,2)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem adding two numpy arrays djf123 2 2,042 Aug-09-2022, 08:31 PM
Last Post: deanhystad
  how to join by stack multiple types in numpy arrays caro 1 1,102 Jun-20-2022, 05:02 PM
Last Post: deanhystad
  numpy.dot() result different with classic computation for large-size arrays geekgeek 5 1,831 Jan-25-2022, 09:45 PM
Last Post: Gribouillis
  Concatenate str JohnnyCoffee 2 2,879 May-01-2021, 03:58 PM
Last Post: JohnnyCoffee
  Two numpy arrays Sandra2312 1 1,777 Jan-18-2021, 06:10 PM
Last Post: paul18fr
  Save Numpy Array in Another Numpy Array (not Concatenate!) quest 1 1,822 Nov-06-2020, 01:06 PM
Last Post: quest
  numpy in1d with two simple arrays claw91 3 2,527 Sep-21-2020, 12:43 PM
Last Post: scidam
  Concatenate two dataframes moralear27 2 1,836 Sep-15-2020, 08:04 AM
Last Post: moralear27
  Type coercion with Numpy arrays Mark17 2 2,488 Jul-24-2020, 02:04 AM
Last Post: scidam
  filling and printing numpy arrays of str pjfarley3 4 3,206 Jun-07-2020, 09:09 PM
Last Post: pjfarley3

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020