HomeLinuxPandas to HTML

Pandas to HTML


Pandas in Python can alter a Pandas DataFrame into an HTML desk. A Pandas DataFrame is executed utilizing the “pandas.DataFrame.to_html()” technique. Let’s take a look at our instance and focus on the process to rework our Python DataFrame to HTML supply code. To perform this, we should first design the DataFrame that finally renders into HTML.

Syntax:

 

pandas.DataFrame.to_html(index,max_rows,max_cols)

 

Parameters:

    1. If “index” is ready to True, the HTML output comprises the index for every row. In any other case, no index is current whether it is set to False. By default, it’s True.
    2. The max_rows takes an integer worth that converts the desired variety of rows into an HTML format.
    3. The max_cols takes an integer worth that converts the desired variety of columns into an HTML format.

Return Format:

All the information is positioned underneath the desk tag.

The index values are positioned underneath the “<th>” tag. The row values are positioned underneath the “<td>” tag which is underneath the “<th>” column.

Instance 1: With No Parameter

On this instance, we’ve got a DataFrame named “issues” that holds the “Product”, ”Title”, and “Bought Standing” columns with 2 rows.

Convert this DataFrame to HTML by passing no parameter.

import pandas

# Create a pandas dataframe with 3 columns.
issues=pandas.DataFrame({‘Product’:[1,2],
                            ‘Title’:[‘Solar dish’,‘glasses’],
                            ‘Bought Standing’:[1,0]},index=[‘one’,‘two’])
print(issues)

print()

# Convert issues DataFrame to html
print(issues.to_html())

 
Output:

     Product        Title  Bought Standing
one        1  Photo voltaic dish                 1
two        2     glasses                 0

<desk border=“1” class=“dataframe”>
  <thead>
    <tr fashion=“text-align: proper;”>
      <th></th>
      <th>Product</th>
      <th>Title</th>
      <th>Bought Standing</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>one</th>
      <td>1</td>
      <td>Photo voltaic dish</td>
      <td>1</td>
    </tr>
    <tr>
      <th>two</th>
      <td>2</td>
      <td>glasses</td>
      <td>0</td>
    </tr>
  </tbody>
</desk>

 
Clarification:

Within the first output, we displayed the precise DataFrame. Within the second output, our DataFrame is displayed in HTML format.

Let’s run the HTML code output within the browser.

Instance 2: With the Index Parameter

Convert this DataFrame to HTML by ignoring the index. Right here, the index is ready to False.

import pandas

issues=pandas.DataFrame({‘Product’:[1,2],
                            ‘Title’:[‘Solar dish’,‘glasses’],
                            ‘Bought Standing’:[1,0]},index=[‘one’,‘two’])
# Convert to html with out index
print(issues.to_html(index=False))

 
Output:

<desk border=“1” class=“dataframe”>
  <thead>
    <tr fashion=“text-align: proper;”>
      <th>Product</th>
      <th>Title</th>
      <th>Bought Standing</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>Photo voltaic dish</td>
      <td>1</td>
    </tr>
    <tr>
      <td>2</td>
      <td>glasses</td>
      <td>0</td>
    </tr>
  </tbody>
</desk>

 
Clarification:

Our DataFrame is displayed in HTML format with out the index values.

Let’s run the HTML code output within the browser.


You’ll be able to see that the index values weren’t discovered within the HTML desk.

Instance 3: With the Max_Rows Parameter

Convert solely the primary row of the DataFrame to HTML by setting the max_rows parameter to 1.

import pandas

issues=pandas.DataFrame({‘Product’:[1,2],
                            ‘Title’:[‘Solar dish’,‘glasses’],
                            ‘Bought Standing’:[1,0]})

# Convert to html with one row.
print(issues.to_html(max_rows=1))

 
Output:

<desk border=“1” class=“dataframe”>
  <thead>
    <tr fashion=“text-align: proper;”>
      <th></th>
      <th>Product</th>
      <th>Title</th>
      <th>Bought Standing</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1</td>
      <td>Photo voltaic dish</td>
      <td>1</td>
    </tr>
  </tbody>
</desk>

 
Clarification:

Our DataFrame is displayed in HTML format that has just one row.

Let’s run the HTML code output within the browser.

Instance 4: With the Max_Cols Parameter

Convert solely the primary column of the DataFrame to HTML by setting the max_cols parameter  to 1.

import pandas

issues=pandas.DataFrame({‘Product’:[1,2],
                            ‘Title’:[‘Solar dish’,‘glasses’],
                            ‘Bought Standing’:[1,0]})

# Convert to html with one column.
print(issues.to_html(max_cols=1))

 
Output:

<desk border=“1” class=“dataframe”>
  <thead>
    <tr fashion=“text-align: proper;”>
      <th></th>
      <th>Product</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1</td>
      <td></td>
    </tr>
    <tr>
      <th>1</th>
      <td>2</td>
      <td></td>
    </tr>
  </tbody>
</desk>

 
Clarification:

Our DataFrame is displayed in HTML format that has just one column.

Let’s run the HTML code output within the browser.

Conclusion

When rendering a DataFrame into an HTML code, we use the issues.to_html() operate. Additionally, we convert the prevailing DataFrame by setting the index, max_rows and max_cols parameters in separate examples. For all of the outputs, we displayed the HTML precise information by offering a screenshot after each output.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments