1
Good afternoon. I am comparing two given frames, data frame 1 has an sql query and data frame 2 a mysql query I want to compare the two. When the DF 1 record is not in DF 2 I want to save the result in another empty data frame, then load the data of that empty data frame in my table in mysql, so I can automate a process.
DF1 = Function for connection to sql server database and dataframe 1
DF2 = Function for connection to Mysql database and dataframe 2
import pymysql.cursors
import pyodbc
import pandas as pd
from sqlalchemy import create_engine
# Função para trazer os dados do Sql e jogar em um dataframe
def get_vendas_sqlserver():
    #Conexão com SQL Server
    connection = pyodbc.connect("DSN=SQLServer")  #autocommit=True
    try:
        # Conteúdo do data frame 1
        df = pd.read_sql_query("SELECT * FROM dw.dbo.vW_Vendateste123",connection,index_col=None,coerce_float=True, parse_dates= 'DataBaseContrato') 
        return df      
    finally:
        connection.close()
# Função para trazer os dados do mysql e jogar em um dataframe
def get_vendas_mysql():
    #Conexão Mysql
    cnxmysql = pymysql.connect(host='teste',
                                 user='teste',
                                 password='teste',
                                 db='dw')
    try:
        # Conteúdo do data frame 2
        df = pd.read_sql_query("SELECT * FROM ft_venda_copy", cnxmysql, index_col=None, coerce_float=True,
                               parse_dates='DataBaseContrato')
        return df
    finally:
        cnxmysql.close()
def compara_valores():
    cnx = create_engine('mysql+pymysql://teste:teste@teste/dw')
    df1 = get_vendas_sqlserver()
    df2 = get_vendas_mysql()
    #criando um dataframe vazio para guardar o resultado
    df_result = pd.DataFrame()
    print('df1: ',df1,' ', 'df2: ',df2, sep='\n')
    # o metodo iterrows retorna um tuple com o indice 
    for index, row in df1.iterrows():
    # verifica se este registro existe no DF do MySQL
        if row["IdContrato"] in df2["IdContrato"]:
            print("Contrato {0} encontrada no Mysql".format(row["Idcontrato"]))       
        else:
            print("Contrato {0} nao encontrada no Mysql".format(row["IdContrato"]))
        df_result.to_sql(con=cnx, name='ft_venda_copy', if_exists='append', index=False)
When I check the Result data frame it returns me empty
# adiciona o registro em um novo DF que vai ser usado para gravar no banco
df_result.append(row)
print(df_result)
df1:
      IdUnidade  IdContrato  IdProspect TipoContrato  ... 
0           276      9607.0         NaN          PCV  ...                 
1           328      8391.0         NaN          PCV  ...            
2           362     10233.0         NaN          PCV  ...            
3           309     10548.0         NaN          PCV  ...            
4           237      8849.0         NaN          PCV  ...            
5           308      8116.0         NaN          PCV  ...            0 
[6355 rows x 34 columns]
df2:
      IdUnidade  IdContrato  IdProspect TipoContrato  ... 
0           276      9607.0         NaN          PCV  ...                 
1           328      8391.0         NaN          PCV  ...            
2           362     10233.0         NaN          PCV  ...            
3           309     10548.0         NaN          PCV  ...            
4           237      8849.0         NaN          PCV  ...            
5           308      8116.0         NaN          PCV  ...            0 
[6355 rows x 34 columns]
You edited the question, added the command I suggested in the comments of my answer, but note that I commented "Run and play the result in your question." , where is the result?
– Sidon