将numpy写入csv文件时出错

时间:2015-07-14 11:02:38

标签: python python-2.7 numpy scipy

我想将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

1 个答案:

答案 0 :(得分:3)

您必须指定要保存的数据格式。默认情况下,它采用浮动格式

np.savetxt(path + 'testSearchStream9.csv', new, delimiter=",", fmt="%s")

请参阅SciPy文档的savetxt function部分。