pandasjupyter-notebooktry-exceptpymssqlread-sql

I have difficulty to get final concat dataframe by try except for pymssql


for i in Com_Tables:
try:
    frames = [pd.read_sql(f"SELECT * FROM {i}", conn).assign(ComId_Code = i)]

except:
    pass

finally:
    dfxilnex = pd.concat(frames)

i am expecting output to concat all data from each item in list however the result is showing only data for last item in list


Solution

  • You are not appending anything so there is nothing to concatnate. try this:

    frames = []
    for i in Com_Tables:
        try:
            frames.append(pd.read_sql(f"SELECT * FROM {i}", conn).assign(ComId_Code = i))
    
        except:
            pass
    
    dfxilnex = pd.concat(frames)