Unable to create a second dataframe python pandas

十年热恋 提交于 2019-12-13 15:34:18

问题


My second data frame is not loading values when i create it. Any help with why it is not working? When i make my cursor a list, it has a bunch of values in it, but for whatever reason when i try to do a normal data frame load with pandas a second time, it does not work.

My code:

    conn = pyodbc.connect(constr, autocommit=True)
    cursor = conn.cursor()
    secondCheckList = []
    checkCount = 0
    maxValue = 0
    strsql = "SELECT * FROM CRMCSVFILE"
    cursor = cursor.execute(strsql)
    cols = []
    SQLupdateNewIdField = "UPDATE CRMCSVFILE SET NEW_ID = ? WHERE Email_Address_Txt = ? OR TELEPHONE_NUM = ? OR DRIVER_LICENSE_NUM = ?"
    for row in cursor.description:
        cols.append(row[0])
    df = pd.DataFrame.from_records(cursor)
    df.columns = cols
    newIdInt = 1
    for row in range(len(df['Email_Address_Txt'])):
        #run initial search to figure out the max number of records. Look for email, phone, and drivers license, names have a chance not to be unique
        SQLrecordCheck = "SELECT * FROM CRMCSVFILE WHERE Email_Address_Txt = '" + str(df['Email_Address_Txt'][row]) + "' OR TELEPHONE_NUM = '" + str(df['Telephone_Num'][row]) + "' OR DRIVER_LICENSE_NUM = '" + str(df['Driver_License_Num'][row]) + "'"
##        print(SQLrecordCheck)
        cursor = cursor.execute(SQLrecordCheck)
## maxValue is indeed a list filled with records
            maxValue =(list(cursor))
## THIS IS WHERE PROBLEM OCCURS
        tempdf = pd.DataFrame.from_records(cursor)

回答1:


Why not just use pd.read_sql_query("your_query", conn) this will return the result of the query as a dataframe and requires less code. Also you set cursor to cursor.execute(strsql) at the top and then you are trying to call execute on cursor again in your for loop but you can no longer call execute on cursor you will have to set cursor = conn.cursor() again.



来源:https://stackoverflow.com/questions/40118259/unable-to-create-a-second-dataframe-python-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!