将 numpy 写入 csv 文件时出错
error when writing numpy into csv file
我想将一个 numpy 数组保存到一个 CSV 文件中。我的代码给我一个错误:
import os
import pandas as pd
import numpy as np
path ='/Users/mas/Documents/workspace/Avito/input/' # path to testing file
sample = pd.read_csv(path + 'sampleSubmission.csv')
index = sample.ID.values - 1
test = np.array(pd.read_csv(path + 'dataset5test.csv'))
#print new[0:10,:]
new = test[index,:]
np.savetxt(path + 'testSearchStream9.csv', new, delimiter=",")
os.system('say "Done"')
这是我得到的错误:
np.savetxt(path + 'testSearchStream9.csv', new, delimiter=",")
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/lib/npyio.py", line 1061, in savetxt
fh.write(asbytes(format % tuple(row) + newline))
TypeError: float argument required, not str
您必须指定要保存的数据格式。默认情况下,格式为浮点数
np.savetxt(path + 'testSearchStream9.csv', new, delimiter=",", fmt="%s")
请参阅 SciPy 文档的 savetxt function 部分。
我想将一个 numpy 数组保存到一个 CSV 文件中。我的代码给我一个错误:
import os
import pandas as pd
import numpy as np
path ='/Users/mas/Documents/workspace/Avito/input/' # path to testing file
sample = pd.read_csv(path + 'sampleSubmission.csv')
index = sample.ID.values - 1
test = np.array(pd.read_csv(path + 'dataset5test.csv'))
#print new[0:10,:]
new = test[index,:]
np.savetxt(path + 'testSearchStream9.csv', new, delimiter=",")
os.system('say "Done"')
这是我得到的错误:
np.savetxt(path + 'testSearchStream9.csv', new, delimiter=",")
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/lib/npyio.py", line 1061, in savetxt
fh.write(asbytes(format % tuple(row) + newline))
TypeError: float argument required, not str
您必须指定要保存的数据格式。默认情况下,格式为浮点数
np.savetxt(path + 'testSearchStream9.csv', new, delimiter=",", fmt="%s")
请参阅 SciPy 文档的 savetxt function 部分。