2

I'm a complete beginner in python, and my problem is that i have 4 arrays, with x items:

persons_id = [ 78694, 51203, ... ]
dates = [ '20072017', '19072017', ... ]
codes = [ 1500, 0606, ... ]
ranges = [ 70, 60, ... ]

What i'm trying to do is ( in a loop ) to produce that kind of output:

reporting = numpy.array([persons_id[0],
                        dates[0],
                        codes[0],
                        ranges[0]],
                        [persons_id[1],
                        dates[1],
                        codes[1],
                        ranges[1]],
                        [...])

Thank you in advance

Julien8
  • 29
  • 7

1 Answers1

4

Option 1
np.vstack

np.vstack((persons_id, dates, codes, ranges)).T

array([['78694', '20072017', '1500', '70'],
       ['51203', '19072017', '606', '60']],
      dtype='<U21')

Option 2
np.stack(..., axis=1)

np.stack((persons_id, dates, codes, ranges), axis=1)

array([['78694', '20072017', '1500', '70'],
       ['51203', '19072017', '606', '60']],
      dtype='<U21')
cs95
  • 379,657
  • 97
  • 704
  • 746
  • You're solution would have been perfect if i was sure that all my arrays would have the same shape. – Julien8 Oct 26 '17 at 11:53
  • @dearblackswan Check this link to see how to pad lists: https://stackoverflow.com/questions/1277278/python-zip-like-function-that-pads-to-longest-length. However, if you want to just drop extra values, that's even simpler. – cs95 Oct 26 '17 at 11:58