我的第二个数据框在创建它时没有加载值。有什么帮助可以解释为什么它不起作用吗?当我将光标设置为列表时,它里面有一堆值,但是无论出于何种原因,当我尝试第二次使用 pandas 进行正常的数据帧加载时,它都不起作用。
我的代码:
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)
最佳答案
为什么不直接使用 pd.read_sql_query("your_query", conn) 这会将查询结果作为数据帧返回,并且需要更少的代码。另外,您将光标设置为顶部的cursor.execute(strsql),然后尝试在for循环中再次调用光标上的执行,但您不能再在光标上调用执行,您必须设置光标= conn.cursor()再次。
关于python - 无法创建第二个数据框 python pandas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40118259/