转换为CSV文件时,我的Numpy阵列格式不正确

vxbzzdmp  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(161)

我试图将一个numpy的维数组(22227,2)转换成一个2列22227行的csv文件,但是,每当我尝试用下面的代码做这个时,我得到的是一行的列数是原来的两倍。

# using open function
sample_data = np.genfromtxt("wg1_regular_baseline_trial6.csv",delimiter=",", dtype=float)
    
sample_data = np.array(sample_data)
s_data=sample_data[1000:-4000]

#find mean of original data
adjusted_value=[]
mean_wave=sample_data[:,1].mean()

#adjust data for mean value
for i in range(len(sample_data)):
    value=sample_data[i]
    adjusted_value+=[[value[0],value[1]-mean_wave]]

#create array
adjusted_data = np.array(adjusted_value)
a_data=adjusted_data[1000:-4000]
print(np.shape(adjusted_data))

#create csv file
adjusted_data.tofile('BL_Trial6.csv', sep = ',')

打印调整后数据的数组时输出的代码为

[[0.00000000e+00 2.63267449e-02]
 [1.00000000e-02 2.64037449e-02]
 [2.00000000e-02 2.62877449e-02]
 ...
 [2.22240000e+02 9.33474486e-03]
 [2.22250000e+02 1.04347449e-02]
 [2.22260000e+02 1.15347449e-02]]

我试图使用numpy整形工具,但没有成功,因为它仍然打印一行与许多许多列。

50few1ms

50few1ms1#

对于简单的(2,2)数组:

In [251]: arr
Out[251]: 
array([[  1,   2],
       [  3, -40]])

比较tofile

In [252]: arr.tofile('test',sep=',')

In [253]: more test
1,2,3,-40

使用savetxt

In [254]: np.savetxt('test',arr, delimiter=',', fmt='%d')

In [255]: more test
1,2
3,-40

相关问题