Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read sql query to pandas dataframe / python / django

I'm using this below in views.py to get app

from django.db import connection

def test(request):

    cursor = connection.cursor()
    sql = """
    SELECT x , n
    from table1 limit 10
    """
    cursor.execute(sql)
    rows = cursor.fetchall()

    # df1 = pd.read_sql_query(sql,cursor)  <==> not working ) 
    # df1.columns = cursor.keys()    <==> not working ) 

    return render(request, 'app/test.html',{ "row" : rows,})

I am able to print row and got a list of something like this below in test.html

row((x1,yvalue1),(x2,yvalue2) , .... ))

But what I'm trying to do is to get all data with its column name that I fetched and put into dataframe , hopefully to use something like this below :

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_sql_query.html#pandas.read_sql_query

like image 232
JPC Avatar asked Jan 04 '15 21:01

JPC


People also ask

Can pandas read SQL table?

pandas read_sql() function is used to read SQL query or database table into DataFrame. This is a wrapper on read_sql_query() and read_sql_table() functions, based on the input it calls these function internally and returns SQL table as a two-dimensional data structure with labeled axes.

Can you run SQL on a DataFrame?

Pandasql allows you to write SQL queries for querying your data from a pandas dataframe. This allows you to get around the normal requirement of having to learn a lot of Python in Pandas. Instead, you can simply write your regular SQL query within a function call and run it on a Pandas dataframe to retrieve your data!

What is Read_sql in Python?

Read SQL query or database table into a DataFrame. This function is a convenience wrapper around read_sql_table and read_sql_query (for backward compatibility). It will delegate to the specific function depending on the provided input.


1 Answers

I think aus_lacy is a bit off in his solution - first you have to convert the QuerySet to a string containing the SQL backing the QuerySet

from django.db import connection

query = str(ModelToRetrive.objects.all().query)
df = pandas.read_sql_query(query, connection)

Also there is a less memory efficient but still valid solution:

df = DataFrame(list(ModelToRetrive.objects.values('id','some_attribute_1','some_attribute_2'))) 
like image 124
JohnnyM Avatar answered Sep 28 '22 06:09

JohnnyM