前提・実現したいこと
jupyternotebookでcsvファイルを書き出そうとしています。
numpy配列をCSV出力するという参考記事を読んで実装しました。
発生している問題・エラーメッセージ
以下のエラーを解決する方法がわからず困っています。
<class 'list'> --------------------------------------------------------------------------- TypeError Traceback (most recent call last) /Users/username/anaconda/lib/python3.6/site-packages/numpy/lib/npyio.py in savetxt(fname, X, fmt, delimiter, newline, header, footer, comments, encoding) 1386 try: -> 1387 v = format % tuple(row) + newline 1388 except TypeError: TypeError: must be real number, not numpy.str_ During handling of the above exception, another exception occurred: TypeError Traceback (most recent call last) <ipython-input-11-ae15ad0fca57> in <module>() 2 print(type(result)) 3 #csvファイルとして保存 ----> 4 np.savetxt('result.csv',result,delimiter=',') /Users/username/anaconda/lib/python3.6/site-packages/numpy/lib/npyio.py in savetxt(fname, X, fmt, delimiter, newline, header, footer, comments, encoding) 1389 raise TypeError("Mismatch between array dtype ('%s') and " 1390 "format specifier ('%s')" -> 1391 % (str(X.dtype), format)) 1392 fh.write(v) 1393 TypeError: Mismatch between array dtype ('<U1') and format specifier ('%.18e')
該当のソースコード
python
1import io 2import numpy as np 3np.set_printoptions(threshold=np.inf) 4import pandas as pd 5 6result = ['a', 'b', 'c', 'd'] 7print(type(result)) 8#csvファイルとして保存 9np.savetxt('result.csv',result,delimiter=',')
###試したこと
ndarrayでもエラー内容は変わりませんでした。
python
1result = np.array(result) 2print(type( 3result = ['a', 'b', 'c', 'd'] 4result = np.array(result) 5print(type(result)) 6#csvファイルとして保存 7np.savetxt('result.csv',result,delimiter=',') 8#出力 9<class 'numpy.ndarray'>
補足情報(FW/ツールのバージョンなど)
python3.6
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。