Solution 1 :

Just keep it simple. Filter your Dataframe to desired recored before you call to_html()

In example I filter to rows when index is between 1 and 2

df = pd.DataFrame([{'Zone': 'AZ', 'roll': '', 'prod_id': '', 'match_id': '32'},
 {'Zone': 'PO', 'roll': 'cshdgs', 'prod_id': '+1352648', 'match_id': '13'},
 {'Zone': 'RT', 'roll': 'abc', 'prod_id': '+1235', 'match_id': '12'},
 {'Zone': 'SZ', 'roll': 'cshdgs', 'prod_id': '+1352648', 'match_id': '1352'},
 {'Zone': 'WW', 'roll': 'abc', 'prod_id': '+1235', 'match_id': '123'}]).reset_index()

df[df["index"].between(1,2)].to_html(index=False)

output

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th>index</th>
      <th>Zone</th>
      <th>roll</th>
      <th>prod_id</th>
      <th>match_id</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>PO</td>
      <td>cshdgs</td>
      <td>+1352648</td>
      <td>13</td>
    </tr>
    <tr>
      <td>2</td>
      <td>RT</td>
      <td>abc</td>
      <td>+1235</td>
      <td>12</td>
    </tr>
  </tbody>
</table>

Problem :

I am working on a project and I want to display a specific row after rendering a dataframe as a HTML table. The dataframe was created by someone else and I need to display a specific row on the page. I can’t show the actual code, so I’ll use place holders instead.

Rendering dataframe as HTML

Displaying the table using Jinja

How the table looks like

I would like to be able to display a specific row instead, i.e. display the row that is Index “2” but with the columns still present. The HTML code is what I would get back, so how do I display a specific row on the web page?

Displaying a specific row

<table>
  <thead>
    <tr>
      <th></th>
      <th>First</th>
      <th>Second</th>
      <th>Third</th>
    </tr>
    <tr>
      <th>Index</th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  <thead>

 <tbody>
   <tr>
     <th>1</th>
     <td>one</td>
     <td>two</td>
     <td>three</td>
   </tr>
   <tr>
     <th>2</th>
     <td>one</td>
     <td>two</td>
     <td>three</td>
   </tr>
 </tbody>
</table>

Comments

Comment posted by Jacobm001

Are you trying to generate an html table out of a subset of the data or are you trying to alter the page display of the generated html?

Comment posted by Marcus

@Jacobm001 I’m trying to generate a html table out of a subset of the data

Comment posted by Cliabhach

I having some trouble understanding the initial call to

By

Leave a Reply

Your email address will not be published. Required fields are marked *