将 sql、pandas 与 Bokeh 相结合
Integrating sql,pandas alongwith Bokeh
这里我们尝试加载包,然后编写一个 SQl 查询以与 Pandas 集成,最后使用 Bokeh 显示情节但是 bokeh 没有显示任何内容。
您可以将以下内容视为数据集df_new_2
:
name success_rate failure_rate
A 94.7 5.3
B 94.3 5.7
C 91 9
D 88 13
E 84 16
F 81 19
G 78 22
H 74.6 25.4
代码从这里开始
import pandas.io.sql
import pandas as pd
import pyodbc
from bokeh import mpl
from bokeh.plotting import output_file,show
server = 'root' #getting the server to work
db = 'y' #assigning database
# Create the connection
conn = pyodbc.connect("DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;PORT= 3306;DATABASE=y;UID=root;PWD=123456789;")
cursor=conn.cursor()
# query db- Here we are trying to count the number of success in a table and the name for which the success has been found by joining tables
sql = """
SELECT count(*) AS TOTAL,
COUNT(CASE WHEN status=0 THEN 1 END) AS success,
b.name
FROM a
JOIN b
ON b.id=a.merchant
GROUP BY merchant
LIMIT 10
"""
df = pandas.io.sql.read_sql(sql, conn) #defining df as query result
df.head()
df_new=df.set_index('name') #indexing as the name of a
df_new['success_rate']=df_new['success']*100/df_new['TOTAL']
df_new['failure_rate']=100-df_new['success_rate'] #assigning failure rate
df_new2=pd.DataFrame(df_new,columns=['success_rate','failure_rate'])
p=df_new2.plot(kind='barh',stacked=True)
output_file("pandas_series.html", title="pandas_series.py example") #assigning the name of output screen
show(mpl.to_bokeh) #showing the output of bokeh
现在有一些对您更有用的东西。不得不避免使用 mpl,因为我无法让它工作。一个可能的原因是我认为水平条形图在散景中不可用。
import pandas as pd
from bokeh.charts import Bar
from bokeh.plotting import output_file, show
from bokeh.charts.operations import blend
from bokeh.charts.attributes import cat, color
df_new2 = pd.DataFrame({'Success Rate' : [94.7,94.3,91,88,84,81,78,74.6], 'Failure Rate' : [5.3,5.7,9,12,16,19,22,25.4]})
df_new2['inds'] = ['A','B','C','D','E','F','G','H']
p = Bar(df_new2,
values=blend('Failure Rate','Success Rate', name='% Success/Failure', labels_name='stacked'),
label=cat('inds'),
stack=cat(columns='stacked', sort=False),
color=color(columns='stacked', palette=['Red', 'Green'],
sort=False),
legend='top_right',
title="Success Rate vs. Failure Rate")
output_file("pandas_series.html", title="pandas_series.py example") #assigning the name of output screen
show(p) #showing the output of bokeh
这里我们尝试加载包,然后编写一个 SQl 查询以与 Pandas 集成,最后使用 Bokeh 显示情节但是 bokeh 没有显示任何内容。
您可以将以下内容视为数据集df_new_2
:
name success_rate failure_rate
A 94.7 5.3
B 94.3 5.7
C 91 9
D 88 13
E 84 16
F 81 19
G 78 22
H 74.6 25.4
代码从这里开始
import pandas.io.sql
import pandas as pd
import pyodbc
from bokeh import mpl
from bokeh.plotting import output_file,show
server = 'root' #getting the server to work
db = 'y' #assigning database
# Create the connection
conn = pyodbc.connect("DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;PORT= 3306;DATABASE=y;UID=root;PWD=123456789;")
cursor=conn.cursor()
# query db- Here we are trying to count the number of success in a table and the name for which the success has been found by joining tables
sql = """
SELECT count(*) AS TOTAL,
COUNT(CASE WHEN status=0 THEN 1 END) AS success,
b.name
FROM a
JOIN b
ON b.id=a.merchant
GROUP BY merchant
LIMIT 10
"""
df = pandas.io.sql.read_sql(sql, conn) #defining df as query result
df.head()
df_new=df.set_index('name') #indexing as the name of a
df_new['success_rate']=df_new['success']*100/df_new['TOTAL']
df_new['failure_rate']=100-df_new['success_rate'] #assigning failure rate
df_new2=pd.DataFrame(df_new,columns=['success_rate','failure_rate'])
p=df_new2.plot(kind='barh',stacked=True)
output_file("pandas_series.html", title="pandas_series.py example") #assigning the name of output screen
show(mpl.to_bokeh) #showing the output of bokeh
现在有一些对您更有用的东西。不得不避免使用 mpl,因为我无法让它工作。一个可能的原因是我认为水平条形图在散景中不可用。
import pandas as pd
from bokeh.charts import Bar
from bokeh.plotting import output_file, show
from bokeh.charts.operations import blend
from bokeh.charts.attributes import cat, color
df_new2 = pd.DataFrame({'Success Rate' : [94.7,94.3,91,88,84,81,78,74.6], 'Failure Rate' : [5.3,5.7,9,12,16,19,22,25.4]})
df_new2['inds'] = ['A','B','C','D','E','F','G','H']
p = Bar(df_new2,
values=blend('Failure Rate','Success Rate', name='% Success/Failure', labels_name='stacked'),
label=cat('inds'),
stack=cat(columns='stacked', sort=False),
color=color(columns='stacked', palette=['Red', 'Green'],
sort=False),
legend='top_right',
title="Success Rate vs. Failure Rate")
output_file("pandas_series.html", title="pandas_series.py example") #assigning the name of output screen
show(p) #showing the output of bokeh