I have some data in the form of a csv file that I'm reading into Python and converting to an HTML table using Pandas.
Heres some example data:
name threshold col1 col2 col3
A 10 12 9 13
B 15 18 17 23
C 20 19 22 25
And some code:
import pandas as pd
df = pd.read_csv("data.csv")
table = df.to_html(index=False)
This creates the following HTML:
<table border="1" class="dataframe"><thead><tr style="text-align: right;"><th>name</th><th>threshold</th><th>col1</th><th>col2</th><th>col3</th></tr></thead><tbody><tr><td>A</td><td>10</td><td>12</td><td>9</td><td>13</td></tr><tr><td>B</td><td>15</td><td>18</td><td>17</td><td>23</td></tr><tr><td>C</td><td>20</td><td>19</td><td>22</td><td>25</td></tr></tbody>
</table>
No I want to conditionally add a class to each cell in the html table if its value is less than a certain threshold. The threshold is different for each row in the table.
So given the example data above, I want to add a class, class="custom" to cell col2 with name A and to cell col1 with name C. In CSS, I will then fill the cell color as red if it has the "custom" class.
The result would be something like:
<table border="1" class="dataframe"><thead><tr style="text-align: right;"><th>name</th><th>threshold</th><th>col1</th><th>col2</th><th>col3</th></tr></thead><tbody><tr><td>A</td><td>10</td><td>12</td><td class="custom">9</td><td>13</td></tr><tr><td>B</td><td>15</td><td>18</td><td>17</td><td>23</td></tr><tr><td>C</td><td>20</td><td class="custom">19</td><td>22</td><td>25</td></tr></tbody>
</table>
How can this be achieved using Beautiful Soup?