Here we are trying to load packages and then write an SQl query to integrate with Pandas and then finally using Bokeh to show the plot But bokeh is not showing anything.
You can consider the following as the dataset 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
The code starts here
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