0

Here is the code

import numpy as np
import pandas as pd
a = [[[1, 2, 3, 4, 5] , [6, 7, 7, 8, 10], [8, 7, 99, 89, 18]]]
b = np.asarray(a)

If I say print(b) I get:

[[[ 1  2  3  4  5]
  [ 6  7  7  8 10]
  [ 8  7 99 89 18]]]

I want to print the content of b one array after the other such as:

[ 1  2  3  4  5]
[ 6  7  7  8 10]
[ 8  7 99 89 18]

So I can save it in a csv format in different columns. I am using python3.7.10

  • 1
    I suggest you to have a look at this [post](https://stackoverflow.com/questions/6081008/dump-a-numpy-array-into-a-csv-file) – XtianP Apr 29 '21 at 17:09

2 Answers2

0

You can do it like this:

import numpy as np
import pandas as pd
a = [[[1, 2, 3, 4, 5] , [6, 7, 7, 8, 10], [8, 7, 99, 89, 18]]]
b = np.asarray(a[0])
for i in b:
    print(i)
zoldxk
  • 2,632
  • 1
  • 7
  • 29
0

You could print them on screen as you want like this:

import numpy as np
import pandas as pd
a = [[[1, 2, 3, 4, 5] , [6, 7, 7, 8, 10], [8, 7, 99, 89, 18]]]
b = np.asarray(a)

for i in range(b.shape[1]):
    print(b[0,i])

To save them on a file as a matrix you could do this:

np.savetxt("file.csv", b[0], delimiter=",")

The file after this will look like this:

1.000000000000000000e+00,2.000000000000000000e+00,3.000000000000000000e+00,4.000000000000000000e+00,5.000000000000000000e+00
6.000000000000000000e+00,7.000000000000000000e+00,7.000000000000000000e+00,8.000000000000000000e+00,1.000000000000000000e+01
8.000000000000000000e+00,7.000000000000000000e+00,9.900000000000000000e+01,8.900000000000000000e+01,1.800000000000000000e+01