3

Here is a minimalist example of what seems to be a problem with pandas.to_html in pandas==0.25.1

I create a simple dataframe that, when converted to html, suffers from extraneous newline character padding.

df = pd.DataFrame()
>>> df['name']=['barney','frank']
>>> df['age'] = [4,5]
>>> df
     name  age
0  barney    4
1   frank    5
>>> df.to_html()

Seem innocent enough but we get..

<table border="1" class="dataframe">\n  <thead>\n    <tr style="text-align: right;">\n      <th></th>\n      <th>name</th>\n      <th>age</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>barney</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>frank</td>\n      <td>5</td>\n    </tr>\n  </tbody>\n</table>

What's with all the /n ?

Peter Cotton
  • 1,671
  • 14
  • 17
  • The number of `\n` roughly corresponds to the number of spaces in the table definition `>>>> df`, I doubt that's a coincidence. Consider defining the datatable in a more controlled manner, using something like this: [see SO example](https://stackoverflow.com/a/54508159/11700321) or even this [another SO example](https://stackoverflow.com/a/50939211/11700321) – EGC Oct 18 '19 at 00:30
  • That's a good thought but I can reproduce the same effect using df = pd.DataFrame([[1, 2], [1, 3], [4, 6]], columns=['A', 'B']) instead. – Peter Cotton Oct 18 '19 at 01:07
  • 3
    For now I'm just doing df.to_html().replace('\n','') but this seems kinda lame. – Peter Cotton Oct 18 '19 at 01:15

0 Answers0