简体   繁体   中英

converting a 1D numpy array to a 2D txt (or) ascii file

"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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM