Pandas and SQL
Reading Pandas DataFrame using SQL select statements
Customer Key | WWI Customer ID | Customer | Bill To Customer | Category | Buying Group | Primary Contact | Postal Code | Credit Limit | Valid From | Valid To | Lineage Key | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 0 | Unknown | NaN | NaN | NaN | NaN | NaN | ? - | 00:00.0 | ##############################################... | 0 |
Customer Key | WWI Customer ID | WWI Customer ID | |
---|---|---|---|
0 | 0 | 0 | Unknown |
Read from SQL to DF
install sqlalchemy
to us pandas's read_sql
and to_sql
functions
sqlalchemy
to us pandas's read_sql
and to_sql
functionsalternatively you can use sqlalchemy with a little extra code
install pymysql (it is an extension for sqlalchemy to enable it to connect to mysql sources)
create a connection for pandas using slqalchemy function
sqlalchemy.create_engine('mysql+pymysql://username
:@host_name
/db_name
')
Id | Name | City | |
---|---|---|---|
0 | 1 | Ajeet | Ghaziabad |
1 | 2 | Rashid | Noida |
2 | 3 | Ajay | Gurgaon |
3 | 4 | Ramu | Hyderbad |
4 | 5 | Rajesh | Bangalore |
Once you read the data from SQL and store into a DataFrame you can do anything you want with it
Now if you want you can save this DataFrame in you SQL Database
Congratulation !! 🎉
You can now access/read DataFrames using SQL Select statements instead of using the traditional Datafram syntax.
You can also read data from SQL and save the result as DataFrame.
You can save an existing dataframe directly into sql table.
Last updated