如何循环遍历dbf文件,将dbf转换为dataframe,并为文件列表中的每个文件写出单独的输出?

2024-09-24 02:25:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要循环使用dbf文件(来自ArcGIS),转换为dataframe,排序,与另一个dataframe连接,然后根据在for循环中执行的操作的结果编写一个唯一的文件。当我这样做时,打印的文件是不正确的-它有nan和错误的值

当我一次使用一个文件时,它工作得很好,所以问题出在for循环中

def write_me_out(climate, Andict, Prdict):
with open('IXJ' + str(file), 'w') as fout:
    for idx, row in climate.iterrows():
        i = row.ROW     #climate.ROW[row]
        j = row.COLUMN_
        lu= row.LU_CODE
            if lu in [13, 113]:  
                if lu == 13:
                    cropcodes = Andict
                else:
                    cropcodes = Prdict
                for code,value in cropcodes.items():
                    #
                    if float(value) > 1E-30: 
                        fout.write('{0}\t{1}\t{2}\t{3}\n'.format(code, i, 
                        j, value))
            else:
                fout.write('{0}\t{1}\t{2}\t{3}\n'.format(lu, i, j, 
                '1.0'))


basepath=r'\folder'
infiles=[.dbf,.dbf',.dbf',.dbf',.dbf']


for file in infiles:
    fileloc=os.path.join(basepath, file)
    dbf = Dbf5(fileloc)
    df =dbf.to_dataframe()
    df=df.loc[df['ACTIVE_MOD'] == 1]  ##select only active cells
    df = df.rename(columns={i:'LU_CODE' for i in df.columns if 
    i.startswith('LU_')})
    df = df.loc[:,['ROW','COLUMN_','LU_CODE']]  
    dfLU = df.reset_index(drop=True)
    luConcat = pd.concat([dfzone, dfLU], axis=1)
    C = luConcat.loc[luConcat['ZONE_IHM']==0]
    Coastal = C.reset_index(drop=True)
    I = luConcat.loc[luConcat['ZONE_IHM']==1]
    Inland = I.reset_index(drop=True)

    write_me_out(Coastal, coastAnnDict, coastPrDict)
    write_me_out(Inland, inlandAnDict, inlandPrDict)    

Tags: 文件indataframedfforifoutloc