Python Forum
how to join by stack multiple types in numpy arrays
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to join by stack multiple types in numpy arrays
#1
import numpy as np

arr1 = np.array([1, 2, 3, 4, 5, 6, 7])
arr2 = np.array(['n', 'b', 'c', 'y', 'f', 'j', 'p'])

l = np.stack((arr1, arr2), axis=1)
print(l)
the output is:
[['1','n'] ['2', 'b'] ['3','c'] ['4','y']['5','f'] ['6','j'] ['7','p']]

i would like that the types will be like the input so that the output will be:
[[1,'n'] [2, 'b'] [3,'c'] [4,'y'][ 5,'f'] [6,'j'] [7,'p']]
Reply
#2
numpy arrays have a data type. All elements in the array are that type. You cannot make a numpy array that has strings and integers.

That said, you can have a numpy array of structures.
import numpy as np
from numpy.lib import recfunctions

arr1 = np.array([1, 2, 3, 4, 5, 6, 7])
arr2 = np.array(["n", "b", "c", "y", "f", "j", "p"])

l = recfunctions.merge_arrays((arr1, arr2))
print(l)
Output:
[(1, 'n') (2, 'b') (3, 'c') (4, 'y') (5, 'f') (6, 'j') (7, 'p')]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem adding two numpy arrays djf123 2 2,104 Aug-09-2022, 08:31 PM
Last Post: deanhystad
  numpy.dot() result different with classic computation for large-size arrays geekgeek 5 1,902 Jan-25-2022, 09:45 PM
Last Post: Gribouillis
  Two numpy arrays Sandra2312 1 1,814 Jan-18-2021, 06:10 PM
Last Post: paul18fr
  The difference between os.path.join( and os.sep.join( Pedroski55 2 9,486 Nov-17-2020, 08:38 AM
Last Post: Pedroski55
  numpy in1d with two simple arrays claw91 3 2,605 Sep-21-2020, 12:43 PM
Last Post: scidam
  Deleting multiple variables/arrays Robotguy 0 1,507 Aug-18-2020, 09:56 PM
Last Post: Robotguy
  Type coercion with Numpy arrays Mark17 2 2,534 Jul-24-2020, 02:04 AM
Last Post: scidam
  filling and printing numpy arrays of str pjfarley3 4 3,315 Jun-07-2020, 09:09 PM
Last Post: pjfarley3
  Installing Numpy multiple locations dwhe 4 3,154 Dec-21-2019, 02:02 AM
Last Post: dwhe
  How to concatenate nested numpy arrays? python_newbie09 2 4,799 Apr-16-2019, 07:00 PM
Last Post: python_newbie09

Forum Jump:

User Panel Messages

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