python-pandas and databases like mysql

后端 未结 13 2072
有刺的猬
有刺的猬 2020-12-02 03:51

The documentation for Pandas has numerous examples of best practices for working with data stored in various formats.

However, I am unable to find any good examples

相关标签:
13条回答
  • 2020-12-02 04:11

    And this is how you connect to PostgreSQL using psycopg2 driver (install with "apt-get install python-psycopg2" if you're on Debian Linux derivative OS).

    import pandas.io.sql as psql
    import psycopg2
    
    conn = psycopg2.connect("dbname='datawarehouse' user='user1' host='localhost' password='uberdba'")
    
    q = """select month_idx, sum(payment) from bi_some_table"""
    
    df3 = psql.frame_query(q, conn)
    
    0 讨论(0)
  • 2020-12-02 04:22

    The same syntax works for Ms SQL server using podbc also.

    import pyodbc
    import pandas.io.sql as psql
    
    cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=servername;DATABASE=mydb;UID=username;PWD=password') 
    cursor = cnxn.cursor()
    sql = ("""select * from mytable""")
    
    df = psql.frame_query(sql, cnxn)
    cnxn.close()
    
    0 讨论(0)
  • 2020-12-02 04:25

    For Sybase the following works (with http://python-sybase.sourceforge.net)

    import pandas.io.sql as psql
    import Sybase
    
    df = psql.frame_query("<Query>", con=Sybase.connect("<dsn>", "<user>", "<pwd>"))
    
    0 讨论(0)
  • 2020-12-02 04:28

    For Postgres users

    import psycopg2
    import pandas as pd
    
    conn = psycopg2.connect("database='datawarehouse' user='user1' host='localhost' password='uberdba'")
    
    customers = 'select * from customers'
    
    customers_df = pd.read_sql(customers,conn)
    
    customers_df
    
    0 讨论(0)
  • 2020-12-02 04:29

    For recent readers of this question: pandas have the following warning in their docs for version 14.0:

    Warning: Some of the existing functions or function aliases have been deprecated and will be removed in future versions. This includes: tquery, uquery, read_frame, frame_query, write_frame.

    And:

    Warning: The support for the ‘mysql’ flavor when using DBAPI connection objects has been deprecated. MySQL will be further supported with SQLAlchemy engines (GH6900).

    This makes many of the answers here outdated. You should use sqlalchemy:

    from sqlalchemy import create_engine
    import pandas as pd
    engine = create_engine('dialect://user:pass@host:port/schema', echo=False)
    f = pd.read_sql_query('SELECT * FROM mytable', engine, index_col = 'ID')
    
    0 讨论(0)
  • 2020-12-02 04:29

    This should work just fine.

    import MySQLdb as mdb
    import pandas as pd
    con = mdb.connect(‘127.0.0.1’, ‘root’, ‘password’, ‘database_name’);
    with con:
     cur = con.cursor()
     cur.execute(“select random_number_one, random_number_two, random_number_three from randomness.a_random_table”)
     rows = cur.fetchall()
     df = pd.DataFrame( [[ij for ij in i] for i in rows] )
     df.rename(columns={0: ‘Random Number One’, 1: ‘Random Number Two’, 2: ‘Random Number Three’}, inplace=True);
     print(df.head(20))
    
    0 讨论(0)
提交回复
热议问题