【发布时间】:2016-12-30 01:20:27
【问题描述】:
我正在尝试将 .csv 中的 NULL 值转换为 NaN,然后使用这些编辑保存文件。下面代码中的f 在数据中的正确位置具有NaN 值。但是,我无法将其保存为 .csv。错误打印在代码下方。
#take .csv with NULL and replaces with NaN - write numerical and NaN values to .csv
import csv
import numpy as np
import pandas
f = pandas.read_csv('C:\Users\mmso2\Google Drive\MABL Wind\_Semester 2 2016\Wind Farm Info\DataB\DataB - Copy.csv')#convert file to variable so it can be edited
outfile = open('C:\Users\mmso2\Google Drive\MABL Wind\_Semester 2 2016\Wind Farm Info\DataB\DataB - NaN1.csv','wb')#create empty file to write to
writer = csv.writer(outfile)#writer will write when given the data to write below
result = f[f is 'NULL'] = np.nan
writer.writerows(f)
错误:
Traceback (most recent call last):
File "C:/Users/mmso2/Google Drive/MABL Wind/_Semester 2 2016/_PGR Training/CENTA/MATLAB/In class ex/SAR_data/gg_nan.py", line 12, in <module>
writer.writerows(f)
_csv.Error: sequence expected
【问题讨论】:
-
为什么不
f.to_csv(...)? -
我以前没听说过 - 谢谢!
标签: python python-2.7 csv nan