#Import previously saved model
from absenteeism_module import *
model = absenteeism_model('model', 'scaler')
#Import new data set
model.load_and_clean_data('Absenteeism_new_data.csv')
# Predict with new value
model.predicted_outputs()
#Import library for mysql
import pymysql
# Specify the database details for further peocess.
conn = pymysql.connect(database = 'predicted_outputs', user = 'nativeuser', password = '365Pass')
#Create the cursor to involke the connection
cursor = conn.cursor()
#Store the predicted output in the nwe data frame called 'df_new_obs'
df_new_obs = model.predicted_outputs()
df_new_obs
# To use .execute() method retrive the data from data base
cursor.execute('SELECT * FROM predicted_outputs;')
# Output 0 shows there is no data present in the table
#Asssign insert query to variable
insert_query = 'INSERT INTO predicted_outputs VALUES '
insert_query
df_new_obs.shape
So in our new data freame we have 40 rows and 13 column
df_new_obs['Age']
df_new_obs[df_new_obs.columns.values[6]][0]
#The first iteration we want to check every row in the dataframe and then run insert command
for i in range(df_new_obs.shape[0]):
insert_query += '('
#Similarly we want to insert data in column. Here j represent column
for j in range(df_new_obs.shape[1]):
insert_query += str(df_new_obs[df_new_obs.columns.values[j]][i]) + ', '
insert_query = insert_query[:-2] + '), '
insert_query
As we can see above insert query at the end it is , but in data base query it should be ;. To do that we need to follow below steps
#Remove ',' from insert query and pur ';' in that.
insert_query = insert_query[:-2] + ';'
# Now check again it has been corrected or not
insert_query
# Now with the help of cursor execute the insert query
cursor.execute(insert_query)
# You have to commit it for successful insertion in your data base
conn.commit()
# Close the connection
conn.close()