1

So I have a dataframe that has multiple columns and I am using this code to convert the dataframe to HTML code:

options_df = pd.DataFrame(data, columns=[
    'Ticker', 'Type', 'Strike', 'Expiration', 'Filled', 'Current Premium', 'P/L', 'Date Signaled'])

html_str = '''
<html>
    <head><title>HTML Pandas Dataframe with CSS</title></head>
    <link rel="stylesheet" href="df_styles.css"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script src="/js/my.js"></script>
    <body>
        {options_table}
    </body>
</html>.
'''

with open("index.html", "w") as options_f:
    options_f.write(html_str.format(
        options_table=options_df.to_html(classes='mystyle')))

This outputs as: (Only showing the first row for length purposes)

<html>
    <head><title>HTML Pandas Dataframe with CSS</title></head>
    <link rel="stylesheet" href="df_styles.css"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script src="/js/my.js"></script>
    <body>
        <table border="1" class="dataframe mystyle">
<thead>
<tr style="text-align: right;">
  <th></th>
  <th>Ticker</th>
  <th>Type</th>
  <th>Strike</th>
  <th>Expiration</th>
  <th>Filled</th>
  <th>Current Premium</th>
  <th>P/L</th>
  <th>Date Signaled</th>
</tr>
</thead>
<tbody>
<tr>
  <th>0</th>
  <td>$OSTK</td>
  <td>Call</td>
  <td>90</td>
  <td>2021-03-19</td>
  <td>7.9</td>
  <td>3.06</td>
  <td>-484</td>
  <td>2020-11-30</td>
</tr>

For the eighth column (so the column that reads -484 in the output above) in each row I would like give it a specific id or class. Is there a way to do that within python using pandas dataframe.to_html(), or is there another function that can do this?

If not, the task I am trying to do is change the color of the background of the eighth column depending on whether the value in it is negative or positive. And my idea was to do this in jQuery, but without a specific id or class, it looks like I can't do this. Is there another way I can accomplish this? Thanks!

1 Answer 1

2

One quick-and-dirty option is to process the string returned by df.to_html(). For example:

data = [
    ['K1', 1],
    ['K2', -1],
]

df = pd.DataFrame(data, columns=['key', 'value'])

m_positive = df['value'] > 0
df.loc[m_positive, 'value'] = '__positive__' + df.loc[m_positive, 'value'].astype(str)
df.loc[~m_positive, 'value'] = '__negative__' + df.loc[~m_positive, 'value'].astype(str)

def updateRowStyle(line):
    return line.replace('>__positive__', ' class = "positive">').replace('>__negative__', ' class="negative">')

result =list(map(updateRowStyle, df.to_html().split('\n')))

for line in result:
    print(line)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.