美文网首页
python把缺测的时间行找出来并补为nan

python把缺测的时间行找出来并补为nan

作者: Aerosols | 来源:发表于2019-11-02 00:50 被阅读0次

批量读取的txt文件有缺测行,降缺测时间找出来并补为nan

import os
import pandas as pd
import numpy as np
import glob

path_root=r'E:\meteorological data\190701-091023'
date = pd.date_range('2019-07-01 01:00:00','2019-10-10 23:00:00',freq='H')

frame = []
for itime in date:
    filename = 'surf_hour_' + itime.strftime('%Y%m%d%H') + '.txt'
    namestr = os.path.join(path_root,filename)
#    if os.path.exists(namestr):
        #print(filename)
#        try:
#            df = pd.read_csv(namestr,sep='\s+',header=0,index_col=0)
#            ser = df.loc[54399]
#        except:
#            ser = pd.Series([np.nan]*15, name = 54399,index=ser.index)
#    else:
#        ser = pd.Series([np.nan]*15, name = 54399,index=ser.index)
    
    try:
        df = pd.read_csv(namestr,sep='\s+',header=0,index_col=0)
        ser = df.loc[54399]        
    except:
        ser = pd.Series([np.nan]*15, name = 54399,index=ser.index)    
    frame.append(ser)
df = pd.DataFrame(frame, index =date )

相关文章

网友评论

      本文标题:python把缺测的时间行找出来并补为nan

      本文链接:https://www.haomeiwen.com/subject/gtccbctx.html