Can anybody help me out with the following strange scenario? So I have the following Table ( Oracle ) which looks like as follows: Screenshot
Going back to python, I have created the following Script. I connect to the database and afterwards I retrieve the data from within the table. Afterwards, the dataframe is built upon the content of the table. Using the function highlightGreaterThen I want to add a background color to a cell, depending on the value. Unfortunately this is not happening as desired, because it seems that the function keeps on going on the ELSE Clause, even though the IF is not meant.
#! /usr/bin/python
# import the necessary components first
import cx_Oracle
import config
import pandas as pd
connection = cx_Oracle.connect(
config.username,
config.password,
config.dsn,
encoding=config.encoding
)
mon_user_accounts = """ SELECT USER_ID, USER_NAME, ACCOUNT_STATUS, REMAINING_DAYS,
expiry_date , inserted_time as "STATUS_TIME"
FROM KVS_SERVICE.mon_user_accounts
WHERE inserted_time = (select max(inserted_time) from mon_user_accounts) """
df_usr_acc = pd.read_sql(mon_user_accounts, con=connection)
def highlightGreaterThen(color):
for value in df_usr_acc['REMAINING_DAYS']:
if value <= 30:
color = 'chocolate'
else:
color = 'honeydew'
return 'background-color: %s' % color
s = df_usr_acc.style.set_table_attributes('class="table table-hover table-condensed"').applymap(
highlightGreaterThen, subset=['USER_ID'])
to_render = s.hide_index().render()
print(to_render)
If I execute the function manually, and add some print within it, than it prints the desired result, but when executing it within my script, it doesn't do good.
for value in df_usr_acc['REMAINING_DAYS']:
if value <= 30:
print('chocolate')
else:
print('honeydew')
The result is:
honeydew
chocolate
honeydew
honeydew
honeydew
honeydew
honeydew
But when executing the entire script, the result is:
T_ce7b8808_c6a5_11ea_9513_34e12def64cdrow0_col0 { background-color: honeydew;}
T_ce7b8808_c6a5_11ea_9513_34e12def64cdrow1_col0 { background-color: honeydew;}
T_ce7b8808_c6a5_11ea_9513_34e12def64cdrow2_col0 { background-color: honeydew;}
T_ce7b8808_c6a5_11ea_9513_34e12def64cdrow3_col0 { background-color: honeydew;}
T_ce7b8808_c6a5_11ea_9513_34e12def64cdrow4_col0 { background-color: honeydew;}
T_ce7b8808_c6a5_11ea_9513_34e12def64cdrow5_col0 { background-color: honeydew;}
T_ce7b8808_c6a5_11ea_9513_34e12def64cdrow6_col0 { background-color: honeydew;}
What Am I doing wrong?
Thanks.
Well it seems that when looping within the function, the code was appended only with the last encountered value. That being said, when using the print, the iteration was successfully done. The function has been changed to:
def userAccountHighlightGreaterThen(color):
return ['background-color: chocolate'
if value <=30
else 'background-color: honeydew'
for value in df_usr_acc['REMAINING_DAYS']
]
Afterwards, I have called the function using style.apply instead of style.applymap. This solved my issue. Hope it will help you out as well.