20

I wrote the following script to load data from CSV file in numpy array form:

import numpy
sample = numpy.genfromtxt('sample_cor.csv', delimiter = ',')
print sample

and this sample looked like:

[[ 259463.392 2737830.062 ]
 [ 255791.4823 2742050.772 ]
 [ 249552.4949 2746152.328 ]
 [ 247925.1228 2746422.143 ]
 [ 262030.4697 2728966.229 ]
 [ 260462.1936 2731412.856 ]
 [ 260644.0281 2735003.027 ]
 [ 268588.7974 2732835.097 ]]

now I want to extract every row in this array to string with a comma, for example, I expected row 1 to be converted to 259463.392,2737830.062, row 2 to be 255791.4823,2742050.772, and so on.

I tried the code below:

ss = numpy.array_str(sample[0])
print ss
print type(ss)

and got the result maybe not what I want,

[ 259463.392 2737830.062]
<type 'str'>

(I used coords = '259467.2,2737833.87' and got the string form which was what I want: 259467.2,2737833.87)

How to convert elements in a numpy array to string with a comma?

asked Oct 26, 2016 at 12:05
1
  • 2
    The form without the , is just the normal way that numpy arrays are displayed. It helps distinguish them from Python lists. The , is not an integral part of either a list or an array. It's just a display feature. Commented Oct 26, 2016 at 17:59

1 Answer 1

18

Here's an approach using join method -

[",".join(item) for item in a.astype(str)]

Sample run -

In [141]: a
Out[141]: 
array([[ 259463.392 , 2737830.062 ],
 [ 255791.4823, 2742050.772 ],
 [ 249552.4949, 2746152.328 ],
 [ 247925.1228, 2746422.143 ],
 [ 262030.4697, 2728966.229 ],
 [ 260462.1936, 2731412.856 ],
 [ 260644.0281, 2735003.027 ],
 [ 268588.7974, 2732835.097 ]])
In [142]: [",".join(item) for item in a.astype(str)]
Out[142]: 
['259463.392,2737830.062',
 '255791.4823,2742050.772',
 '249552.4949,2746152.328',
 '247925.1228,2746422.143',
 '262030.4697,2728966.229',
 '260462.1936,2731412.856',
 '260644.0281,2735003.027',
 '268588.7974,2732835.097']
answered Oct 26, 2016 at 12:11
Sign up to request clarification or add additional context in comments.

5 Comments

thank you for the advice, but I have a little question that why the showing of mine of numpy array in the post lack comma?
@Heinz Hmm if the input is a NumPy array, it should. Or else, you could try print(repr(a)).
Thank you, repr() returns the having-comma array.
@Divakar is there a "pure" numpy solution to this and would that make it faster for large datasets? Thank you
I am very late to the party, though! hey @Heinz, since you're printing that Numpy array via print(), it doesn't display the commas. Try simply running the variable in a single Jupyter cell - then, elements display with commas.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.