-1

"simulation" is a 1D numpy array with 237,569 elements (numbers) that i want to convert to a 673 x 353 (673 lines, 353 columns) either array, text, or ascii file

example

[1 2 3 4 5 6 7 8 9 10 11 12]

need it to be

 1  2  3
 4  5  6
 7  8  9
10 11 12

a similar issue was discussed in this question

the number of columns tho was 3 instead of a relatively large number (353)

I tried

import numpy as np
import pandas as pd

simulation = pd.read_csv("simulation.csv",header=None)
simulation_array = np.reshape(simulation, (673, 353))

# Save the array to a text file
np.savetxt("simulation_2D.txt", simulation_array, fmt="%d", delimiter=" ")

! had this error

Traceback (most recent call last):
  File "C:\Users\hp\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\internals\managers.py", line 1671, in create_block_manager_from_blocks
    make_block(values=blocks[0], placement=slice(0, len(axes[0])))
  File "C:\Users\hp\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\internals\blocks.py", line 2744, in make_block
    return klass(values, ndim=ndim, placement=placement)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\internals\blocks.py", line 131, in __init__
    f"Wrong number of items passed {len(self.values)}, "
ValueError: Wrong number of items passed 353, placement implies 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\hp\Desktop\pythonProject1\main.py", line 7, in <module>
    simulation_array = np.reshape(simulation, (673, 353))
  File "<__array_function__ internals>", line 6, in reshape
  File "C:\Users\hp\AppData\Local\Programs\Python\Python36\lib\site-packages\numpy\core\fromnumeric.py", line 299, in reshape
    return _wrapfunc(a, 'reshape', newshape, order=order)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python36\lib\site-packages\numpy\core\fromnumeric.py", line 55, in _wrapfunc
    return _wrapit(obj, method, *args, **kwds)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python36\lib\site-packages\numpy\core\fromnumeric.py", line 48, in _wrapit
    result = wrap(result)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\generic.py", line 1790, in __array_wrap__
    return self._constructor(result, **d).__finalize__(
  File "C:\Users\hp\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\frame.py", line 497, in __init__
    mgr = init_ndarray(data, index, columns, dtype=dtype, copy=copy)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\internals\construction.py", line 234, in init_ndarray
    return create_block_manager_from_blocks(block_values, [columns, index])
  File "C:\Users\hp\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\internals\managers.py", line 1681, in create_block_manager_from_blocks
    raise construction_error(tot_items, blocks[0].shape[1:], axes, e)
ValueError: Shape of passed values is (673, 353), indices imply (237569, 1)

Process finished with exit code 1
wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • Welcome to Stack Overflow! Check out the [tour]. [Please don't post pictures of text](https://meta.stackoverflow.com/q/285551/4518341). I transcribed it for you. Check out [ask] if you want more tips. – wjandrea Dec 20 '22 at 21:48
  • Why do you say "text or ASCII"? Those aren't mutually exclusive. – wjandrea Dec 20 '22 at 21:51
  • What do you mean by "array file"? Some sort of binary format? I'm not a NumPy expert myself. – wjandrea Dec 20 '22 at 21:51
  • consider "simulation.csv" to be an excel file with one column and 237569 line that i want to convert to (673,353) csv (or txt file) – maelaynayn el baida Dec 20 '22 at 21:59
  • In your code, `simulation` is **not** a 1D NumPy array, it's a Pandas DataFrame. Have you considered using [`np.loadtxt()`](https://numpy.org/doc/stable/reference/generated/numpy.loadtxt.html) instead of `pd.read_csv()`? Or maybe [converting the DataFrame into a NumPy array](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_numpy.html)? – wjandrea Dec 20 '22 at 22:02

1 Answers1

0

that's it @wjandrea thank you

forgot that i save.txt'ed my numpy array to a csv file before and now it's a dataframe, i had to convert the data frame back to a numpy array before reshaping it by using simul=simulation.to_numpy()

import numpy as np
import pandas as pd

simulation = pd.read_csv("simulation.csv",header=None)
simul=simulation.to_numpy()

simulation_array = np.reshape(simul, (673,353))
#np.savetxt("simulation2D.txt", simulation_array, fmt="%1.3f", delimiter=" ")

print(simulation_array.shape)

it worked thank you