Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print numpy array without brackets

Tags:

python

numpy

I am in the process of trying to teach myself python so I am very new to this. My code is pretty simple. I just have a numpy array that I have randomly generated with integers. My code looks like this

arr = np.random.randint(100, size=(5,5))

print(arr)

When it prints it prints with brackets around it like this

[[98 87 45  5 67]
 [33 39  1 40 96]
 [97 55 85  2 65]
 [18 28 32 55 21]
 [96 46 14 87 28]]

How do I remove all of the brackets so it is only the numbers with the spaces between?

like image 384
Mike Avatar asked Dec 28 '25 22:12

Mike


2 Answers

What about using Pandas?

import numpy as np
import pandas as pd

arr = np.random.randint(100, size=(5,5))
df = pd.DataFrame(arr)


print(df.to_string(header=False, index=False))

 45  40  99   8  20
 29  18  54  52  51
 94  52  84  61  17
 44  54  38  48  62
  4  76  95  73  46
like image 152
Andreas K. Avatar answered Dec 31 '25 12:12

Andreas K.


Try:

for el in arr:
     print(' '.join(el.astype(str)))
like image 25
Grzegorz Skibinski Avatar answered Dec 31 '25 11:12

Grzegorz Skibinski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!