我们可以连接到关系数据库以使用pandas
库分析数据,以及另一个用于实现数据库连接的额外库。 这个软件包被命名为sqlalchemy,它提供了在python中使用的完整的sql语言功能。
使用在pandas环境安装章中讨论的anaconda,安装非常简单。 假设您已经按照本章的说明安装了anaconda,请在anaconda提示窗口中运行以下命令来安装sqlalchemy软件包。
conda install sqlalchemy
我们将使用sqlite3作为关系数据库,因为它非常轻便且易于使用。 尽管sqlalchemy库可以连接到各种关系源,包括mysql,oracle和postgresql以及mssql。 我们首先创建一个数据库引擎,然后使用sqlalchemy库的to_sql
函数连接到数据库引擎。
在下面的例子中,我们通过使用已经通过读取csv文件创建的数据帧中的to_sql
函数来创建关系表。 然后使用pandas
的read_sql_query
函数来执行和捕获来自各种sql查询的结果。
from sqlalchemy import create_engine
import pandas as pd
data = pd.read_csv('/path/input.csv')
# create the db engine
engine = create_engine('sqlite:///:memory:')
# store the dataframe as a table
data.to_sql('data_table', engine)
# query 1 on the relational table
res1 = pd.read_sql_query('select * from data_table', engine)
print('result 1')
print(res1)
print('')
# query 2 on the relational table
res2 = pd.read_sql_query('select dept,sum(salary) from data_table group by dept', engine)
print('result 2')
print(res2)
执行上面示例代码,得到以下结果 -
result 1
index id name salary start_date dept
0 0 1 rick 623.30 2012-01-01 it
1 1 2 dan 515.20 2013-09-23 operations
2 2 3 tusar 611.00 2014-11-15 it
3 3 4 ryan 729.00 2014-05-11 hr
4 4 5 gary 843.25 2015-03-27 finance
5 5 6 rasmi 578.00 2013-05-21 it
6 6 7 pranab 632.80 2013-07-30 operations
7 7 8 guru 722.50 2014-06-17 finance
result 2
dept sum(salary)
0 finance 1565.75
1 hr 729.00
2 it 1812.30
3 operations 1148.00
还可以使用pandas中提供的sql.execute
函数将数据插入到关系表中。 在下面的代码中,我们将先前的csv文件作为输入数据集,将其存储在关系表中,然后使用sql.execute
插入另一条记录。
from sqlalchemy import create_engine
from pandas.io import sql
import pandas as pd
data = pd.read_csv('c:/users/rasmi/documents/pydatasci/input.csv')
engine = create_engine('sqlite:///:memory:')
# store the data in a relational table
data.to_sql('data_table', engine)
# insert another row
sql.execute('insert into data_table values(?,?,?,?,?,?)', engine, params=[('id',9,'ruby',711.20,'2015-03-27','it')])
# read from the relational table
res = pd.read_sql_query('select id,dept,name,salary,start_date from data_table', engine)
print(res)
执行上面示例代码,得到以下代码 -
id dept name salary start_date
0 1 it rick 623.30 2012-01-01
1 2 operations dan 515.20 2013-09-23
2 3 it tusar 611.00 2014-11-15
3 4 hr ryan 729.00 2014-05-11
4 5 finance gary 843.25 2015-03-27
5 6 it rasmi 578.00 2013-05-21
6 7 operations pranab 632.80 2013-07-30
7 8 finance guru 722.50 2014-06-17
8 9 it ruby 711.20 2015-03-27
还可以使用pandas中的sql.execute
函数将数据删除到关系表中。 下面的代码根据给定的输入条件删除一行。
from sqlalchemy import create_engine
from pandas.io import sql
import pandas as pd
data = pd.read_csv('c:/users/rasmi/documents/pydatasci/input.csv')
engine = create_engine('sqlite:///:memory:')
data.to_sql('data_table', engine)
sql.execute('delete from data_table where name = (?) ', engine, params=[('gary')])
res = pd.read_sql_query('select id,dept,name,salary,start_date from data_table', engine)
print(res)
执行上面示例代码,得到以下结果 -
id dept name salary start_date
0 1 it rick 623.3 2012-01-01
1 2 operations dan 515.2 2013-09-23
2 3 it tusar 611.0 2014-11-15
3 4 hr ryan 729.0 2014-05-11
4 6 it rasmi 578.0 2013-05-21
5 7 operations pranab 632.8 2013-07-30
6 8 finance guru 722.5 2014-06-17