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!