Python:如何处理CSV中的缺失值?
作者:互联网
我有一个给定的CSV示例,如下所示:
ID,ID_TYPE,OB_DATE,VERSION_NUM,MET_DOMAIN_NAME,OB_END_CTIME,OB_DAY_CNT,SRC_ID,REC_ST_IND,PRCP_AMT,OB_DAY_CNT_Q,PRCP_AMT_Q,METO_STMP_TIME,MIDAS_STMP_ETIME,PRCP_AMT_J
90, RAIN, 2006-01-01 00:00,1, WADRAIN,900,1,24109,1011,0,0,6, 2006-01-17 09:04,0,
150, RAIN, 2006-01-01 00:00,1, DLY3208,900,1,30747,1011,0,0,6, 2006-01-09 13:21,3,
174, RAIN, 2006-01-01 00:00,1, WADRAIN,900,1,24775,1011,0.2,0,6, 2006-01-17 09:04,0,
我想确定CSV中每个给定日期的工作日.我实现的代码如下所示:
import csv
from datetime import datetime as dt
csv_file = open('raindata.csv')
csv_reader = csv.DictReader(csv_file)
field_names = list(csv_reader.fieldnames)
if 'WEEKDAY' in field_names:
print "data has error"
elif 'RECWEEKDAY' in field_names:
print "data has error"
else:
field_names.insert(field_names.index('OB_DATE') + 1, 'WEEKDAY')
field_names.insert(field_names.index('METO_STMP_TIME') + 1, 'RECWEEKDAY')
def get_weekday(ob_date):
return dt.strptime(ob_date, ' %Y-%m-%d %H:%M').strftime('%A')
output = open('raindata.csv','w')
csv_writer = csv.DictWriter(output, field_names)
csv_writer.writeheader()
for row in csv_reader:
row['WEEKDAY'] = get_weekday(row['OB_DATE'])
row['RECWEEKDAY'] = get_weekday(row['METO_STMP_TIME'])
csv_writer.writerow(row)
我的脚本可以正常运行并给出正确的结果,但是在OB_DATE列和METO_STMP_TIME列中缺少Date值的地方,它失败.
如何更改现有代码,以便对于空白的Date值,相应的Weekday值也为空白?
解决方法:
对于其他替代方法,您可以修改get_weekday函数以处理空白日期.
def get_weekday(ob_date):
return dt.strptime(ob_date, ' %Y-%m-%d %H:%M').strftime('%A') if ob_date.strip() else ""
标签:csv,python-2-x,python 来源: https://codeday.me/bug/20191027/1942792.html