skip to Main Content

I have ‘n’ after > and spaces before <. Is there a way to don’t save them when I call to_html method?
I have:

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">

I want to have:

<table border="1" class="dataframe"><thead><tr style="text-align: right;">

I tried escape=False, sparsify=True, justify='center' but it didn’t work.

2

Answers


  1. You cannot do this with pandas alone, but it’s easy to post-process the string to remove the newlines and following spaces:

    import re
    import pandas as pd
    
    df = pd.DataFrame()
    
    out = re.sub(r'ns*', '', df.to_html())
    
    print(out)
    

    Output:

    <table border="1" class="dataframe"><thead><tr style="text-align: right;"><th></th></tr></thead><tbody></tbody></table>
    
    Login or Signup to reply.
  2. You can try package minify-html:

    from minify_html import minify
    
    import pandas as pd
    
    
    df = pd.DataFrame(
        {
            "a": [1, 1, 1, 1],
            "b": [1, 2, 3, 4],
            "c": [np.nan, 6, 7, 8],
        }
    )
    
    s = minify(df.to_html(index=False))
    print(s)
    

    Prints:

    <table border=1 class=dataframe><thead><tr style="text-align: right;"><th>a<th>b<th>c<tbody><tr><td>1<td>1<td>NaN<tr><td>1<td>2<td>6.0<tr><td>1<td>3<td>7.0<tr><td>1<td>4<td>8.0</table>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search