python 将1D numpy数组转换为2D txt(或)ascii文件

q3qa4bjr  于 2022-12-21  发布在  Python
关注(0)|答案(1)|浏览(127)

"simulation"是一个包含237,569个元素(数字)的1D numpy数组,我要将其转换为673 x 353(673行,353列)的数组、文本或ASCII文件
范例

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

我需要它
this question中也讨论了类似的问题
列的数目Th0是3而不是相对大的数目(353)。
我试过了

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=" ")

!有此错误

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
wooyq4lh

wooyq4lh1#

就是这样@wjandrea谢谢
我忘了我以前将numpy数组保存到csv文件中,现在它是一个 Dataframe ,我必须将 Dataframe 转换回numpy数组,然后使用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)

成功了谢谢

相关问题