Issue
I have a dict which i create df
data = {'BTC': {'PNL': 834.8485, 'FUNDING': 575.20837762, 'DELTA VOL': 18845436.3535, 'HEDGE VOL': 16444662.545, 'OI': 4598885.7407, 'DEFICIT': 149.35084}, 'BTC 4HR': {'PNL': 76.2125, 'FUNDING': 197.52681642, 'DELTA VOL': 1999585.538, 'HEDGE VOL': 1729156.5247, 'OI': None, 'DEFICIT': None}, 'ETH': {'PNL': -661.112, 'FUNDING': -218.42574899, 'DELTA VOL': 10720104.9915, 'HEDGE VOL': 10088271.477, 'OI': 552973.992, 'DEFICIT': 61.9926}, 'ETH 4HR': {'PNL': 65.331, 'FUNDING': 23.57589308, 'DELTA VOL': 373668.3985, 'HEDGE VOL': 356349.724, 'OI': None, 'DEFICIT': None}, 'TOTAL': {'PNL': 2859.98348042, 'FUNDING': 871.26688115, 'DELTA VOL': 33607091.07200743, 'HEDGE VOL': 30570729.07912772, 'OI': 6019831.030358, 'DEFICIT': 9250.907426}, 'TOTAL 4HR': {'PNL': 177.84871257, 'FUNDING': 246.41302914, 'DELTA VOL': 2881419.73415996, 'HEDGE VOL': 2592306.47723815, 'OI': None, 'DEFICIT': None}}
after creating df from this, need to make bold 0,0 cell 0,2 and 0,4 cells only. tried with style.applymap but couldnt succeed.
df1 = pd.DataFrame(data).T
df1
def color_negative_red(val):
try:
if val < 0:
color = 'red'
elif val>0:
color='green'
return 'color: %s' % color
except:
return 'color: black'
also making color code changes which is working.
df1=df1.style.apply(styler).set_caption("Futures Risk Monitor").format("{:20,.0f}").set_table_styles([{
'selector': 'thead th',
'props': [('text-align', 'center')]
}])
display(df1)
Solution
You can use a custom function, building on your existing color_negative_red
:
def color_negative_red(val):
try:
if val < 0:
color = 'red'
elif val>0:
color='green'
return 'color: %s' % color
except:
return 'color: black'
def styler(df):
style = df.applymap(color_negative_red)
style.iloc[0, [0,2,4]] += '; font-weight: bold'
return style
(df1.style.apply(styler, axis=None)
.set_caption("Futures Risk Monitor")
.format("{:20,.0f}")
.set_table_styles([{'selector': 'thead th',
'props': [('text-align', 'center')]}])
)
Output:
Answered By - mozway
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.