Reputation: 3
I am using jupyter notebook to return this dataframe. I am trying to color the word "Bullish" green and "Bearish" red. Also if it is bullish the Buy/Sell Trade price should be green and Bearish the Buy/Sell price should be Red. Thanks for your help!
Sample file here: https://drive.google.com/drive/folders/1WWdFlXIqQCM6LyeWZglPzqUwhCy3blTD?usp=sharing
import pandas as pd
import numpy as np
from datetime import date
from datetime import timedelta
df_rr = pd.read_csv("W:\mac\dev\py\RR.csv", parse_dates=["DATE"], index_col="DATE")
df_rr.loc["2021-03-10"]
EDIT 1
The code works but it drops my date column. DO you know what happened to it? Also when I use this code, I get the following error. I want to be able to return 1 date of data so that is why I use: df_rr.loc["2021-03-10"]
I get AttributeError: 'Styler' ojbect has no attribute 'loc'
is there a better way to limit by one date?
EDIT 2
3/12/2021 Thanks for your edit. One final thing, can you help make the text in the INDEX column for Bullish and Bearish colored as well. Really appreciate you.
FOLLOW UP - This works great but can I only have the words BULLISH and BEARISH colored instead of the full text string? Thanks!
EDIT 3
It looks the new code adds zeros to my values. How do you chop trailing zeros in the price columns? Thanks!
EDIT 4 - 4/2/2021
Here is the view after I add in your code
3 questions:
Thanks as always!
Upvotes: 0
Views: 653
Reputation: 13349
You can colour using style
def cts(x):
c1 = 'color: red'
c2 = 'color: green'
c3 = 'color: black'
if 'BULLISH' in x['INDEX']:
return c3,c1,c1
elif 'BEARISH' in x['INDEX']:
return c3,c2,c2
else:
return c3,c3,c3
df = df.reset_index(drop=True).style.apply(cts,subset=['INDEX', 'BUY TRADE', 'SELL TRADE'], axis=1)
df:
df.loc['2020-12-07'].reset_index().style.apply(cts,subset=['INDEX', 'BUY TRADE', 'SELL TRADE'], axis=1)
If you want to colour INDEX
also do: (Initially I was showing black but for those you can change the color to red/green based on the condition)
if 'BULLISH' in x['INDEX']:
return c1,c1,c1
elif 'BEARISH' in x['INDEX']:
return c2,c2,c2
else:
return c3,c3,c3
Upvotes: 1