I need to generate an excel sheet based on the records of MySQL. But before writing them into excel I want to add new key-value in a data frame by checking the condition of amount > 10000. Unfortunately, the status is always showing Ok status in the data frame. Even though the condition is working fine it is not getting reflected in the data frame. Some ideas or hint is really appreciated.
import mysql.connector
import pandas as pd
import numpy as np
import sys
conn = mysql.connector.connect(host='localhost',
database='db_name',
user='sammy',
password='password')
def apply_color(x):
if x < 10000:
color = 'red'
else:
color = 'white'
return 'background-color: %s' %color
if conn.is_connected():
writer = pd.ExcelWriter('demo.xlsx', engine='xlsxwriter')
writer.save()
SQL_Query=pd.read_sql_query('''select * from demo_table''', conn)
df = pd.DataFrame(SQL_Query, columns=['id','desp','amount'])
df['status'] = np.where(df['amount'].astype(float) >= 10000, 'OK', 'NOTOKS')
df.style.applymap(apply_color, subset=['status'])
writer = pd.ExcelWriter('demo.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', index=False)
writer.save()