When using Pandas to analyze data, besides reading text-based data, such as Excel and CSV files, database reading is also involved. The usual solution is Pandas+ SQLAlchemy.
The following is a record of some operations, as a memo.
Connecting to a database using sqlalchemy
Engine translates to engine, the car is driven by the engine, and SQLAlchemy is driven by the Engine, which maintains a connection pool (Pool) object and a dialect (Dialect). Dialect simply means whether you are connecting to MySQL or Oracle or PostgreSQL or some other database.
Connection example.
The databases supported by SQLAlchemy include: PostgreSQL, MySQL, MariaDB, Oracle, Microsoft SQL Server, SQLite, etc. The specific connection methods are not detailed here. Some of the most error-prone database passwords have special characters, which need to be coded and processed before submission on the day.
Solution.
Save the DataFrame to the database
DataFrame comes with to_sql method, which is used as follows.
|
|
Parameter Description.
- name: database table name
- con: database connection
- schema: schema of the database module, not needed for most connections
- if_exists: optional: {‘fail’, ‘replace’, ‘append’}
- index: if or not the index column of the DataFrame will be written
- index_label: the name of the index column
- chunksize: set the number of entries to be written in bulk
- dtype: set the type of the field
- method: insert method, can not be set
Usage examples.
|
|
Traditional way: fetching database data by cursor
Code example.
Transforming data from the database into a DataFrame.
Easy way: read directly using pd.read_sql()
|
|