KrunalParmar KrunalParmar - 3 months ago 7
Python Question

Concatenating two string columns of numpy array into single column in python

I have a numpy array as following :

2016-07-02 10:55:01
2016-07-02 10:55:01
2016-07-02 10:55:01
2016-07-02 17:01:34
2016-07-02 17:01:34
2016-07-02 16:59:52
2016-07-02 17:01:34
2016-07-02 16:59:52
2016-07-02 16:59:52
2016-07-02 10:40:00
2016-07-02 12:01:14


this are two columns of array. date and time. but i want both into a single column concatenated by '\t'. both the values are in string format.

I did it by a loop as follows, but that is a bad idea and taking much time. :

for D in Data:
Data2 = np.append(Data2,np.array(D[0]+"\t"+D[1]))


Please suggest an efficient solution.

Answer

It's all about searching for a vectorized flavour of what you wanted to do (apply function to each row of array in your case):

import numpy as np


np.apply_along_axis(lambda d: d[0] + '\t' + d[1], 1, arr)
Comments