Dataframe Python

Asked

Viewed 79 times

1

Good afternoon guys, I have a question and I wanted to ask for your help. I wanted to know how to make a Dataframe in python that stores the data of an sql view and also know how the syntax would look, thanks.

Note: I already made the connection to the database.

Here I make the connection to the database, then I wanted to play my view within a data frame.

import pyodbc 
import pandas as pd

#Conexão com Sql Server
connection = pyodbc.connect("DSN=SQLServer")  

if connection:
    print ("Conectado ao SQL!\n")


try:
    with connection.cursor() as cursor:
        #Utilizando o fetchal para trazer varios registros
        sql = "SELECT * FROM dw.dbo.vW_Vendas"
        cursor.execute(sql)
        tabela = cursor.fetchall()
        print(tabela)
finally:
    connection.close()
  • 1

    Ola Ytado would be good for your question if you add the code you made up to the moment. If you are talking about pandas.DataFrame maybe this will help you pandas.read_sql

  • 1

    Thanks I’ll take a look, added the code to improve the question

1 answer

3


df = pd.read_sql("SELECT * FROM dw.dbo.vW_Vendas", connection)
  • Thank you very much gave it right

Browser other questions tagged

You are not signed in. Login or sign up in order to post.